Heroku Scheduler任务未按预期运行

时间:2018-06-06 04:53:41

标签: ruby-on-rails heroku scheduled-tasks rake rake-task

我使用Heroku的Scheduler插件以预定的时间间隔在我的应用中运行作业,就像传统服务器环境中的cron一样。这些任务相当简单,因为它们只是将一个sidekiq工作者排入队列。但是,如下图所示,由于某种原因,:update_job_posts无法在4:30UTC运行。

我无法以这种方式合并这两个作业,还是需要单独运行

如果我使用heroku run运行任务,它就会通过。 heroku run用于执行您的作业,因此您可以放心,如果它适用于heroku运行,它将在调度程序中运行。

$ heroku run rake scheduler:update_job_posts
Running rake scheduler:update_job_posts on ⬢ agile-temple-76502... up, run.6604 (Free)
Updating Job feed...
I, [2018-06-06T04:40:50.341411 #4]  INFO -- : [ActiveJob] Enqueued JobFeedImportJob (Job ID: e3b9a0ed-aacb-45af-84f9-ef02acfeb85e) to Sidekiq(default)
I, [2018-06-06T04:40:50.344400 #4]  INFO -- : [ActiveJob] Enqueued RemoteFeedImportJob (Job ID: b965853d-adb3-46ee-b48d-f19306ce9d2a) to Sidekiq(default)
done.

此外,heroku logs --tail显示正在运行的其他任务:

heroku[scheduler.5009]: Starting process with command `bundle exec rake scheduler:update_news_articles`
heroku[scheduler.5009]: State changed from starting to up
app[scheduler.5009]: Updating News feed...
app[scheduler.5009]: I, [2018-06-06T04:31:05.636594 #4]  INFO -- : [ActiveJob] Enqueued NewsFeedImportJob (Job ID: d672b0c5-598b-4eb7-81b6-1a0d2d8a5891) to Sidekiq(default)
app[scheduler.5009]: done.
app[worker.1]: 4 TID-tfdlg NewsFeedImportJob JID-d999a576b3193a404b3d8fdd INFO: start
app[worker.1]: I, [2018-06-06T04:31:05.636769 #4]  INFO -- : [ActiveJob] [NewsFeedImportJob] [d672b0c5-598b-4eb7-81b6-1a0d2d8a5891] Performing NewsFeedImportJob from Sidekiq(default)
heroku[scheduler.5009]: Process exited with status 0
heroku[scheduler.5009]: State changed from up to complete

scheduler.rake

namespace :scheduler do
  desc "Enqueue sidekiq job, fetches events, and creates a record for new entries"
  task :update_event_feed => :environment do
    puts "Updating Event feed..."
    EventFeedImportJob.perform_later
    puts "done."
  end

  desc "Enqueue sidekiq job, fetches job posts, and creates a record for new entries"
  task :update_job_posts => :environment do
    puts "Updating Job feed..."
    JobFeedImportJob.perform_later
    RemoteFeedImportJob.perform_later
    puts "done."
  end

  desc "Enqueue sidekiq job, fetches news articles, and creates a record for new entries"
  task :update_news_articles => :environment do
    puts "Updating News feed..."
    NewsFeedImportJob.perform_later
    puts "done."
  end
end

Heroku Scheduler

1 个答案:

答案 0 :(得分:2)

Known issues and alternatives

Heroku Scheduler在文档中说明

  

计划程序是一个免费的附加组件,不保证作业将在预定的时间执行,或者根本不执行:

在极少数情况下,可能会跳过作业。

在极少数情况下,作业可能会运行两次。

我在某些情况下实现了这一点,我注意到几天之后它已经不能正常使用后台进程然后再次阅读他们的文档并找出原因。

现在我已经实施了sidekiq-scheduler并且它正常运行。

我希望它会有所帮助。