Run queues in the background on Development in Laravel.
Hi folks, in this post I will write about a simple command to get our queues running in the background, It will be helpful if you want to execute at least one time to show the functunallity:
The command
php artisan queue:work &
You can review your jobs with the command:
jobs
And you can get the process id by the job command with the flag “l”
jobs -l
Ok… but how I can store the jobs into a log file… that could be useful and yes there is a command to make it:
php artisan queue:work >storage/logs/yourjobsfilename.log
Now you can go to “storage/logs/” directory and you will see the log file with the name you choose, and if you make events with listeners that implements f “ShouldQueue” interface you will be able to see the logs in your file.
A little Reminder
To create a event with queues listeners you need to change your env file from:
QUEUE_CONNECTION=sync
To
QUEUE_CONNECTION=database
If you are going to work with “database” connection is neccesary to run the next migrations:
php artisan queue:table
php artisan migrate
Also have an Event and a Listener that implements the “ShouldQueue” interface, and by last register the event associated with the listener or listeners in your “providers/EventProvider.php” path and in “EventProvider.php” file add your event and listeners with the next notation as example:
protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ],];
Remeber Its important to kill your background process
you can use the next command to kill your process
jobs -l
KILL <theProcessIdNumber>
Thanks for reading and Happy coding!