Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError uninitialized constant MyWorker #3779

Closed
AshvinChauhan opened this issue Feb 27, 2018 · 20 comments
Closed

NameError uninitialized constant MyWorker #3779

AshvinChauhan opened this issue Feb 27, 2018 · 20 comments

Comments

@AshvinChauhan
Copy link

AshvinChauhan commented Feb 27, 2018

Rails version: 5.1.4
Ruby version: 2.5.0
Sidekiq / Pro / Enterprise version(s): 5.1.0

sidekiq.yml

---
:concurrency: 25
:pidfile: ./tmp/pids/sidekiq.pid
:logfile: ./log/sidekiq.log
:queues:
 - default
 - mailers

sidekiq.rb

Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://localhost:6379/0' }
end

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://localhost:6379/0' }
end

Error
NameError uninitialized constant MyWorker
Every time I am getting this error, and after some retries its working fine. and in development everything is working fine, but in production its not.
I also try to add it in application.rb as following
config.autoload_paths += Dir[Rails.root.join('app', 'workers')]

@mikegee
Copy link
Contributor

mikegee commented Feb 27, 2018

The advice from this blog post in 2014 has solved this kind of problem for me.

@mperham
Copy link
Collaborator

mperham commented Feb 27, 2018

@mperham mperham closed this as completed Feb 27, 2018
@paneq
Copy link

paneq commented Feb 28, 2018

@mikegee I am happy my blog post was helpful to you. just remember that what you were trying to do was probably unnecessary https://blog.arkency.com/2014/11/dont-forget-about-eager-load-when-extending-autoload/#one_more_thing and if you have app/workers/one/my_worker.rb then the convention is to name it One::MyWorker and not MyWorker for autoloading to work.

@mikegee
Copy link
Contributor

mikegee commented Feb 28, 2018

Thanks @paneq! I’ve used your advice to add lib to the path in about a dozen apps since you posted that.

@AshvinChauhan
Copy link
Author

Solution is not working for me, my worker is in app folder, and I am not added it in autoloading or eagerloading still its showing NameError uninitialized constant MyWorker, and after retry its working, I mean in first try its not working, it always running after retry.

@paneq
Copy link

paneq commented Mar 1, 2018

@AshvinChauhan what is the full path to the file (app/my_worker.rb ?) and full class name (MyWorker without namespaces?)

@AshvinChauhan
Copy link
Author

full path is app/workers/my_worker.rb

and my)worker.rb containing following

class MyWorker
  include Sidekiq::Worker

  def perform(args*)
    # My stuff
  end
end

@AshvinChauhan
Copy link
Author

@mperham can you please reopen the issue, because my issue is not resolved yet.

@mperham
Copy link
Collaborator

mperham commented Mar 6, 2018 via email

@AshvinChauhan
Copy link
Author

Issue is only in rails 5, its working proper in older version of rails.

@AshvinChauhan
Copy link
Author

Any update?

@mperham
Copy link
Collaborator

mperham commented Mar 9, 2018 via email

@AshvinChauhan
Copy link
Author

Okay will provide you soon

@mozillo
Copy link

mozillo commented Dec 17, 2018

In rails 5 production mode got same issue.

@nflorentin
Copy link

nflorentin commented Feb 6, 2019

In rails 5, same problem here in development with a simple mailer in app/mailers. Sometimes email is sent, sometimes after one or various retries. Always the same error "uninitialized constant ContactMailer" but randomly occurs. Sidekiq 5.2.5, redis 4.1.0, rails 5.2.2, ruby 2.5.3. Never had this problem before.

@ritesh2741
Copy link

ritesh2741 commented May 10, 2019

I am having exact same issue, Sending text messages using it , the order of the gets messed up because of the uninitialized constant error. Sidekiq 5.2.6, Rails 5.1.6, Ruby 2.5.3, Redis 4.0. Works after one or couple retries.
@mperham or @AshvinChauhan Can you please share some thoughts ?

### UPDATE: I figured out the issue, i was using same redis server for multiple instances of the app causing sidekiq confusion of where to look in redis.

#2834 (comment)

this helped me which suggests to use namespace in case same redis db is used.

@ceimox
Copy link

ceimox commented Jun 19, 2020

Oh Man this was really hard to figure out for me, it has nothing to do with the eager_load or autoload paths. If you are using puma with many workers then you need to call these below the workers number in your config/puma.rb

workers 2
preload_app! #used when workers are more than 0

@Mirk32
Copy link

Mirk32 commented Aug 10, 2020

In my case, I just restarted Sidekiq on production and it fixed a problem: sudo service sidekiq restart

@sunwooz
Copy link

sunwooz commented Nov 30, 2022

Restarting the sidekiq service worked for me as well

@HeppokoNeet
Copy link

Restarting the sidekiq service worked for me as well

I solved too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests