Laravel任务重复执行问题求助:单次调度任务多次触发
Laravel队列任务重复执行问题求助
我在Laravel应用中遇到了任务重复执行的问题。仅调度了一次任务:
dispatch(new SendFirebaseNotificationJob($data));
但监控队列运行命令:
artisan queue:work --sleep=3 --tries=0 --backoff=3 --max-time=3600
时,发现同一个任务的handle方法被多次触发(4次、6次甚至10次),日志如下:
2023-05-21 23:31:24 App\Jobs\SendFirebaseNotificationJob ..... 108.77ms DONE 2023-05-21 23:31:57 App\Jobs\SendFirebaseNotificationJob ........... RUNNING 2023-05-21 23:31:57 App\Jobs\SendFirebaseNotificationJob ..... 152.49ms DONE 2023-05-21 23:32:24 App\Jobs\SendFirebaseNotificationJob ........... 2023-05-21 23:32:24 App\Jobs\SendFirebaseNotificationJob ........... RUNNING RUNNING 2023-05-21 23:32:24 App\Jobs\SendFirebaseNotificationJob ..... 209.73ms DONE 2023-05-21 23:32:24 App\Jobs\SendFirebaseNotificationJob ..... 444.06ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ........... RUNNING ........... RUNNING ........... RUNNING ........... RUNNING ........... RUNNING ........... RUNNING ........... RUNNING ........... RUNNING ........... RUNNING ........... RUNNING Processing job with UID: 764 <- //The same job start mutabile times! Processing job with UID: 764 Processing job with UID: 764 Processing job with UID: 764 Processing job with UID: 764 Processing job with UID: 764 Processing job with UID: 764 Processing job with UID: 764 Processing job with UID: 764 Processing job with UID: 764 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 131.32ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 131.92ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 159.31ms DONE 155.82ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 156.36ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 156.24ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 156.36ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 156.58ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 192.53ms DONE 2023-05-21 23:37:24 App\Jobs\SendFirebaseNotificationJob ..... 237.49ms DONE 2023-05-21 23:41:28 App\Jobs\SendFirebaseNotificationJob ........... RUNNING 2023-05-21 23:41:28 App\Jobs\SendFirebaseNotificationJob ........... RUNNING
已做排查:
- 检查过任务的幂等性
- 删除过数据库中的任务请求,避免重复调度
但问题仍存在,需要让任务仅执行一次,求解决建议或指导。
内容的提问来源于stack exchange,提问作者Bashar Al Masri
相关产品推荐
相关产品推荐

