防止在代码中调用的作业与 Laravel 后台作业 运行 重叠

Prevent a job, called in code, to overlap with job running in background in Laravel

在 Laravel,我运行在我的代码中找到了这样的工作

Artisan::queue('mediaserver:process-queue', ['index' => $indexName]);

同一作业计划每小时在后台 运行。有时我想通过我的 UI(因此通过代码)触发它来加快速度。我需要防止的是,该进程在后台始终 运行ning 时启动。

最好的工作方法是什么?

Laravel 的任务调度程序通过 creating a temp file before the command runs, and destroying it after 处理重叠作业。

文件名基于 cron 计划表达式和命令名。

The existence of this file cause subsequent attempts to run the same command to be skipped

我会做类似的事情。在 mediaserver:process-queue 任务的顶部,我会检查 storage/ 中是否存在名为 mediaserver__process_queue 的临时文件。如果是,请退出。如果它不创建它。任务结束时销毁它。

您只需要小心处理任务因未捕获的异常而意外退出的情况,导致 mediaserver__process_queue 即使任务不再 运行.