2015-05-14 41 views
17

我在Heroku(雪松)上运行的Rails 4.2的Ruby 2.1.6从应用程序调用(不是控制台)时,如何让ActiveJob在Heroku上的Sidekiq中排队工作?

我有Sidekiq与工头Redis的本地运行,并在我的应用我叫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 

在本地,它的工作方式与预期相同。邮件显示在'邮件程序'队列中,然后使用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"> 

但如果在我的应用我只要简单地完成动作,我想作为一个用户,没有什么是永远传递到队列的方式...我完全没有想法。我正在运行一个工作进程。我已连接到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 
+2

你是否用'-q mailers'启动Sidekiq? –

+0

是的 - 忘了提及。在我的proc文件中:'worker:bundle exec sidekiq -q default -q mailers' –

回答

62

尴尬,原来一切都正常工作,我在做测试通过代码发送的电子邮件的行动绝不会已经发送了一封电子邮件,这是因为用户我正在测试它已在电子邮件通知关闭设置

感叹。

但是对于后人和那些试图让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在本地在终端窗口中运行服务器。在gemfile中添加了gem 'sidekiq',并且在开发中一切都很顺利。

如果您添加路线as shown here,您将能够在您的Web浏览器中监视队列。

最后,不要忘记,您需要在Procfile中添加一名工作人员,并使用heroku ps:scale worker=1将其扩展到heroku中的一名工作人员 - 它不会导致它在您的Procfile中启动。

另外:当您将sidekiq放入您的Procfile中时,一定要告诉它处理邮件程序队列,例如,:

worker: bundle exec sidekiq -q default -q mailers

+12

在Heroku中启用Sidekiq的精彩总结,感谢您的好评! –

+0

@jason ...和来自sidekiq的作者... :)是啊,很好的工作+1 –

+2

好文章!唯一我错过的东西(但我可能没有足够好的阅读文档)是,你实际上需要为sidekiq添加一个初始化程序,在其中定义要使用的连接数,如https://gist.github.com/ vindia/7f356499e646898dbeab 没有这个初始化程序,我的工作被困在队列中。 – vindia

相关问题