Commit 7c72864c authored by Toon Claes's avatar Toon Claes

Extract try_obtain_lease to a concern

parent 370fb882
#
# 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 module ExclusiveLeaseGuard
extend ActiveSupport::Concern extend ActiveSupport::Concern
# override in subclass
def lease_timeout
raise NotImplementedError
end
def lease_key def lease_key
@lease_key ||= self.class.name.underscore @lease_key ||= self.class.name.underscore
end end
def log_error(message, extra_args = {})
logger.error(messages)
end
def try_obtain_lease def try_obtain_lease
lease = exclusive_lease.try_obtain lease = exclusive_lease.try_obtain
...@@ -27,4 +43,8 @@ module ExclusiveLeaseGuard ...@@ -27,4 +43,8 @@ module ExclusiveLeaseGuard
def release_lease(uuid) def release_lease(uuid)
Gitlab::ExclusiveLease.cancel(lease_key, uuid) Gitlab::ExclusiveLease.cancel(lease_key, uuid)
end end
def renew_lease!
exclusive_lease.renew
end
end end
...@@ -2,6 +2,7 @@ module Geo ...@@ -2,6 +2,7 @@ module Geo
class BaseSchedulerWorker class BaseSchedulerWorker
include Sidekiq::Worker include Sidekiq::Worker
include CronjobQueue include CronjobQueue
include ExclusiveLeaseGuard
DB_RETRIEVE_BATCH_SIZE = 1000 DB_RETRIEVE_BATCH_SIZE = 1000
LEASE_TIMEOUT = 60.minutes LEASE_TIMEOUT = 60.minutes
...@@ -72,10 +73,6 @@ module Geo ...@@ -72,10 +73,6 @@ module Geo
DB_RETRIEVE_BATCH_SIZE DB_RETRIEVE_BATCH_SIZE
end end
def lease_key
@lease_key ||= self.class.name.underscore
end
def lease_timeout def lease_timeout
LEASE_TIMEOUT LEASE_TIMEOUT
end end
...@@ -139,33 +136,6 @@ module Geo ...@@ -139,33 +136,6 @@ module Geo
scheduled_jobs.map { |data| data[:job_id] } scheduled_jobs.map { |data| data[:job_id] }
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 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 def current_node
Gitlab::Geo.current_node Gitlab::Geo.current_node
end end
......
...@@ -3,6 +3,7 @@ module Geo ...@@ -3,6 +3,7 @@ module Geo
include Sidekiq::Worker include Sidekiq::Worker
include GeoQueue include GeoQueue
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
include ExclusiveLeaseGuard
BATCH_SIZE = 250 BATCH_SIZE = 250
LEASE_TIMEOUT = 60.minutes LEASE_TIMEOUT = 60.minutes
...@@ -40,31 +41,8 @@ module Geo ...@@ -40,31 +41,8 @@ module Geo
end end
end end
def try_obtain_lease def lease_timeout
lease = exclusive_lease.try_obtain LEASE_TIMEOUT
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)
end end
def log_info(message, params = {}) def log_info(message, params = {})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment