Welcome to the course. We will begin talking about Laravel queues from a classic simple example: sending an email.
Why You Would Need Queues?
Sending an email takes time. If, for example, after registration, you need to send emails to administrators, this would fire several email notifications, and that could take a few seconds. Those couple of seconds are too long for the user to wait.
Example: Email Notifications to Administrators
Imagine a typical Laravel application scenario. The user registers to your application, and after it is created
Hi everyone,
I'm trying to use queues to manage system integration processes. I've already created the jobs and the logic they need to execute. I used the routes/console.php file to schedule them, but every time I run the queue with:
php artisan queue:work --queue=sync
it only executes a few jobs and then stops.
Do you have any ideas why this might be happening? Is there any additional tutorial or documentation on best practices for scheduling jobs with queues?
Thanks! Daniele
It's a little bit confusing, since the
routes/console.phphas nothing to do with queues, but rather - a scheduler.For queues themselves - your command seems correct. I'm not sure what might be an issue here. Do you maybe have an output available to see what's going on? (terminal at the point of stopping).
Also you might want to look into: https://laraveldaily.com/lesson/queues-laravel/queue-listen-local - for local development. For production, you do need to use a
daemonto launch a queue workerI used the console.php file because I wanted to schedule various jobs within one or more chains. For example: Schedule::call(function () { logger('Job chain started'); Bus::chain([ new SyncCustomersJob, new SyncWarehouseShipmentsLinesJob, ]) ->catch(function ($e) { logger()->error('Error: ' . $e->getMessage()); }) ->onQueue('sync') ->dispatch(); }) ->dailyAt('14:49');
logger('Sync chain dispatched'); My goal was to have these chains launched in parallel. However, it seems that jobs only get executed at the specific scheduled time(dailyAt('14:49')), and when the time changes, no new jobs are executed—even if they are part of the same chain. There are no errors—it's just that the next job doesn't run.
At this point, I’m wondering if using queues is the right tool for this kind of task.
Thanks a lot for your input and support!
So, in this case you should not use the console.php file, but rather - create a new command and schedule it. Inside the command - add the whole dispatching of your jobs.
The problem might be related to how scheduler works, but I'm not sure. I have never seen such implementation :) It's always like this:
That way, there's no closure magic happening and worst case, you can re-trigger the job manually by writing
php artisan trigger:sync-customers-and-shipments.Hope that helps!