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 {class} #2084

Closed
fertapric opened this issue Dec 10, 2014 · 5 comments
Closed

NameError: uninitialized constant {class} #2084

fertapric opened this issue Dec 10, 2014 · 5 comments

Comments

@fertapric
Copy link

Hi all!

I've been fighting with NameError: uninitialized constant {class} lately at my company. It seems to be related to Why doesn't Sidekiq autoload my Rails application code?, but including the lib dir in config.eager_load_paths and/or setting config.eager_load = true didn't solve the issue.

I even try to require directly the class in config/application.rb, with no success. I've checked if the naming of the code/files was not following the proper naming convention, but it seems everything is right.

These are examples of the files/classes involved:

Gemfile

source "https://this.is.sidekiq.pro.url.com"
source 'https://rubygems.org'

gem 'rails', '3.2.20'

...

gem 'sidekiq-pro'
gem 'sidekiq-failures', '~> 0.4'
gem 'sidekiq-unique-jobs', '~> 3.0'

...

config/application.rb

require File.expand_path('../boot', __FILE__)

require 'active_record/railtie'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'active_resource/railtie'
require 'sprockets/railtie'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  # Bundler.require(*Rails.groups(assets: %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  Bundler.require(:default, :assets, Rails.env)
end

module MyApp
  class Application < Rails::Application
    custom_paths = %W[#{config.root}/lib ... ] # We have many other paths here
    config.autoload_paths += custom_paths
    config.autoload_once_paths += ["#{config.root}/lib/errors"]
    config.eager_load_paths += custom_paths
    config.eager_load = true

    ...

  end
end

lib/random_logo.rb

class RandomLogo
  ...
end

config/initializers/paperclip_interpolations.rb

Paperclip.interpolates :random_logo do |attachment, style|
  RandomLogo.path style, attachment.instance.first_letter, attachment.instance.logo_color
end

...

app/workers/my_worker.rb

class MyWorker
  include Sidekiq::Worker

  sidekiq_options queue: :publish, unique: true, unique_args: :unique_args,
    unique_unlock_order: :before_yield

  class << self
    def unique_args(id, action, publish_job_openings)
      [id, action, publish_job_openings]
    end
  end

  def perform(id, action, publish_job_openings)
    # This method will serialize an object using ActiveModel::Serializer,
    # trying to obtain the URL for the logo and raising
    # NameError: uninitialized constant RandomLogo
  end
end

In addition:

@mperham
Copy link
Collaborator

mperham commented Dec 10, 2014

I have no idea what's causing this. But based on my experience this usually indicates a problem loading RandomLogo, i.e. one of its dependencies can't be loaded.

@fertapric
Copy link
Author

RandomLogo class has no dependencies and it's a pretty simple class

Here is the entire source code:

lib/random_logo.rb

class RandomLogo
  COLORS = %w[
    5D9CEC
    4A89DC

    4FC1E9
    3BAFDA

    48CFAD
    37BC9B

    A0D468
    8CC152

    EC87C0
    D770AD

    AC92EC
    967ADC

    ED5565
    DA4453

    FC6E51
    E9573F

    FFCE54
    F6BB42

    656D78
    434A54

    CCD1D9
    AAB2BD

    AB988C
    8E7E74
  ]

  LETTERS = ("A".."Z").to_a + ("0".."9").to_a

  SIZES = {
    original: '400x255',
    large: '147x92',
    profile: '122x77',
    identity: '79x50',
    contact: '58x39',
    thumb: '65x65#',
    small: '250x160#',
    regular: '298x190#',
    extra_large: '400x255#'
  }

  class << self
    def path(size = :original, letter = nil, color = nil)
      letter ||= random_letter
      color ||= random_color

      "logo.#{size}.#{letter.upcase}.#{color}.png"
    end

    def random_letter
      LETTERS.sample
    end

    def random_color
      COLORS.sample
    end
  end

end

@mperham
Copy link
Collaborator

mperham commented Dec 10, 2014

Have you tried moving it into app/models and seeing if it just starts working?

@fertapric
Copy link
Author

No, I have not. I will try and post results.

@fertapric
Copy link
Author

Ok, I'm gonna close this issue. At the end, it was not related to Sidekiq.

It seems that one of our Capistrano tasks was not restarting Sidekiq properly in some of our servers. As result, we were running old code (facepalm).

Thanks!

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

2 participants