Send notification after all instances of a job completes
Every day twice a scheduled cron triggers multiple instances of a Laravel. Multiple queue workers also run at the same - so many of these jobs might run in parallel. This number of instances could vary each time. Is there a built-in way to identify if all instances of the job dispatched at the same time completed?
For example. I have a job called GetNewItems scheduled to run at 1 am and 1 pm. Suppose there are 10 instances of the job that started at 1 am. In case a few of these don't complete and it is already 1 pm, then a new set of jobs come in again. Is there a way I can send a notification when all the instances initiated at 1 am completes?
As far as I know it's not possible to monitor it like that. However there are multiple ways in which you can fix this.
It might not be the best solution, but I currently can't think of a better solution. Let's say after each job you simply fire an event that keeps a counter with the completed jobs. You can then have a scheduled task that checks every minute if all jobs are completed by checking the counter. If that is in this case you reset the counter and you then know if this is done. You can also keep a counter for each day that you run those 10 jobs. I hope you get the idea.
But i notice that with more than 100 jobs on different workers, randomly i dont get my job_current incremented even if all jobs are successfully done (which means while all my jobs succeed, in my table some are missing as the job_current stay < to job_max)
So i feel :
all jobProcessed event might not be all fired
or my table column job_current cant handle potentially simultaneous update of some jobs completing in same time
@Dave97 It's probably the second reason! You can however add a lock on writing to the database. You need to try this out. Not sure what else you have in your application that could cause this!
@bobbybouwmann
Yes, i decided to implement a shared lock...
but i still often have the last job which doesnt increment my db, as if the last job was not all the time firing the Queue::after JobProcessed or JobFailed listener that im using for incrementation (sometimes it does so the code must be ok).
I have 100 http call so 100jobs with 20 workers. It is kind of surprising to see the 50 first jobs are done within 2s then for the next ones, the lengh of time increase progressively until 50s. But they are all done despite the fact that it randomly doesnst increment the last one in db.
I am not familiar with using shared lock:
What happen when a job try to update a row of db locked by another job, does it wait or does it fail?
This answer could help me to understand what happen....