我已经用我的 Rails 项目设置了 Sidekiq。它与 Unicorn 一起在 Heroku 上运行。我已经完成了所有配置步骤,包括设置正确的 REDISTOGO_URL ( as this question references ),我在 unicorn.rb 的 after_fork 中添加了以下内容:
after_fork do |server,worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
Rails.logger.info('Connected to ActiveRecord')
end
Sidekiq.configure_client do |config|
config.redis = { :size => 1 }
end
end
我的 Procfile 如下:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: bundle exec sidekiq
现在我调用我的工作人员 perform_async 并将任务添加到队列中。事实上,在我的 Sidekiq Web 界面中,它说队列中有 7 个项目,并且那里有所有数据。然而,没有工作人员处理队列,对于我的一生,我不知道为什么。如果我跑
heroku ps
我得到以下输出:
=== web: `bundle exec unicorn -p $PORT -c ./config/unicorn.rb`
web.1: up 2012/12/09 08:04:24 (~ 9m ago)
=== worker: `bundle exec sidekiq`
worker.1: up 2012/12/09 08:04:08 (~ 10m ago)
有人知道这里发生了什么吗?
更新
这是我的 worker 类的代码。是的,我知道 Oj gem 可能与 sidekiq 存在一些问题,但我想我会先试一试。此时我没有收到任何错误消息(工作人员甚至没有运行)。
require 'addressable/uri'
class DatasiftInteractionsWorker
include Sidekiq::Worker
sidekiq_options queue: "tweets"
def perform( stream_id , interactions )
interactions = Oj.load(interactions)
interactions.each{ |interaction|
if interaction['interaction']['type'] == 'twitter'
url = interaction['links']['normalized_url'] unless interaction['links']['normalized_url'][0].nil?
url = interaction['links']['url'] if interaction['links']['normalized_url'][0].nil?
begin
puts interaction['links'] if url[0].nil?
next if url[0].nil?
host = Addressable::URI.parse(url[0]).host
host = host.gsub(/^www\.(.*)$/,'\1')
date_str = Time.now.strftime('%Y%m%d')
REDIS.pipelined do
# Add domain to Redis domains Set
REDIS.sadd date_str , host
# INCR Redis host
REDIS.incr( host + date_str )
end
rescue
puts "ERROR: Could not store the following links: " + interaction['links'].to_s
end
end
}
end
end
最佳答案
我的偏好是创建一个/config/sidekiq.yml 文件,然后在 Procfile 中使用 worker: bundle exec sidekiq -C config/sidekiq.yml
。
Here's 示例 sidekiq.yml 文件
关于ruby-on-rails - 如何让 Sidekiq worker 在 Heroku 上运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13789186/