I need to run a task for a list that can reach thousands of items.
To avoid a single job running for a long time, I've created a job to enqueue all items.
The problem is that I need to run another task before and after that queue gets done.
The solution I see is to use delays:
$schedule->job(new \App\Jobs\PauseSystem()) ->hourly('00:01');
$schedule->job(new \App\Jobs\EnqueueAllItems()) ->hourly('00:02'); // adds all items as separated job in a queue
$schedule->job(new \App\Jobs\ReopenSystem()) ->hourly('55:00');
This way I have the time between 00:02
and 55:00
to ensure all items have been completed.
It looks not safe and maybe causes job overlapping.
Is there any safer way to run a task after a queue finish all its jobs?
Since you mentioned there are parallel and multiple jobs, here is how we solved a similar issue. We have a reporting system to prepare a report from multiple customers. There were more than 1000 customers.
ReportPusher
job was responsible for a single customerReportCollector
job should do its work.ReportPusher
jobs completed. Here how we did;
ReportPusher
jobs at the same time$total
ReportCollector
n minutes later (it could be 15 minutes) ReportPusher
jobs increments another key when they complete their process $incremented
15 Minutes later when ReportCollector
works, do these;
total
equals to incremented count
let ReportCollector does it workThis fallback policy is to prevent total failure if there is a mistake in one/two of the customers. We don't wont to throw all the calculated data(%99 of the customers) just because of one/two customers. You may put some alert/error tracking system to fix corrupted data later.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.