diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-07 18:09:30 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-07 18:09:30 +0300 |
commit | c6b3ec3f56fa32a0e0ed3de0d0878d25f1adaddf (patch) | |
tree | 967afee9a510ff9dd503ebd83706dc760ec2e3ed /app/models/concerns/update_highest_role.rb | |
parent | 903ccf7c93eb9490c76857bffe744249cc07de09 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/concerns/update_highest_role.rb')
-rw-r--r-- | app/models/concerns/update_highest_role.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/app/models/concerns/update_highest_role.rb b/app/models/concerns/update_highest_role.rb new file mode 100644 index 00000000000..7efc436c6c8 --- /dev/null +++ b/app/models/concerns/update_highest_role.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module UpdateHighestRole + extend ActiveSupport::Concern + + HIGHEST_ROLE_LEASE_TIMEOUT = 10.minutes.to_i + HIGHEST_ROLE_JOB_DELAY = 10.minutes + + included do + after_commit :update_highest_role + end + + private + + # Schedule a Sidekiq job to update the highest role for a User + # + # The job will be called outside of a transaction in order to ensure the changes + # to be commited before attempting to update the highest role. + # The exlusive lease will not be released after completion to prevent multiple jobs + # being executed during the defined timeout. + def update_highest_role + return unless update_highest_role? + + run_after_commit_or_now do + lease_key = "update_highest_role:#{update_highest_role_attribute}" + lease = Gitlab::ExclusiveLease.new(lease_key, timeout: HIGHEST_ROLE_LEASE_TIMEOUT) + + if lease.try_obtain + UpdateHighestRoleWorker.perform_in(HIGHEST_ROLE_JOB_DELAY, update_highest_role_attribute) + else + # use same logging as ExclusiveLeaseGuard + # rubocop:disable Gitlab/RailsLogger + Rails.logger.error('Cannot obtain an exclusive lease. There must be another instance already in execution.') + # rubocop:enable Gitlab/RailsLogger + end + end + end +end |