Commit 83fa8fbb authored by Bob Van Landuyt's avatar Bob Van Landuyt

Mark the AuthorizedProjectsWorker as idempotent

The AuthorizedProjectsWorker is a wrapper around the
`Users::RefreshAuthorizedProjectsService`.

The service itself prevents 2 jobs from running at the same time using
an exclusive lease. And the job itself creates and removes any project
authorizations by recalculating all the authorizations for the given
user using `Gitlab::ProjectAuthorizations`.

Doing this twice will mean the last execution has no effect.
parent 9b28dc85
...@@ -856,7 +856,7 @@ ...@@ -856,7 +856,7 @@
:urgency: :high :urgency: :high
:resource_boundary: :unknown :resource_boundary: :unknown
:weight: 2 :weight: 2
:idempotent: :idempotent: true
- :name: background_migration - :name: background_migration
:feature_category: :not_owned :feature_category: :not_owned
:has_external_dependencies: :has_external_dependencies:
......
# frozen_string_literal: true # frozen_string_literal: true
class AuthorizedProjectsWorker # rubocop:disable Scalability/IdempotentWorker class AuthorizedProjectsWorker
include ApplicationWorker include ApplicationWorker
prepend WaitableWorker prepend WaitableWorker
...@@ -8,6 +8,8 @@ class AuthorizedProjectsWorker # rubocop:disable Scalability/IdempotentWorker ...@@ -8,6 +8,8 @@ class AuthorizedProjectsWorker # rubocop:disable Scalability/IdempotentWorker
urgency :high urgency :high
weight 2 weight 2
idempotent!
# This is a workaround for a Ruby 2.3.7 bug. rspec-mocks cannot restore the # This is a workaround for a Ruby 2.3.7 bug. rspec-mocks cannot restore the
# visibility of prepended modules. See https://github.com/rspec/rspec-mocks/issues/1231 # visibility of prepended modules. See https://github.com/rspec/rspec-mocks/issues/1231
# for more details. # for more details.
......
...@@ -21,5 +21,22 @@ describe AuthorizedProjectsWorker do ...@@ -21,5 +21,22 @@ describe AuthorizedProjectsWorker do
job.perform(-1) job.perform(-1)
end end
end end
it_behaves_like "an idempotent worker" do
let(:job_args) { user.id }
it "does not change authorizations when run twice" do
group = create(:group)
create(:project, namespace: group)
group.add_developer(user)
# Delete the authorization created by the after save hook of the member
# created above.
user.project_authorizations.delete_all
expect { job.perform(user.id) }.to change { user.project_authorizations.reload.size }.by(1)
expect { job.perform(user.id) }.not_to change { user.project_authorizations.reload.size }
end
end
end end
end end
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