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

相關問題