[英]rails cron job overlap
我每10分鍾使用heroku Scheduler運行一個任務。 任務正在做某事,如下所示。 我正在考慮如何防止下一份工作與當前工作重疊。 有什么可以防止Cron工作重疊的問題嗎?
task force_close: :environment do
#get all unvoted wine_question
questions = Question.where(closed: false)
puts "Total #{questions} of wine_question will be closed"
finish_count = 0
questions.each do |question|
begin
question.force_close!
finish_count += 1
rescue StandardError => bang
puts "question #{self.id} error when running #{bang}"
end
end
puts "Total #{finish_count} of question was closed"
end
有兩種方法。
第一,
在問題上創建選中的字段。 就像force_closed:boolean
並在調用force_close!
時觸摸force_closed字段force_close!
方法,因此您可以通過where(closed: false, force_closed: false)
查找問題
第二,
創建批處理歷史記錄表。 列應為task_name
和run_at
,並將運行時間信息保存在批處理歷史記錄表中,
# last line of task
BatchHistory.create(task_name: 'force_close', run_at: Time.now)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.