Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uninitialized constant sidekiq worker

New rails project.

Rails 5.0.2 Sidekiq 4.2.10

I ran rails g sidekiq:worker deposit_collector and then filled out the code I needed inside the perform method.

To test I logged into the rails console and typed: DepositCollector.perform_async and I get the error:

NameError: uninitialized constant DepositCollectorWorker

The worker is where it should be in the app/workers/ folder. I've used sidekiq on several projects before and have never run into this.

like image 633
Deekor Avatar asked May 05 '17 02:05

Deekor


People also ask

What is uninitialized constant?

The Uninitialized Constant error is a variation of a regular NameError exception class. It has several possible causes. You'll see this error when the code refers to a class or module that it can't find, often because the code doesn't include require, which instructs the Ruby file to load the class.

What is Sidekiq worker?

Sidekiq workers are classes that include Sidekiq::Worker (docs) and represent units of work that may be performed immediately (inline) or may be enqueued to be performed in the background (async).

What is Sidekiq worker in Rails?

Sidekiq allows Rails to launch any task in the background.


2 Answers

By default, Rails will include all subdirectories of the app folder in the autoload paths list. You can review the list of autoload paths in the console with:

puts ActiveSupport::Dependencies.autoload_paths

But Rails only looks for these paths at boot time. So when you add a new folder, like app/workers, it is not enough to restart the Rails console. You need to both exit the Rails console and stop the Spring Application Preloader with:

spring stop

Then start up the Rails console again and the files in the app/workers folder will load properly.

like image 153
cschroed Avatar answered Oct 06 '22 01:10

cschroed


I didn't have spring installed so I took the lazy approach and turned on eager loading in my config/environments/development.rb file:

config.eager_load = true

and then turned it back to false after restarting my server.

like image 44
NemyaNation Avatar answered Oct 06 '22 01:10

NemyaNation