我想安排一个作业 (
Task1
) 每 5 秒运行一次。我有以下代码:
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule)
{
$schedule->job(new Task1)->everyFiveSeconds(); // <----- This is the line in question
$schedule->job(new Task2)->everyMinute();
$interval = DB::table('settings')->where('key', 'interval')?->value ?? 5;
$schedule->job(new Task3)->cron(StringHelper::cron24($interval));
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
我在 Telescope 上监控工作。该作业每 5 秒平稳运行一次,持续约 40 秒(8 个周期)。然而,它会在零秒标记周围跳过大约 4 个周期。例如
2024-08-28 06:47:24 Running [App\Jobs\Task1] ........ 9ms DONE
2024-08-28 06:47:29 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 06:47:34 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 06:47:39 Running [App\Jobs\Task1] ....... 10ms DONE
2024-08-28 06:47:44 Running [App\Jobs\Task1] ....... 10ms DONE
2024-08-28 06:47:49 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 06:47:54 Running [App\Jobs\Task1] ....... 12ms DONE
2024-08-28 06:47:59 Running [App\Jobs\Task1] ........ 9ms DONE
2024-08-28 06:48:22 Running [App\Jobs\Task1] ...... 314ms DONE <--- a jump here
2024-08-28 06:48:22 Running [App\Services\Task2] 11ms DONE
2024-08-28 06:48:22 Running [App\Services\Task3] 13ms DONE
2024-08-28 06:48:27 Running [App\Jobs\Task1] ........ 9ms DONE
2024-08-28 06:48:32 Running [App\Jobs\Task1] ........ 8ms DONE
2024-08-28 06:48:37 Running [App\Jobs\Task1] ........ 6ms DONE
2024-08-28 06:48:42 Running [App\Jobs\Task1] ........ 8ms DONE
2024-08-28 06:48:47 Running [App\Jobs\Task1] ........ 8ms DONE
2024-08-28 06:48:52 Running [App\Jobs\Task1] ........ 6ms DONE
2024-08-28 06:48:57 Running [App\Jobs\Task1] ........ 5ms DONE
2024-08-28 06:49:11 Running [App\Jobs\Task1] ...... 170ms DONE <--- a jump here
2024-08-28 06:49:11 Running [App\Services\Task2] 17ms DONE
2024-08-28 06:49:16 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 06:49:21 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 06:49:26 Running [App\Jobs\Task1] ....... 21ms DONE
2024-08-28 06:49:31 Running [App\Jobs\Task1] ....... 12ms DONE
2024-08-28 06:49:36 Running [App\Jobs\Task1] ....... 14ms DONE
2024-08-28 06:49:41 Running [App\Jobs\Task1] ........ 9ms DONE
Task1
运行时间不到 1 秒。我们在 AWS 上使用 Vapor 无服务器架构和 SQS。
当我将持续时间更改为 1 分钟 (
everyMinute
) 时,任务在 1 分钟标记处完美运行。
当我将持续时间更改为 15 秒 (
everyFifteenSeconds
) 时,作业每 15 秒正确运行一次。
当我删除其他两个任务(
Task2
和 Task3
)时,5 秒任务运行得更好一些,但仍然会在分钟内跳跃。
2024-08-28 07:21:31 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 07:21:36 Running [App\Jobs\Task1] ....... 13ms DONE
2024-08-28 07:21:41 Running [App\Jobs\Task1] ........ 8ms DONE
2024-08-28 07:21:46 Running [App\Jobs\Task1] ........ 5ms DONE
2024-08-28 07:21:51 Running [App\Jobs\Task1] ........ 8ms DONE
2024-08-28 07:21:56 Running [App\Jobs\Task1] ........ 6ms DONE
2024-08-28 07:22:11 Running [App\Jobs\Task1] ....... 91ms DONE <--- a jump here
2024-08-28 07:22:16 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 07:22:21 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 07:22:26 Running [App\Jobs\Task1] ........ 7ms DONE
2024-08-28 07:22:31 Running [App\Jobs\Task1] ........ 9ms DONE
2024-08-28 07:22:36 Running [App\Jobs\Task1] ........ 8ms DONE
2024-08-28 07:22:41 Running [App\Jobs\Task1] ........ 8ms DONE
2024-08-28 07:22:46 Running [App\Jobs\Task1] ........ 8ms DONE
2024-08-28 07:22:51 Running [App\Jobs\Task1] ........ 6ms DONE
2024-08-28 07:22:56 Running [App\Jobs\Task1] ........ 7ms DONE <--- a jump here
2024-08-28 07:23:08 Running [App\Jobs\Task1] ....... 88ms DONE
2024-08-28 07:23:13 Running [App\Jobs\Task1] ........ 9ms DONE
2024-08-28 07:23:18 Running [App\Jobs\Task1] ........ 5ms DONE
2024-08-28 07:23:23 Running [App\Jobs\Task1] ........ 5ms DONE
有什么想法可能是什么原因吗?或者我该如何解决这个问题?谢谢。