【问题标题】:rails cron job overlaprails cron作业重叠
【发布时间】:2016-04-22 00:20:18
【问题描述】:

我正在使用 heroku 调度程序,每 10 分钟运行一次任务。该任务正在执行如下操作。我正在考虑如何防止下一个工作与当前工作重叠。有什么办法可以防止 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

【问题讨论】:

  • the next job get the undone active record to call force_close! again 是什么意思?
  • 对不起我的英文,我现在改了。

标签: ruby-on-rails heroku cron


【解决方案1】:

有两种方法。

首先,
在问题上创建选中的字段。喜欢force_closed:boolean 并在调用force_close! 方法时触摸 force_closed 字段 所以你可以通过where(closed: false, force_closed: false)找到问题

第二,
创建批处理历史表。列应该是 task_namerun_at 并将运行时间信息保存在批处理历史表中,

# last line of task
BatchHistory.create(task_name: 'force_close', run_at: Time.now)

【讨论】:

  • 谢谢。第二个应该更适合我的情况。
猜你喜欢
  • 2012-10-19
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 2018-04-18
  • 1970-01-01
  • 2012-04-28
相关资源
最近更新 更多