Laravel任务调度命令的频率、延迟或偏移

3

有没有办法延迟或偏移计划中的命令,以符合提供的频率选项?

例如:

$schedule->command('GetX')->everyTenMinutes(); --> run at 9:10, 9:20, 9:30
$schedule->command('GetY')->everyTenMinutes(); --> run at 9:15, 9:25, 9:35
1个回答

3
在安排任务时,没有延迟功能。
但是可以使用"when"方法来安排一个任务,每10分钟执行一次,延迟5分钟。
// this command is scheduled to run if minute is 05, 15, 25, 35, 45, 55
// the truth test is checked every minute
$schedule->command('foo:bar')->everyMinute()->when(function () {
    return date('i') - 5 % 10 == 0;
});

按照这个规则,你可以每隔x分钟安排一个任务,并延迟y分钟。
$schedule->command('foo:bar')->everyMinute()->when(function () {
    return date('i') - y % x == 0;
});

如果变得困难,你可以直接编写一个自定义的Cron计划。这是一种更容易理解的方式,以后阅读代码时不会头疼。
$schedule->command('foo:bar')->cron('05,15,25,35,45,55 * * * *');

1
谢谢 @Ben,date('m') 应该改成 date('i')。我把 date('m') - y % x == 0 改成了 substr(date('i'), -1) == 0substr(date('i'), -1) == 5,因为这样更容易理解。 - undefined
你可能错过了括号 return (date('m') - 5) % 10 == 0; - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接