Dan Tappin
Dan Tappin

Reputation: 3032

Sidekiq Job Fails in Production | NameError: uninitialized constant

I have an Expenditure model:

class Expenditure < ApplicationRecord
  multi_tenant :company
  after_commit :related_reindex

  def related_reindex
    ExpenditureRelatedReindex.perform_async(id)
  end
end

Here is my worker expenditure_related_reindex.rb:

class ExpenditureRelatedReindex
  include Sidekiq::Worker
  sidekiq_options :queue => :critical, :retry => true, :backtrace => true

  def perform(record_id)
    e = Expenditure.find(record_id)

    MultiTenant.with(e.company) do
      return unless e
      e.expenditure_items.each(&:reindex)
      e.children&.each(&:reindex)
    end
  end
end

The reindex can take some time so I want these to spin off to SideKiq. I have sime multi tenant code I should mention but I don't think it's the issue. After the record is updated I get:

NameError: uninitialized constant ExpenditureRelatedReindex
Did you mean?  ExpenditureItemsHelper
  from sidekiq/processor.rb:268:in `const_get'
  from sidekiq/processor.rb:268:in `constantize'
  from sidekiq/processor.rb:132:in `block (5 levels) in dispatch'
  from sidekiq/rails.rb:43:in `block in call'
  from active_support/execution_wrapper.rb:87:in `wrap'
  from active_support/reloader.rb:73:in `block in wrap'
  from active_support/execution_wrapper.rb:87:in `wrap'
  from active_support/reloader.rb:72:in `wrap'
  from sidekiq/rails.rb:42:in `call'
  from sidekiq/processor.rb:131:in `block (4 levels) in dispatch'
  from sidekiq/processor.rb:257:in `stats'
  from sidekiq/processor.rb:126:in `block (3 levels) in dispatch'
  from sidekiq/job_logger.rb:13:in `call'
  from sidekiq/processor.rb:125:in `block (2 levels) in dispatch'
  from sidekiq/job_retry.rb:78:in `global'
  from sidekiq/processor.rb:124:in `block in dispatch'
  from sidekiq/logger.rb:10:in `with'
  from sidekiq/job_logger.rb:33:in `prepare'
  from sidekiq/processor.rb:123:in `dispatch'
  from sidekiq/processor.rb:162:in `process'
  from sidekiq/processor.rb:78:in `process_one'
  from sidekiq/processor.rb:68:in `run'
  from sidekiq/util.rb:15:in `watchdog'
  from sidekiq/util.rb:24:in `block in safe_thread'

I have even taken out the entire 'perform' block of code i.e. the worker does nothing to confirm I don't have some sort of regressive call etc. I confirm that my other workers fire and process just fine. Checked for obvious typos - banging my head against the wall at this point.

UPDATE

Ok - I have confirmed one thing - if I add any new workers with any name this triggers the same error. I even rebooted the entire production server to confirm the whole code was reloaded etc.

Upvotes: 0

Views: 2184

Answers (1)

Dan Tappin
Dan Tappin

Reputation: 3032

It ended up being Redis (or at least my fix was related). Found this post:

https://github.com/mperham/sidekiq/issues/2834#issuecomment-184800981

and it was a Redis namespace conflict. My server does have elasticsearch running also so that makes sense. I am not sure why the old workers run but the new ones failed. My fix looks like this:

config.redis = {
    url: ENV['REDIS_URL'],
    namespace: "some_namespace_different_for_each_app"
  }

You also need the redis-namespace gem BTW

Upvotes: 1

Related Questions