Make Current.organization available in Sidekiq workers
What does this MR do and why?
When Sidekiq workers are scheduled, the value of Current.organization is being passed to the worker. This MR will use that value to set Current.organization. The Gitlab::ApplicationContext` is used for this.
When an Organization is not found, the job will be cancelled with Sidekiq::JobRetry::Skip. There is no point processing a job for an organization that has been deleted (or moved to another cell).
When Gitlab::ApplicationContext does not have an organization_id, Current.organization will not be set.
References
How to set up and validate locally
No worker is using Current.organization yet, so we need to modify an existing worker for testing purposes. So we modify the perform method of a random worker. For example StageUpdateWorker:
Change the perform method of app/workers/stage_update_worker.rb to:
def perform(stage_id)
Rails.logger.debug("Current Org: #{Current.organization.path}")
Ci::Stage.find_by_id(stage_id)&.update_legacy_status
end
Restart sidekiq and tail the development log: gdk restart rails-background-jobs && tail -f log/development.log | grep Current
Now, in another terminal , start a rails console using gdk rails c:
- Create a new Organization
my_org = Organizations::Organization.find_or_create_by!(path: 'my-org') { |org| org.name = 'My Org' }
- Set Rails console
Current.organization:Current.organization = my_org
- Start a new background job:
StageUpdateWorker.perform_async(rand(1..1000))
In the terminal that has development log open, you should see debug messages like:
Current Org: my-org
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.