You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Do not use Kernel.sleep in jobs. sleep blocks the worker thread, and it’s not able to process other jobs. Re-schedule the job for a later time, or use limiters with a custom exception.
# badclassSomeJob < ApplicationJobdefperform(user)attempts_number=3ThirdParty::Api::User.renew(user.external_id)rescueThirdParty::Api::Errors::TooManyRequestsError=>errorsleep(error.retry_after)attempts_number -= 1retryunlessattempts_number.zero?raiseendend# good - retry job in a while, a limited number of timesclassSomeJob < ApplicationJobsidekiq_optionsretry: 3sidekiq_retry_indo |count,exception|
caseexceptionwhenThirdParty::Api::Errors::TooManyRequestsErrorcount + 1# i.e. 1s, 2s, 3sendenddefperform(user)ThirdParty::Api::User.renew(user.external_id)endend# good - fine-grained control of API usage in jobsclassSomeJob < ApplicationJobdefperform(user)LIMITER.within_limitdoThirdParty::Api::User.renew(user.external_id)endendend# config/initializers/sidekiq.rbSidekiq::Limiter.configuredo |config|
config.errors << ThirdParty::Api::Errors::TooManyRequestsErrorend
The text was updated successfully, but these errors were encountered:
Reference: https://github.com/toptal/active-job-style-guide#sleep
Do not use Kernel.sleep in jobs. sleep blocks the worker thread, and it’s not able to process other jobs. Re-schedule the job for a later time, or use limiters with a custom exception.
The text was updated successfully, but these errors were encountered: