开发者

whenever gem and scheduling a job every n min starting at an offset

开发者_如何学编程

For staggering purposes I am trying to schedule jobs an a 2 minute offset that run every 5 mins. That is I want 1 job to run 1,6,11,16.. and the other one to run at 2,7,12,17...

I couldn't find an example to do this. So I tried:

every 5.minutes, :at=> 1 do
 command "echo 'you can use raw cron sytax too'"
end 

This seems to work but all the ':at' examples look to be expecting a time in a string format. Is the above valid to do or is just happens to work and the every option doesn't really support a starting time.


It sounds like you have a dependency between the two jobs, so there are two ways I think you can handle this. If you want to run at 1,6,11,16 and so on, as your question suggests, then simply use raw cron syntax:

every '0,5,10,15,20,25,30,35,40,45,50,55 * * * *' do
  command "echo 'you can use raw cron syntax one'"
end

every '1,6,11,16,21,26,31,36,41,46,51,56 * * * *' do
  command "echo 'you can use raw cron syntax two'"
end

But it's probably better to execute the second job once the first one is done. This should ensure that the jobs don't overlap and that the second only runs when after the first completes.

every 5.minutes do
  command "echo 'one' && echo 'two'"
end


every expects an integer.

To avoid thundering herd problem, you can do this.

every 5.minutes - 10.seconds do
  command "echo first task"
end

every 5.minutes + 10.seconds do
  command "echo second task"
end

You can randomise the offset too

def some_seconds
  (-10..10).to_a.sample.seconds
end

every 5.minutes + some_seconds do
  command "echo first task"
end

every 5.minutes + some_seconds do
  command "echo second task"
end

Like other answers this won't work for tasks depending on each other. If your tasks depend on each other, you should use rake to handle it for you or run next one manually in your task.

# shedule.rb
every 5.minutes do
  rake 'jobs:last_one'
end

# Rakefile.rb
namespace :jobs do
  task :first_one do
    sh 'first command'
  end

  task second_one: [:first_one] do
    sh 'second_command that_should_run_after_first_one'
  end

  task last_one: [:second_one] do
    sh 'last_command that_should_run_after_all_commands'
  end
end


Yes, that should be valid. Look at https://github.com/javan/whenever/blob/master/lib/whenever/cron.rb

Look at the parse_time method. These lines in particular:

      when 1.hour...1.day
        hour_frequency = (@time / 60 / 60).round
        timing[0] = @at.is_a?(Time) ? @at.min : @at
        timing[1] = comma_separated_timing(hour_frequency, 23)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜