每当 gem 并从偏移量开始每 n 分钟安排一次作业

发布于 2024-11-14 19:52:03 字数 312 浏览 4 评论 0原文

出于惊人的目的,我尝试将作业安排为每 5 分钟运行一次 2 分钟的偏移量。也就是说,我希望 1 个作业运行 1,6,11,16..,另一个作业运行在 2,7,12,17...

我找不到执行此操作的示例。所以我尝试了:

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

这似乎可行,但所有 ':at' 示例看起来都期待字符串格式的时间。上述操作是否有效,或者只是碰巧起作用,并且每个选项并不真正支持开始时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥琐帝 2024-11-21 19:52:03

听起来这两项工作之间存在依赖关系,所以我认为您可以通过两种方法来处理这个问题。如果您想在 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

但最好在第一个作业完成后执行第二个作业。这应该确保作业不会重叠,并且第二个作业仅在第一个作业完成后运行。

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

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
£噩梦荏苒 2024-11-21 19:52:03

every 需要一个整数。

为了避免雷群问题,你可以这样做。

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

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

您也可以随机化偏移量,

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

就像其他答案一样,这不适用于相互依赖的任务。如果您的任务相互依赖,您应该使用 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

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
迷雾森÷林ヴ 2024-11-21 19:52:03

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

看一下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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文