Why not split the job into 3 separate jobs, one for each section of the process. At the end of each job, fire the event. Then in a listener, queue the next section of the job up and in another listener handle your progress bar. Put all the Jobs in a subdirectory to group them together.
Release Queued Events Immediately
Hey folks,
I am currently facing a problem which I cannot wrap my head around. We are currently using Laravel Echo in combination with Queues and the use case that I am trying to do is as follows:
- User Uploads a file
- The endpoint for uploading the file is dispatching a Job
- The Job takes care of processing the file and during the process dispatches multiple events
- Echo listens to the events being fired and updates the progress accordingly
What actually happens is, that all events within the Job are queued up and released only after the handle method on the job is being executed. That makes it impossible for me to display a Progress Bar.
Just for Demonstration purposes I am providing some sample code of the handle method of the job:
public function handle(ProductNumberAvailabilityService $availabilityService, Manager $manager)
{
$this->notification->addData(['progress' => .25]);
$manager->update($this->notification);
event(new GtinCheckProcessing($this->notification));
sleep(1);
$this->notification->addData(['progress' => .75]);
$manager->update($this->notification);
event(new GtinCheckProcessing($this->notification));
sleep(1);
//.. do some more stuff
// Finish the Job
$this->notification
->setStatus(Notification::STATUS_SUCCEEDED)
->addData(['download' => Storage::url($downloadFile)]);
$manager->update($this->notification);
event(new GtinCheckProcessing($this->notification));
}
What I expect to happen, is that before each sleep the event is fired immediately. What actually happens is, that all events are released only after the whole method is executed.
Is there anything I can do to actually fire an Event without queing it?
Edit: I just tested a little bit, and if I remove the ShouldQueue Interface, the events will be fired immediately, however this leads to the fact, that the Request where the Job was dispatched from, will wait until the Job is finished until responding. This is also not the optimal solution.
Thanks in advance
Please or to participate in this conversation.