每当宝石和每隔n分钟安排一个工作从偏移开始

时间:2021-07-08 08:01:53

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...

出于惊人的目的,我试图安排工作每2分钟一次,每5分钟一次。那就是我想要1个工作来运行1,6,11,16 ..而另一个工作要运行在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.

这似乎有效,但所有':at'示例看起来都期待字符串格式的时间。以上是有效的,或者只是恰好工作,并且每个选项都不支持起始时间。

3 个解决方案

#1


18  

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:

听起来你有两个工作之间的依赖关系,所以有两种方法我认为你可以处理这个问题。如果你想在1,6,11,16等运行,正如你的问题所示,那么只需使用原始cron语法:

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

#2


6  

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.

与其他答案一样,这对于相互依赖的任务不起作用。如果您的任务彼此依赖,您应该使用rake为您处理它或在您的任务中手动运行下一个。

# 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

#3


2  

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

是的,这应该是有效的。请查看https://github.com/javan/whenever/blob/master/lib/whenever/cron.rb

Look at the parse_time method. These lines in particular:

看看parse_time方法。特别是这些界限:

      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)

#1


18  

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:

听起来你有两个工作之间的依赖关系,所以有两种方法我认为你可以处理这个问题。如果你想在1,6,11,16等运行,正如你的问题所示,那么只需使用原始cron语法:

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

#2


6  

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.

与其他答案一样,这对于相互依赖的任务不起作用。如果您的任务彼此依赖,您应该使用rake为您处理它或在您的任务中手动运行下一个。

# 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

#3


2  

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

是的,这应该是有效的。请查看https://github.com/javan/whenever/blob/master/lib/whenever/cron.rb

Look at the parse_time method. These lines in particular:

看看parse_time方法。特别是这些界限:

      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)