Commit 2ebb0be6 authored by Bob Van Landuyt's avatar Bob Van Landuyt Committed by Marcia Ramos

Update the sidekiq-styleguide based on feedback

Addresses the feedback from https://gitlab.com/gitlab-org/gitlab/-/merge_requests/23995
parent a9895a77
...@@ -401,6 +401,8 @@ default weight, which is 1. ...@@ -401,6 +401,8 @@ default weight, which is 1.
## Worker context ## Worker context
> - [Introduced](https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/9) in GitLab 12.8.
To have some more information about workers in the logs, we add To have some more information about workers in the logs, we add
[metadata to the jobs in the form of an [metadata to the jobs in the form of an
`ApplicationContext`](logging.md#logging-context-metadata-through-rails-or-grape-requests). `ApplicationContext`](logging.md#logging-context-metadata-through-rails-or-grape-requests).
...@@ -417,27 +419,27 @@ need to do anything. ...@@ -417,27 +419,27 @@ need to do anything.
There are however some instances when there would be no context There are however some instances when there would be no context
present when the job is scheduled, or the context that is present is present when the job is scheduled, or the context that is present is
likely to be incorrect. For these instances we've added rubocop-rules likely to be incorrect. For these instances, we've added Rubocop rules
to draw attention and avoid incorrect metadata in our logs. to draw attention and avoid incorrect metadata in our logs.
As with most our cops, there are perfectly valid reasons for disabling As with most our cops, there are perfectly valid reasons for disabling
them. In this case it could be that the context from the request is them. In this case it could be that the context from the request is
correct. Or maybe you've specified a context already in a way that correct. Or maybe you've specified a context already in a way that
isn't picked up by the cops. In any case, please leave a code-comment isn't picked up by the cops. In any case, leave a code comment
pointing to which context will be used when disabling the cops. pointing to which context will be used when disabling the cops.
When you do provide objects to the context, please make sure that the When you do provide objects to the context, make sure that the
route for namespaces and projects is pre-loaded. This can be done using route for namespaces and projects is pre-loaded. This can be done by using
the `.with_route` scope defined on all `Routable`s. the `.with_route` scope defined on all `Routable`s.
### Cron-Workers ### Cron workers
The context is automatically cleared for workers in the cronjob-queue The context is automatically cleared for workers in the Cronjob queue
(which `include CronjobQueue`), even when scheduling them from (`include CronjobQueue`), even when scheduling them from
requests. We do this to avoid incorrect metadata when other jobs are requests. We do this to avoid incorrect metadata when other jobs are
scheduled from the cron-worker. scheduled from the cron worker.
Cron-Workers themselves run instance wide, so they aren't scoped to Cron workers themselves run instance wide, so they aren't scoped to
users, namespaces, projects, or other resources that should be added to users, namespaces, projects, or other resources that should be added to
the context. the context.
...@@ -449,46 +451,46 @@ somewhere within the worker: ...@@ -449,46 +451,46 @@ somewhere within the worker:
1. Wrap the code that schedules jobs in the `with_context` helper: 1. Wrap the code that schedules jobs in the `with_context` helper:
```ruby ```ruby
def perform def perform
deletion_cutoff = Gitlab::CurrentSettings deletion_cutoff = Gitlab::CurrentSettings
.deletion_adjourned_period.days.ago.to_date .deletion_adjourned_period.days.ago.to_date
projects = Project.with_route.with_namespace projects = Project.with_route.with_namespace
.aimed_for_deletion(deletion_cutoff) .aimed_for_deletion(deletion_cutoff)
projects.find_each(batch_size: 100).with_index do |project, index| projects.find_each(batch_size: 100).with_index do |project, index|
delay = index * INTERVAL delay = index * INTERVAL
with_context(project: project) do with_context(project: project) do
AdjournedProjectDeletionWorker.perform_in(delay, project.id) AdjournedProjectDeletionWorker.perform_in(delay, project.id)
end end
end end
end end
``` ```
1. Use the a batch scheduling method that provides context: 1. Use the a batch scheduling method that provides context:
```ruby ```ruby
def schedule_projects_in_batch(projects) def schedule_projects_in_batch(projects)
ProjectImportScheduleWorker.bulk_perform_async_with_contexts( ProjectImportScheduleWorker.bulk_perform_async_with_contexts(
projects, projects,
arguments_proc: -> (project) { project.id }, arguments_proc: -> (project) { project.id },
context_proc: -> (project) { { project: project } } context_proc: -> (project) { { project: project } }
) )
end end
``` ```
or when scheduling with delays: Or, when scheduling with delays:
```ruby ```ruby
diffs.each_batch(of: BATCH_SIZE) do |diffs, index| diffs.each_batch(of: BATCH_SIZE) do |diffs, index|
DeleteDiffFilesWorker DeleteDiffFilesWorker
.bulk_perform_in_with_contexts(index * 5.minutes, .bulk_perform_in_with_contexts(index * 5.minutes,
diffs, diffs,
arguments_proc: -> (diff) { diff.id }, arguments_proc: -> (diff) { diff.id },
context_proc: -> (diff) { { project: diff.merge_request.target_project } }) context_proc: -> (diff) { { project: diff.merge_request.target_project } })
end end
``` ```
### Jobs scheduled in bulk ### Jobs scheduled in bulk
...@@ -512,11 +514,11 @@ For example: ...@@ -512,11 +514,11 @@ For example:
Each object from the enumerable in the first argument is yielded into 2 Each object from the enumerable in the first argument is yielded into 2
blocks: blocks:
The `arguments_proc` which needs to return the list of arguments the - The `arguments_proc` which needs to return the list of arguments the
job needs to be scheduled with. job needs to be scheduled with.
The `context_proc` which needs to return a hash with the context - The `context_proc` which needs to return a hash with the context
information for the job. information for the job.
## Arguments logging ## Arguments logging
......
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