Search code examples
ruby-on-rails-3sidekiq

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.


Solution

  • 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)