Sidekiq: Uninitialized constant error

Created on 8 Apr 2018  路  3Comments  路  Source: mperham/sidekiq

Ruby version: ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux]
Sidekiq / Pro / Enterprise version(s): sidekiq-5.1.0 + sidekiq-scheduler-2.2.1

Rails version: 5.1.4

  • config/initializers/sidekiq.yml:
Sidekiq.configure_client do |config|
  require 'sidekiq-scheduler/web'
  config.redis = { url: Rails.configuration.redis_url }
end

Sidekiq.configure_server do |config|
  config.redis = { url: Rails.configuration.redis_url }
end
  • config/environments/production.rb:
  config.cache_classes = true
  config.eager_load = true
  # ...
  • config/application.rb:
    config.eager_load_paths << "#{Rails.root}/app/workers"
    config.autoload_paths << "#{Rails.root}/app/workers"
    # ...
  • config/sidekiq.yml:
:concurrency: 1
:schedule:
  email_users:
    every: 1m
    class: EmailUsersWorker
  • app/workers/email_users_worker.rb:
class EmailUsersWorker
  include Sidekiq::Worker

  def perform
    # ...
  end
end
  • command (from rails root dir):
$ bundle exec sidekiq -e production
  • Problem is clearly visible in the logs:
queueing EmailUsersWorker (email_users)
enqueued retry: {"class":"EmailUsersWorker","queue":"default","args":[],"retry":true,"jid":"830039c41b88444683a7fd5d","created_at":1523192889.904502,"enqueued_at":1523192889.904532,"error_message":"uninitialized constant EmailUsersWorker","error_class":"NameError","failed_at":1523192889.9115372,"retry_count":0}

Very occasionally, the worker is invoked correctly without errors. Probably less than 1% of the time, looks like a race condition.

What I tried so far, without success:

  • explicit require <rails_root>/app/workers/email_users_worker before app initialization in config/application.rb
  • Rails.logger.info("Class: #{EmailUsersWorker}") in config/application.rb logs the expected line when sidekiq is started. I.e. the constant is defined at that time.
  • running sidekiq with -e development (where cache_classes and eager_load are both false)

Please help me resolve this issue, I would gladly cooperate with any required information.

Most helpful comment

If anyone comes across this and none of the other answers on the internet help you:

  • Do NOT change the autoload or eager load paths
  • Check the name of the worker file. Ensure you formated it like: sidekiq_worker_file_name.rb not SidekiqWorkerFileName.rb

All 3 comments

Thank you for your cooperation. According to these docs, everything would have worked without modifying the load paths, but, sadly, it does not. Do you have any specific suggestion you could make?

Thank you in advance.

If anyone comes across this and none of the other answers on the internet help you:

  • Do NOT change the autoload or eager load paths
  • Check the name of the worker file. Ensure you formated it like: sidekiq_worker_file_name.rb not SidekiqWorkerFileName.rb
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mperham picture mperham  路  3Comments

fatcatt316 picture fatcatt316  路  4Comments

bartimaeus picture bartimaeus  路  3Comments

jlecour picture jlecour  路  4Comments

michaeldiscala picture michaeldiscala  路  4Comments