我在Heroku上运行一名sidekiq工作人员,通常我对设置没有任何问题:
Procfile
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
worker: bundle exec sidekiq -C config/sidekiq.yml
application.rb中
config.active_job.queue_adapter = :sidekiq
我有几个worker,perform_async按预期工作。 我也有一个邮件,当我打电话
时,它被正确处理AlertsMailer.favorite_finishing(user).deliver_later
我相信Redis去正确设置,因为除非从rake任务中运行,否则一切正常......
问题是我想使用Heroku调度程序和rake任务发送这些电子邮件:
namespace :scheduler do
task :alert_finishing_favorites => [ :environment ] do
users = User.all
users.each_with_index do | user, index |
Rails.logger.debug "#{index} Delivering AlertsMailer"
AlertsMailer.favorite_finishing(user).deliver_later
end
end
end
从任务调用时,没有任何内容会添加到我的邮件程序队列中,就像我从应用程序或控制台中调用相同的内容一样......
因此,只有在将其作为rake任务运行时,设置才能正常工作。
当我跑步时
heroku run bundle exec rake scheduler:alert_finishing_favorites -a my-app-name
...我得到了这个输出:
Running bundle exec rake scheduler:alert_finishing_favorites on ⬢ my-app-name... up, run.7112 (Free)
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Starting the New Relic agent in "staging" environment.
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : To prevent agent startup add a NEW_RELIC_AGENT_ENABLED=false environment variable or modify the "staging" section of your newrelic.yml.
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Reading configuration from config/newrelic.yml (/app)
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Environment: staging
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : No known dispatcher detected.
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Application: subasteo-eu-prepro
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Redis Instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Net instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Sidekiq instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Memcache instrumentation for dalli gem
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing deferred Rack::Builder instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Rack::Builder middleware instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing deferred Puma::Rack::Builder instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Puma::Rack::Builder middleware instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Rails 3+ middleware instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing ActiveJob instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing ActiveRecord 5 instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Rails 5 Controller instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Rails 5 Action Cable instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Installing Rails 5 view instrumentation
** [NewRelic][2017-01-10 11:33:53 +0000 run.* (4)] INFO : Finished instrumentation
D, [2017-01-10T11:33:54.681738 #4] DEBUG -- : Like Load (71.6ms) SELECT "likes".* FROM "likes" WHERE "likes"."value" = $1 [["value", 1]]
** [NewRelic][2017-01-10 11:33:54 +0000 run.* (4)] INFO : Starting Agent shutdown
虽然在Papertrail我只看到这个:
Jan 10 03:47:32 my-app-name heroku/router: at=info method=GET path="/sidekiq/stats" host=my-app-name.herokuapp.com request_id=198a1796-d82f-44e9-a181-82ee954ccf5b fwd="818.217.253.237" dyno=web.1 connect=0ms service=247ms status=200 bytes=2323
Jan 10 03:47:33 my-app-name heroku/run.1789: Awaiting client
Jan 10 03:47:33 my-app-name heroku/run.1789: Starting process with command `bundle exec rake scheduler:alert_finishing_favorites`
Jan 10 03:47:33 my-app-name heroku/run.1789: State changed from starting to up
不知道为什么deliver_later
直接形成rake任务不起作用,但最后我决定创建一个专职工作者并从表演中调用deliver_now
( ) 方法。按预期工作......
耙
namespace :scheduler do
task :alert_finishing_favorites => [ :environment ] do
AlertasFavoritesWorker.perform_async()
end
end
工人 类AlertasFavoritesWorker
include Sidekiq::Worker
# sidekiq_options :queue => :pando, :backtrace=> true
sidekiq_options :retry => 2 # Limit it since we are scraping. Default is 25!!!
# args are converted into JSON by sidekiq to be saved in Redis
def perform()
likes = Like.where(value: 1)
p "Delivering for #{likes.count} likes!"
Rails.logger.debug "Delivering for #{likes.count}! likes"
likes.each_with_index do | like, index |
Rails.logger.debug "#{index} Delivering AlertsMailer for #{like.user.email}"
p "#{index} Delivering AlertsMailer for #{like.user.email}"
AlertsMailer.favorite_finishing(like).deliver_now
end
end
end