Laravel multiple workers running job twice

北战南征 提交于 2019-12-25 00:41:30

问题


I am using Laravel 5.6 and I am dispatching jobs to a queue and then using supervisor to activate 8 workers on that queue. I was expecting that Laravel will know NOT to run the same job twice but I was surprised to discover that it did. Same job was taken cared of by more than one worker and therefore weird stuff started to happen.

The thing is that one year ago I wrote the same mechanism for another Laravel project (but on Laravel version 5.1) and the whole thing worked out of the box. I didn't have to configure anything.

Anyone can help? Thank you.


回答1:


I was having the exact same problem and It drove me crazy until I managed to solve it!

For some reason Laravel 5.6 creates the "jobs" table with engine=MyISAM which does not support transactions which are necessary for the locking mechanism that prevents the case of job runs twice. I believe that it was different in Laravel 5.1 because I also once wrote an app with Laravel 5.4 and it worked perfectly with 8 workers. When I did the same thing with Laravel 5.6 it didn't work. Same as you describe.

Once I've changed the Engine to InnoDB which supports transactions everything worked as expected and the locking mechanism started to work.

So basically all you need to do is:

ALTER TABLE jobs ENGINE = InnoDB;

Hope that will solve your misery...




回答2:


$schedule->command('emails:send')->withoutOverlapping();



来源:https://stackoverflow.com/questions/51425360/laravel-multiple-workers-running-job-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!