Skip to content

Proposal: Create cop to avoid using find() of job arguments in sidekiq

Context

Calling Model.find(id) raises ActiveRecord::RecordNotFound exception when id does not exist in database. If we have a sidekiq worker with id argument(s) and calling .find() raises the exception which would result in the job being failed and getting retried for 25 times if a retry value was not set. The job would fail even on retry, this would take a toll on the error budget.

Some workers handle this by rescuing exception.

Even though we have mentioned in our docs:

# Bad
def perform(project_id)
  project = Project.find(project_id)
  # ...
end

# Good
def perform(project_id)
  project = Project.find_by_id(project_id)
  return unless project
  # ...
end

Some workers still use .find(). One such case happened with Security::SyncScanPoliciesWorker where we have .find() and the failures of retried jobs are taking a toll on our error budget. We also seem to have a few of other workers with the same problem

Proposal

Create a cop to check if the worker's perform method calls .find() on any of the job arguments.