英文:
Laravel Job Duplication
问题
我目前在我的Laravel应用程序中遇到了作业复制的问题。我有一个作业,我只调度一次 dispatch(new SendFirebaseNotificationJob($data));
,但在监视 artisan queue:work --sleep=3 --tries=0 --backoff=3 --max-time=3600
命令时,有时会看到作业处理多次触发(4或6次)。
我尝试找出问题的原因,但到目前为止还没有找到解决方案。
我将不胜感激任何关于如何解决此问题的建议或指导。如果有人曾经遇到类似的问题或对我可以尝试的有任何建议,我将非常感激。
谢谢您提前的帮助。
英文:
I'm currently experiencing an issue with job duplication in my Laravel application.
I have a job that I dispatch once dispatch(new SendFirebaseNotificationJob($data));
, but while monitoring the artisan queue:work --sleep=3 --tries=0 --backoff=3 --max-time=3600
command, I sometimes see that the job handle has been triggered multiple times (4 or 6 times).
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
I've tried to identify the cause of the issue, but I haven't been able to find a solution so far.
I would appreciate any advice or guidance on how to resolve this issue. If anyone has experienced a similar problem or has any suggestions on what I could try, I would be grateful for any input.
Thanks in advance for your help.
Checking if the job is idempotent: I reviewed the job code to ensure that it can not be executed multiple times, i deleted the job request from the DB so it will not be dispatched.
However, I am still experiencing job duplication in my Laravel application. I expected the job to be executed only once
答案1
得分: 0
你可以使用WithoutOverlapping作业中间件来防止多次分派作业和作业重叠 - https://laravel.com/docs/10.x/queues#preventing-job-overlaps
在需要确保仅分派一次的作业上使用它是一个好的做法。
英文:
You can prevent dispatching job multiple times and job overlaps with WithoutOverlapping job middleware - https://laravel.com/docs/10.x/queues#preventing-job-overlaps
It is good practice to use it on jobs that you need to be sure are dispatched only once.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论