Newly created sidekiq worker not found (NameError: uninitialized constant UniqueJobWorkerTest::UniqueJobWorker)

Newly created sidekiq worker not found (NameError: uninitialized constant UniqueJobWorkerTest::UniqueJobWorker)

unique_job_worker.rb

# -*- encoding : utf-8 -*-
require_relative 'logging_helper'

class UniqueJobWorker
  include Sidekiq::Worker
  include WorkerHelper
  sidekiq_options retry: false,
                  backtrace: true,
                  queue: :sender,
                  failures: true

  def perform(worker,campaign_guid, queue)
    require'pry';binding.pry
  end
end

unique_job_worker_test.rb

require 'test_helper'
require 'mocha/setup'

class UniqueJobWorkerTest < ActiveSupport::TestCase
  def setup
    require'pry';binding.pry
    @worker = UniqueJobWorker.new
  end
  
  test "it exists" do
    assert @worker
  end
end

当通过 redis 排队时,我得到了这个响应

INFO -- : Exception: uninitialized constant UniqueJobWorker

关于为什么我新创建的 worker UniqueJobWorker 在运行时没有通过 redis 或通过简单测试找到任何建议?

提前致谢!

当你在 Rails 之外使用 sidekiq 时,你需要使用 -r 选项来告诉它如何加载你的工人。所以(假设你的工作人员在一个名为 workers 的子目录中):

% sidekiq -r ./workers/unique_job_worker.rb

如果您有多个工作人员,一个选项是创建一个加载程序文件以确保加载所有内容。

load_workers.rb

($LOAD_PATH << 'workers').uniq!
require 'unique_job_worker'
require 'other_worker'
...

然后在命令行中require加载器文件:

% sidekiq -r ./load_workers.rb

我有同样的问题,最终成为 Redis 命名空间问题:

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

为我添加修复它:

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

你还需要 redis-namespace gem 顺便说一句