Sidekiq perform_async未触发

时间:2017-11-10 11:29:45

标签: elasticsearch redis mongoid sidekiq

我正在使用Sidekiq创建Elasticsearch回调。我启动了Redis和Sidekiq,并在rails控制台中创建了一个对象,但似乎perform_sync没有触发。如果我将require 'sidekiq/testing';Sidekiq::Testing.inline!添加到sidekiq.rb,它会按照我的预期记录并引发错误。我错过了什么吗?

Ruby 2.3.0
Rails 5.0.2
Sidekiq 4.2.9
Redis 3.2.0

sidekiq.rb

REDIS_URL  = 'localhost'
REDIS_PORT = '6379'
Sidekiq.configure_server do |config|
  config.redis = { url: "redis://#{REDIS_URL}:#{REDIS_PORT}" }
end

Sidekiq.configure_client do |config|
  config.redis = { url: "redis://#{REDIS_URL}:#{REDIS_PORT}" }
end

indexable.rb

module Indexable
  extend ActiveSupport::Concern

  included do
    def index_elasticsearch
      Rails.logger.debug 'call'          
      Rails.logger.debug Indexer::Logger
      Rails.logger.debug Indexer::Client
      Rails.logger.debug self.id.to_s
      Indexer.perform_async(:index, self.id.to_s) # nothing happens here
      Rails.logger.debug 'after'         
      self
    end
  end

  class Indexer
    include Sidekiq::Worker
    sidekiq_options queue: :elasticsearch, retry: false, backtrace: true

    Logger = Sidekiq.logger.level = Logger::DEBUG ? Sidekiq.logger : nil

    raise 'No config/elasticsearch.yml' unless File.exists? "config/elasticsearch.yml"
    erb    = ERB.new( File.read('config/elasticsearch.yml') ).result
    config = YAML.load(erb)[Rails.env].symbolize_keys
    config.merge! logger: Logger

    Client = Elasticsearch::Client.new(config)

    def perform(operation, record_id)
      Rails.logger.debug [ operation, "ID: #{record_id}"]
      raise
    end
  end
end

some_mongoid_class.rb

class SomeMongoidClass
  ...
  include ::Indexable
  ...
  after_save :index_elasticsearch
end

控制台

bundle exec sidekiq -e development --queue elasticsearch --verbose

...Booting Sidekiq 4.2.9 with redis options {:url=>"redis://localhost:6379"}...
2017-11-10T ... DEBUG: {:queues=>["elasticsearch"], :labels=>[], :concurrency=>25, :require=>".", :environment=>"development", :timeout=>8, ...

rails console

SomeMongoidClass.create(...)

before
#<Logger:0x...
#<Elasticsearch::Transport::Client:0x...
BSON::ObjectId('...')
after

1 个答案:

答案 0 :(得分:0)

我发布最新评论后,此问题就解决了。

这只是一个愚蠢的错误。我没有将工作程序文件放入应用程序的工作程序文件夹中!