ToddT
ToddT

Reputation: 3258

Getting uninitialized constant from Sidekiq worker

I'm missing something, just not sure what. Sidekiq is up and running fine, I can see it in my terminal.

I have this worker, defined in app/workers/sqs_worker.rb

class SqsWorker
  include Sidekiq::Worker
  require 'aws-sdk'

  def perform
    #do something
  end
end

And then in just a test file at app/test.rb I have the very simple code:

require 'sidekiq'

SqsWorker.new.perform_async

When I run the test.rb file I get this error: uninitialized constant SqsWorker (NameError)

Where did I go astray? I'm running Sidekiq (4.1.4)

I tried killing the running processes and restarting both Sidekiq and Redis to no luck.

Upvotes: 1

Views: 3016

Answers (2)

Bruno Arueira
Bruno Arueira

Reputation: 314

Probably, you ran the test.rb from outside of the scope of the application with something like that:

ruby app/test.rb

But for this purpose, You need to add to your test something like this:

require 'rubygems'
require 'bundler/setup'

require File.expand_path('../config/environment',  __FILE__)

SqsWorker.new.perform_async

And run as this:

bundle exec ruby app/test.rb

Why do you need this? Because nowadays, the bundler manages your dependencies added to your app and therefore, you need to load the rails environment too and the last will load all the things under app/ basically.

Upvotes: 0

Amit Patel
Amit Patel

Reputation: 15985

uninitialized constant SqsWorker (NameError) indicates that your script in test.rb is not able to locate class SqsWorker

All you need to do is replace require 'sidekiq' with require_relative 'workers/sqs_worker' to make your script aware about location of SqsWorker class.

Upvotes: 0

Related Questions