Some workers are not loaded in sidekiq job, giving NameError:uninitialized constant

316 Views Asked by At

I am scheduling some background jobs using sidekiq-cron. I am using config/schedule.yml file:

development:
  daily_summary:
    cron: "0 4 * * *"
    class: "DailySummaryWorker"
  weekly_summary:
    cron: "0 3 * * 1"
    class: "WeeklySummaryWorker"
  screenshot:
    cron: "0 5 * * *"
    class: "HomeScreenshotWorker"
staging:
  daily_summary:
    cron: "0 4 * * *"
    class: "DailySummaryWorker"
  weekly_summary:
    cron: "0 3 * * 1"
    class: "WeeklySummaryWorker"
  screenshot:
    cron: "0 5 * * *"
    class: "HomeScreenshotWorker"
  ground_close:
    cron: '30 * * * *'
    class: 'FedexGroundCloseWorker'
production:
  daily_summary:
    cron: "0 4 * * *"
    class: "DailySummaryWorker"
  weekly_summary:
    cron: "0 3 * * 1"
    class: "WeeklySummaryWorker"
  screenshot:
    cron: "0 5 * * *"
    class: "HomeScreenshotWorker"
  cleanup:
    cron: "30 0 * * *"
    class: "LabelCleanupWorker"
  ground_close:
    cron: '30 * * * *'
    class: 'FedexGroundCloseWorker'

and loading the jobs from config/initializers/sidekiq.rb

...
schedule_file = "#{Rails.root}/config/schedule.yml"
Rails.logger.info schedule_file
if File.exist?(schedule_file) 
  Sidekiq::Cron::Job.load_from_hash(YAML.load_file(schedule_file)[Rails.env])
end
...

All job are working fine, except the newly added job i.e ground_close. It is giving the following error:

2023-01-22T08:30:13.564Z 14738 TID-1arc50 WARN: {"retry":3,"queue":"default","class":"FedexGroundCloseWorker","args":[],"jid":"9eaf340c42b913963e3a0b9e","created_at":1674376213.5620716,"enqueued_at":1674376213.5621684}
2023-01-22T08:30:13.564Z 14738 TID-1arc50 WARN: NameError: uninitialized constant FedexGroundCloseWorker
2023-01-22T08:30:13.564Z 14738 TID-1arc50 WARN: /var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.1/lib/active_support/inflector/methods.rb:238:in `const_get'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.1/lib/active_support/inflector/methods.rb:238:in `block in constantize'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.1/lib/active_support/inflector/methods.rb:236:in `each'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.1/lib/active_support/inflector/methods.rb:236:in `inject'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.1/lib/active_support/inflector/methods.rb:236:in `constantize'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.1/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/sidekiq-4.1.2/lib/sidekiq/processor.rb:124:in `process'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/sidekiq-4.1.2/lib/sidekiq/processor.rb:80:in `process_one'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/sidekiq-4.1.2/lib/sidekiq/processor.rb:68:in `run'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/sidekiq-4.1.2/lib/sidekiq/util.rb:17:in `watchdog'
/var/www/[filtered]/shared/bundle/ruby/2.1.0/gems/sidekiq-4.1.2/lib/sidekiq/util.rb:25:in `block in safe_thread'
2023-01-22T08:30:13.564Z 22552 TID-1igvv8 INFO: Cron Jobs - add job with name: ground_close

Or, sometime starts the job but nothing happens

2023-01-23T13:20:19.661Z 53394 TID-12czxw FedexGroundCloseWorker JID-2f008194caf73a92b1a31641 INFO: start
2023-01-23T13:20:19.673Z 53394 TID-1iin2s INFO: Cron Jobs - add job with name: ground_close
2023-01-23T13:20:20.225Z 53394 TID-12czxw FedexGroundCloseWorker JID-2f008194caf73a92b1a31641 INFO: done: 0.564 sec

The job is working fine in staging server. It is not working in production only.

For version information:

  • Ruby: 2.1.2
  • Rails: 4.1.1
  • sidekiq: 4.1.2
  • sidekiq-cron: 0.4.3
1

There are 1 best solutions below

0
Tanveer Ahmad Khan On

It seem like sidekiq-cron require worker classes to be eager loaded. so add following line in config/application.rb

config.eager_load_paths += Dir["#{Rails.root}/app/workers/*"]

make absolutely sure that sidekiq is restarted.

check this thread for more information: https://github.com/rails/rails/issues/13142#issuecomment-275492070