当从应用程序(而不是控制台)调用时,如何让ActiveJob在Herki上将Sidekiq中的作业排入队列?

Jas*_*ton 24 ruby-on-rails heroku sidekiq ruby-on-rails-4.2

我在Heroku(雪松),Ruby 2.1.6上运行Rails 4.2

我已经在Redis上使用Foreman在本地运行Sidekiq,在我的应用程序中,我调用Rails的新的ActiveJob deliver_later方法,如下所示application_helper.rb:

assigning_user = User.find(object.created_by)
completing_user = User.find(object.completed_by)
if NotificationMailer.assigned_user_completed_todo(object, completing_user, assigning_user).deliver_later
  nn.email_status = "sent"
end
Run Code Online (Sandbox Code Playgroud)

在本地,这可以按预期工作.邮件显示在"邮件程序"队列中,然后使用sendgrid进行处理和传递.

heroku run rails console我可以手动触发它,它工作得很好:

Loading production environment (Rails 4.2.1)
irb(main):001:0> a = Account.first
Account Load (1.5ms)  SELECT  "accounts".* FROM "accounts"  ORDER BY     "accounts"."id" ASC LIMIT 1
=> #<Account id: 1, name: "The Prestons", stripe_customer_id: nil, active_until: "2015-06-27 14:50:43", plan: nil, created_at: "2015-04-27 14:50:43", updated_at: "2015-04-27 14:50:43">
irb(main):002:0> NotificationMailer.new_account_created(a).deliver_later
Enqueued ActionMailer::DeliveryJob (Job ID: 7c0ddf9c-6892-4aa9-823e-9954b2a4e642) to Sidekiq(mailers) with arguments: "NotificationMailer", "new_account_created", "deliver_now", gid://raisin/Account/1
=> #<ActionMailer::DeliveryJob:0x007f4d9ed9da58 @arguments=["NotificationMailer", "new_account_created", "deliver_now", #<Account id: 1, name: "The Prestons", stripe_customer_id: nil, active_until: "2015-06-27 14:50:43", plan: nil, created_at: "2015-04-27 14:50:43", updated_at: "2015-04-27 14:50:43">], @job_id="7c0ddf9c-6892-4aa9-823e-9954b2a4e642", @queue_name="mailers">
Run Code Online (Sandbox Code Playgroud)

但是,如果我只是在我的应用程序中完成操作,就像我作为用户的方式,没有任何东西被传递到队列......我完全没有想法.我正在运行一个工作进程.我连接到redis云服务器.我可以看到sidekiq网页仪表板.

我没有设置什么?很高兴提供其他日志/信息,但不确定还提供什么.

我的控制台只是报告操作:

2015-05-14T09:14:37.674095+00:00 heroku[router]: at=info method=POST path="/accounts/1/projects/15/lists/33/items/112" host=www.myapp.com request_id=516db810-8a2c-4fd0-af89-29a59783b0a8 fwd="76.104.193.78" dyno=web.1 connect=1ms service=111ms status=200 bytes=2039
Run Code Online (Sandbox Code Playgroud)

Jas*_*ton 86

令人尴尬的是,事实证明一切正常,我通过代码测试电子邮件传递所采取的行动永远不会发送电子邮件,因为我测试它的用户在设置中关闭电子邮件通知.

叹.

但对于后人以及那些试图让Sidekiq使用Rails 4.2和Redis Cloud在Heroku上工作的人来说,我遇到了一些陷阱:让我花费最多时间来弄清楚的部分是为了让Sidekiq找到你的Redis服务器,你需要设置ENV变量IN Heroku(不在你的代码中).所以,在控制台,首先添加例如rediscloud:

heroku addons:create rediscloud

然后,您需要将URL添加为环境变量:

heroku config:set REDIS_PROVIDER=REDISCLOUD_URL

Sidekiq文档非常有用.我曾经brew install redis在本地安装Redis,然后redis-server在本地终端窗口中运行服务器.添加gem 'sidekiq'到Gemfile中,一切都在开发中很有用.

如果您按此处所示添加路线,您将能够在Web浏览器中监控队列.

最后,不要忘记你需要在你的ProcfileAND中添加一个worker 来使用heroku ps:scale worker=1它在Heroku中扩展到一个worker - 它不会因为它在你的Procfile中而启动.

另外:当您将Sidekiq放入Procfile时,请务必告诉它处理邮件程序队列,例如

worker: bundle exec sidekiq -q default -q mailers

  • 在Heroku中启用Sidekiq的精彩摘要,感谢您的精彩写作! (16认同)
  • 好的写作!我唯一错过的东西(但我可能没有充分阅读文档)是你真的需要为sidekiq添加一个初始化器,你可以在其中定义要使用的连接数,例如https://gist.github.com/ vindia/7f356499e646898dbeab如果没有该初始化程序,我的作业就会卡在队列中. (2认同)
  • 感谢您提到-q mailers选项,我错过了 (2认同)