-
Bob Van Landuyt authored
This adds 2 cops for jobs scheduled that will likely miss context information: *CronWorkerContext* Cron-Workers themselves run instance wide, so they aren't scoped to users, namespaces, projects or other resources that should be added to the context. However, they often schedule other jobs that _do_ require context. That is why the there needs to be an indication of context somewhere in the worker. This can be done by using one of the following methods: 1. Wrap the code that schedules jobs in the `with_context` helper: def perform deletion_cutoff = Gitlab::CurrentSettings .deletion_adjourned_period.days.ago.to_date projects = Project.with_route.with_namespace .aimed_for_deletion(deletion_cutoff) projects.find_each(batch_size: 100).with_index do |project, index| delay = index * INTERVAL with_context(project: project) do AdjournedProjectDeletionWorker.perform_in(delay, project.id) end end end 2. Use the a batch scheduling method that provides context def schedule_projects_in_batch(projects) ProjectImportScheduleWorker.bulk_perform_async_with_contexts( projects, arguments_proc: -> (project) { project.id }, context_proc: -> (project) { { project: project } } ) end *BulkPerformWithContext* Often, when scheduling jobs in bulk, these jobs should have a separate context rather than the overarching context. If that is the case, `bulk_perform_async` can be replaced by the `bulk_perform_async_with_context` helper, and instead of `bulk_perform_in` use `bulk_perform_in_with_context`. For example: ProjectImportScheduleWorker.bulk_perform_async_with_contexts( projects, arguments_proc: -> (project) { project.id }, context_proc: -> (project) { { project: project } } ) Each object from the array in the first argument is yielded into 2 blocks: The `arguments_proc` which needs to the return the list of arguments the job needs to be scheduled with. The `context_proc` which needs to return a hash with the context information for the job. When providing objects for a context, please make sure to load the relevant relations. For routables (namespace, project), use `.with_route`, for projects also include the namespace using `.with_namespace`.
b5b41f1b