Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-07 18:09:30 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-07 18:09:30 +0300
commitc6b3ec3f56fa32a0e0ed3de0d0878d25f1adaddf (patch)
tree967afee9a510ff9dd503ebd83706dc760ec2e3ed /app/models/concerns/update_highest_role.rb
parent903ccf7c93eb9490c76857bffe744249cc07de09 (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.rb38
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