机器在运行cron作业时持续1分钟

时间:2016-02-10 05:18:55

标签: ruby-on-rails rake crontab whenever

我是Rails的新手,我正在使用每次来运行cron job。在我的应用程序中,我有一些可以由驱动程序执行的作业。作业有开始和结束时间,它可以分配给drivers.By cron job我需要检查一些条件,比如

1. job is assigned and start within 30 minutes.
2. job is unassigned and start within 30 minutes.
3. job is assigned and start within 15 minutes.

我在.rake文件中添加了条件并在日程文件中提到了作业,下面我粘贴了我的代码片段。

schedule.rb

every 1.minutes do
  rake "jobs:unassigned_job_start_in_30"
end

every 1.minutes do
  rake "jobs:assigned_job_start_in_30"
end

every 1.minutes do
  rake "jobs:assigned_job_start_in_15"
end

和我的rake文件 auto_expire.rake

desc "assigned job starts in 30 mins."
task :assigned_job_start_in_30 => :environment do
start_range = Time.now.utc.to_time.to_i+(CONFIG[:job_starts_in_time_30]*60)
end_range   = start_range + (1*60)
jobs = Job.where("start_at >= :start_range AND start_at <= :end_range AND status =:status", {start_range: start_range.to_s, end_range: end_range.to_s, status: 2000} )
  jobs.each do |job|
    job.add_event_log(nil, 2054)
      create_and_send_notification(2054, job.id, 0, nil)
    end
  end

desc "assigned job starts in 15 mins."
task :assigned_job_start_in_15 => :environment do
start_range = Time.now.utc.to_time.to_i+    (CONFIG[:job_starts_in_time_15]*60)
end_range   = start_range + (1*60)
jobs = Job.where("start_at >= :start_range AND start_at <=  :end_range AND status =:status", {start_range: start_range.to_s,     end_range: end_range.to_s, status: 2000} )
jobs.each do |job|
  job.add_event_log(nil, 2055)
    create_and_send_notification(2055, job.id, 0, nil)
  end
end

由于cron作业的持续时间为1分钟,所以我的机器会挂起。请告诉我检查条件和执行cron作业的最佳方法是什么。

1 个答案:

答案 0 :(得分:0)

cron作业应该在后台运行,因此你的机器在运行时不应该挂起,可能还有其他原因。

另一种方法是,只运行一个任务,检查并创建运行它的单独系统的任务。一个例子是Sidekiq