diff options
author | Tiago Botelho <tiagonbotelho@hotmail.com> | 2018-05-03 15:19:21 +0100 |
---|---|---|
committer | Tiago Botelho <tiagonbotelho@hotmail.com> | 2018-05-07 12:00:13 +0200 |
commit | 961255b107370a1350f91d0835cf0e849d237f7d (patch) | |
tree | 74690c9c0df07f86a6acccc5b7ebc51d310fefa2 /app/services/concerns | |
parent | 9a13059332a0c81b3a953f57bb9e40346eba951d (diff) | |
download | gitlab-ce-961255b107370a1350f91d0835cf0e849d237f7d.tar.gz |
Adds remote mirror table migration
Diffstat (limited to 'app/services/concerns')
-rw-r--r-- | app/services/concerns/exclusive_lease_guard.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/app/services/concerns/exclusive_lease_guard.rb b/app/services/concerns/exclusive_lease_guard.rb new file mode 100644 index 00000000000..30be6accc32 --- /dev/null +++ b/app/services/concerns/exclusive_lease_guard.rb @@ -0,0 +1,52 @@ +# +# Concern that helps with getting an exclusive lease for running a block +# of code. +# +# `#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 + + def try_obtain_lease + lease = exclusive_lease.try_obtain + + unless lease + log_error('Cannot obtain an exclusive lease. There must be another instance 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 lease_key + @lease_key ||= self.class.name.underscore + end + + def lease_timeout + raise NotImplementedError, + "#{self.class.name} does not implement #{__method__}" + end + + def release_lease(uuid) + Gitlab::ExclusiveLease.cancel(lease_key, uuid) + end + + def renew_lease! + exclusive_lease.renew + end + + def log_error(message, extra_args = {}) + logger.error(message) + end +end |