Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
7c72864c
Commit
7c72864c
authored
Oct 18, 2017
by
Toon Claes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract try_obtain_lease to a concern
parent
370fb882
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
56 deletions
+24
-56
app/workers/concerns/exclusive_lease_guard.rb
app/workers/concerns/exclusive_lease_guard.rb
+20
-0
app/workers/geo/base_scheduler_worker.rb
app/workers/geo/base_scheduler_worker.rb
+1
-31
app/workers/geo/repositories_clean_up_worker.rb
app/workers/geo/repositories_clean_up_worker.rb
+3
-25
No files found.
app/workers/concerns/exclusive_lease_guard.rb
View file @
7c72864c
#
# Concern that helps with getting an exclusive lease for running a worker
#
# `#try_obtain_lease` takes a block which will be run if it was able to obtain the lease.
# Implement `#lease_timeout` to configure the timeout for the exclusive lease.
# Optionally override `#lease_key` to set the lease key, it defaults to the class name with underscores.
#
module
ExclusiveLeaseGuard
extend
ActiveSupport
::
Concern
# override in subclass
def
lease_timeout
raise
NotImplementedError
end
def
lease_key
@lease_key
||=
self
.
class
.
name
.
underscore
end
def
log_error
(
message
,
extra_args
=
{})
logger
.
error
(
messages
)
end
def
try_obtain_lease
lease
=
exclusive_lease
.
try_obtain
...
...
@@ -27,4 +43,8 @@ module ExclusiveLeaseGuard
def
release_lease
(
uuid
)
Gitlab
::
ExclusiveLease
.
cancel
(
lease_key
,
uuid
)
end
def
renew_lease!
exclusive_lease
.
renew
end
end
app/workers/geo/base_scheduler_worker.rb
View file @
7c72864c
...
...
@@ -2,6 +2,7 @@ module Geo
class
BaseSchedulerWorker
include
Sidekiq
::
Worker
include
CronjobQueue
include
ExclusiveLeaseGuard
DB_RETRIEVE_BATCH_SIZE
=
1000
LEASE_TIMEOUT
=
60
.
minutes
...
...
@@ -72,10 +73,6 @@ module Geo
DB_RETRIEVE_BATCH_SIZE
end
def
lease_key
@lease_key
||=
self
.
class
.
name
.
underscore
end
def
lease_timeout
LEASE_TIMEOUT
end
...
...
@@ -139,33 +136,6 @@ module Geo
scheduled_jobs
.
map
{
|
data
|
data
[
:job_id
]
}
end
def
try_obtain_lease
lease
=
exclusive_lease
.
try_obtain
unless
lease
log_error
(
'Cannot obtain an exclusive lease. There must be another worker already in execution.'
)
return
end
begin
yield
lease
ensure
release_lease
(
lease
)
end
end
def
exclusive_lease
@lease
||=
Gitlab
::
ExclusiveLease
.
new
(
lease_key
,
timeout:
lease_timeout
)
end
def
renew_lease!
exclusive_lease
.
renew
end
def
release_lease
(
uuid
)
Gitlab
::
ExclusiveLease
.
cancel
(
lease_key
,
uuid
)
end
def
current_node
Gitlab
::
Geo
.
current_node
end
...
...
app/workers/geo/repositories_clean_up_worker.rb
View file @
7c72864c
...
...
@@ -3,6 +3,7 @@ module Geo
include
Sidekiq
::
Worker
include
GeoQueue
include
Gitlab
::
ShellAdapter
include
ExclusiveLeaseGuard
BATCH_SIZE
=
250
LEASE_TIMEOUT
=
60
.
minutes
...
...
@@ -40,31 +41,8 @@ module Geo
end
end
def
try_obtain_lease
lease
=
exclusive_lease
.
try_obtain
unless
lease
log_error
(
'Cannot obtain an exclusive lease. There must be another worker already in execution.'
)
return
end
begin
yield
lease
ensure
release_lease
(
lease
)
end
end
def
lease_key
@lease_key
||=
self
.
class
.
name
.
underscore
end
def
exclusive_lease
@lease
||=
Gitlab
::
ExclusiveLease
.
new
(
lease_key
,
timeout:
LEASE_TIMEOUT
)
end
def
release_lease
(
uuid
)
Gitlab
::
ExclusiveLease
.
cancel
(
lease_key
,
uuid
)
def
lease_timeout
LEASE_TIMEOUT
end
def
log_info
(
message
,
params
=
{})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment