A RuboCop extension focused on enforcing Sidekiq best practices and coding conventions.
Add this line to your application's Gemfile:
gem 'rubocop-sidekiq_plus', require: falseAnd then execute:
bundle installAdd the following to your .rubocop.yml:
plugins:
- rubocop-sidekiqDo not pass ActiveRecord objects to Sidekiq jobs. Pass the id and fetch the record in the job instead.
Do not process large datasets within a single Sidekiq job. Split work into smaller jobs instead.
Enforce consistent job class name suffix (Job or Worker).
Use a constant class name for Sidekiq jobs. Dynamic job class names are harder to trace and may be insecure.
Avoid using ActiveRecord::Base.connection directly in jobs. Use connection_pool.with_connection.
Do not pass Date/Time objects to Sidekiq jobs. Convert to a string or timestamp first.
Detect deprecated Sidekiq.default_worker_options usage. Use Sidekiq.default_job_options instead.
Avoid using the deprecated delay extension. Prefer deliver_later or enqueue a Sidekiq job.
Avoid Sidekiq::Worker and use Sidekiq::Job instead.
Detect excessive retry counts in sidekiq_options.
Avoid passing large arguments to Sidekiq jobs. Pass IDs and load records in the job instead.
Avoid calling perform_async inside loops. Use perform_bulk instead.
Avoid implicit job dependencies by enqueuing jobs from other jobs.
Ensure job classes are located under app/jobs or app/workers.
Ensure job file names match the class name.
Prefer including Sidekiq::Job over Sidekiq::Worker. Configurable with PreferredModule option.
Encourage logging in job perform methods. Disabled by default.
Ensure network calls in jobs have explicit timeouts configured.
Avoid mixing ActiveJob retry_on with Sidekiq retry options.
Avoid rescuing all exceptions in Sidekiq jobs. Rescue specific exceptions and consider re-raising.
Avoid perform_async in tests. Disabled by default.
Avoid using perform_inline in production code. Use perform_async instead.
Do not use keyword arguments in the perform method. Sidekiq cannot serialize keyword arguments to JSON.
Avoid passing PII in job arguments. Disabled by default.
Prefer Sidekiq::Job over ActiveJob. Disabled by default.
Require explicit queue specification for jobs. Disabled by default.
Use Sidekiq.redis instead of creating new Redis connections in jobs.
Prefer retry: false over retry: 0 for clarity. Disabled by default.
Require explicit retry configuration for jobs. Disabled by default.
Avoid self-scheduling jobs. Disabled by default.
Avoid passing sensitive data in job arguments.
Avoid silently swallowing exceptions in jobs.
Do not use sleep in Sidekiq jobs. It blocks the worker thread.
Do not pass symbols to Sidekiq jobs. Use strings instead.
Do not create threads inside Sidekiq jobs. Use separate jobs or Sidekiq's built-in concurrency.
Do not enqueue Sidekiq jobs inside database transactions. The job may run before the transaction commits.
Detect unknown or unsupported keys in sidekiq_options.
Use logger instead of puts/print in jobs.
The following cops are available for Sidekiq Pro users:
Ensure batch callback methods are named correctly (on_complete, on_success, on_death).
Check that jobs enqueued in batch callbacks have retry enabled for reliability.
Discourage polling batch status in loops. Use batch callbacks instead. Disabled by default.
Recommend registering callbacks or descriptions for batches for tracking. Disabled by default.
Detect batch.jobs blocks that may be empty. Empty batches cause errors in Sidekiq Pro versions before 7.1.
Validate TTL range for expiring jobs. Too short TTL may cause jobs to expire before processing. Disabled by default.
Avoid passing large arguments to jobs within a batch. This can exhaust Redis memory when many jobs are enqueued simultaneously.
Ensure nested batches reference their parent batch for proper tracking.
Recommend enabling super_fetch! and reliable_push! for production reliability. Disabled by default.
The following cops are available for Sidekiq Enterprise users:
Ensure sensitive data is consolidated in the last argument when using encryption. Sidekiq Enterprise only encrypts the last argument.
Recommend proper secret bag usage with encryption. Warns when encrypted jobs have only ID-like arguments. Disabled by default.
Warn about long-running operations in Sidekiq.leader? checks. Prefer delegating work to jobs. Disabled by default.
Create rate limiters as class constants for reuse. Creating limiters inside the perform method causes Redis memory leaks.
Recommend setting lock_timeout for concurrent limiters to match job execution time.
Specify wait_timeout option for rate limiters to avoid blocking worker threads indefinitely.
Validate cron expressions for periodic jobs.
Periodic jobs should not require arguments. Use optional arguments or the args option in periodic registration.
Warn about unique jobs with too short TTL that may expire during retries.
Require unique_for option when using unique_until. Without a TTL, uniqueness locks may persist indefinitely if jobs fail.
Validate unique_until option values. Avoid unique_until: :start which may cause concurrent execution.
All cops are enabled by default except for:
Sidekiq/QueueSpecifiedSidekiq/RetrySpecifiedSidekiq/ConsistentJobSuffixSidekiq/ExcessiveRetrySidekiq/JobDependencySidekiq/JobFileLocationSidekiq/JobFileNamingSidekiq/MissingLoggingSidekiq/AsyncInTestSidekiq/PiiInArgumentsSidekiq/SidekiqOverActiveJobSidekiq/RetryZeroSidekiq/SelfSchedulingJobSidekiqPro/BatchStatusPollingSidekiqPro/BatchWithoutCallbackSidekiqPro/ExpiringJobWithoutTTLSidekiqPro/ReliabilityNotEnabledSidekiqEnt/EncryptionWithoutSecretBagSidekiqEnt/LeaderElectionWithoutBlock
Example configuration:
Sidekiq/JobInclude:
PreferredModule: Job # or Worker
Sidekiq/PerformInlineUsage:
AllowedInTests: true
Sidekiq/QueueSpecified:
Enabled: true
Sidekiq/RetrySpecified:
Enabled: trueAfter checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests.
Bug reports and pull requests are welcome on GitHub at https://github.com/ydah/rubocop-sidekiq_plus.
The gem is available as open source under the terms of the MIT License.