Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sidekiq error Uninitialized Constant

I'm having an issue with sidekiq. Basically we're getting NameError: uninitialized constant on our sidekiq setup which is causing a large number of jobs to fail.

The error log says:

NameError: uninitialized constant GameUser::Lock
/data/@myapp/releases/20130321230952/app/models/game_user.rb:71:in `node_calls_base_get_user'
/data/@myapp/shared/bundled_gems/ruby/1.9.1/gems/sidekiq-2.8.0/lib/sidekiq/processor.rb:45:in `block (3 levels) in process'
/data/@myapp/shared/bundled_gems/ruby/1.9.1/gems/sidekiq-2.8.0/lib/sidekiq/middleware/chain.rb:109:in `call'

The code is here:

# app/models/game_user.rb
def self.node_calls_base_get_user(serial, game_name)
  if Lock.get("user:#{id}") # Set up lock to prevent multiple users to be created      
    Lock.delete("user:#{id}")
  end
  return false
end

Lock is defined in a library:

# lib/lock.rb
class Lock
  def self.get(key)
    lock = CACHE.add("lock:#{key}", 1, 5) # Let lock autoexpire after 5 seconds
    return true
  end
end

And the lib/ folder is automatically loaded with the rest of the configurations.

module Myapp
  class Application < Rails::Application
    ...
    # Custom directories with classes and modules you want to be autoloadable.
    config.autoload_paths += %W(#{config.root}/lib)
    ...    
  end
end

I have no idea why this is happening. It seems to happen more frequently when we deploy, but it seems to happen often enough otherwise.

I've been following the following thread: https://github.com/mperham/sidekiq/issues/331 but it doesn't seem to offer a solution besides adding the lib folder to the autoload_paths.

I'm using:

gem 'rails', '3.2.13' gem 'sidekiq', '>= 2.7.5'

Any help would be greatly apreciated.

like image 262
roloenusa Avatar asked Mar 22 '13 00:03

roloenusa


People also ask

How do I know if I have Sidekiq?

To test your Sidekiq Worker jobs array, run WorkerNameHere.jobs in terminal and see if it contains your job with your jid. If it does, then it was enqueued in Sidekiq to be run as a job.

What is Sidekiq thread?

Sidekiq handles concurrency by using multiple threads in its process. This way, it can process multiple jobs at once, each thread processing one job at a time. By default, Sidekiq uses 10 threads per process. You can configure it to use more threads, thus increasing concurrency.

What does Sidekiq store in Redis?

Sidekiq is one of the more widely used background job frameworks that you can implement in a Rails application. It is backed by Redis, an in-memory key-value store known for its flexibility and performance. Sidekiq uses Redis as a job management store to process thousands of jobs per second.


1 Answers

Add the lib folder to the eager_load_paths. This is different to the autoload_paths. Sidekiq won't load classes on the fly, which is why you get errors when you try to access a constant that wasn't eagerly loaded.

So in your application.rb

config.autoload_paths += %W(#{config.root}/lib)
config.eager_load_paths += %W(#{config.root}/lib)
like image 138
Chris Aitchison Avatar answered Nov 03 '22 00:11

Chris Aitchison