delayed_job.rb 1.1 KB

123456789101112131415161718192021222324252627
  1. Delayed::Worker.destroy_failed_jobs = false
  2. Delayed::Worker.max_attempts = 5
  3. Delayed::Worker.max_run_time = (ENV['DELAYED_JOB_MAX_RUNTIME'].presence || 2).to_i.minutes
  4. Delayed::Worker.read_ahead = 5
  5. Delayed::Worker.default_priority = 10
  6. Delayed::Worker.delay_jobs = !Rails.env.test?
  7. Delayed::Worker.sleep_delay = (ENV['DELAYED_JOB_SLEEP_DELAY'].presence || 10).to_f
  8. Delayed::Worker.logger = Rails.logger
  9. # Delayed::Worker.logger = Logger.new(Rails.root.join('log', 'delayed_job.log'))
  10. # Delayed::Worker.logger.level = Logger::DEBUG
  11. ActiveSupport.on_load(:delayed_job_active_record) do
  12. class Delayed::Job
  13. scope :pending, -> { where("locked_at IS NULL AND attempts = 0") }
  14. scope :awaiting_retry, -> { where("failed_at IS NULL AND attempts > 0 AND locked_at IS NULL") }
  15. scope :failed_jobs, -> { where("failed_at IS NOT NULL") }
  16. end
  17. database_deadlocks_when_using_optimized_strategy = lambda do
  18. ENV["DATABASE_ADAPTER"] == "mysql2"
  19. end
  20. Delayed::Backend::ActiveRecord.configure do |config|
  21. config.reserve_sql_strategy = :default_sql
  22. end if database_deadlocks_when_using_optimized_strategy.call
  23. end