A queue is a very important and interesting feature of Lumen, You can run your tasks(background process/cron tasks) using queue without fail. You need to push tasks into the queue and the rest of the work will do by the worker.
Sometimes, We need to run a task one by one or partially which will process some data and return the final processed data as a response. You can use a queue in Lumen for periodically running tasks, cron tasks and background scripts tasks.
Normally, We are using CRON job to call task periodically but some time that task failed due to execution time, server response time 500, or any other server issue, next time that task will run on the scheduled time, again and again that process will continue but failed process will not retry if the scheduled task was failed.
I am using the following module for the Lumen queue
You can also check other recommended tutorials of Lumen/Laravel,
sudo yum install beanstalkd sudo /usr/bin/beanstalkd -l 127.0.0.1 -p 11300 sudo service beanstalkd start(stop/restart)
Step 1: Added beanstalkd dependency in composer.json
, I am using beanstalkd 3.0+."pda/pheanstalk": "~3.0"
Now run update composer from the command line,D:\lumen_code> composer update
Step 2: Added Queue configuration in config/queue.php
file, If you do not have a cache folder then you need to create a Config folder in the root of the lumen application(ex. – lumen_code\Config
) and copy queue.php
file from vendor\laravel\lumen-framework\config
and paste into lumen_code/config
folder, so the new file location would be lumen_code/config/queue.php. You need update queue.php as like the below configuration.
'default' => 'beanstalkd', 'beanstalkd' => [ 'driver' => 'beanstalkd', 'host' => '127.0.0.1', 'queue' => 'default', 'ttr' => 3600, ],
We have changed the default queue driver to beanstalkd queue driver and updated beanstalkd driver configuration.
Step 3: Now I have created a new jobs file testQueue.php
in jobs/
folder. We need to add the below code into testQueue.php
file,
queue = $data['queue']; } /** * Execute the job. * * @param * @return void */ public function handle() { Log::info('Hello! Queue job '.$this->queue.' is run at start time - '.microtime(true)); } }
I have created parameterized queue method that will help to run tasks with parameters. I have just created a sample queue method which will print messages into LOG file, You can see into storage/Log/lumen.log
file.
Step 4: We have successfully created jobs and created a new method that will push tasks into the queue, I have created test_queue() method into sampleController.php file like the below code which will push tasks into the queue.
use Illuminate\Support\Facades\Queue; use App\Jobs\testQueue; public function test_queue() { for($i=0; $i<=10; $i++){ Queue::push(new testQueue(array('queue' => $i))); echo "successfully push"; } }
I have included testQueue
which is earlier created for queue jobs.
Artisan is the name of the command-line interface included with Laravel/Lumen. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.
php artisan queue:listen &
php artisan queue:work
We have learned about Laravel/Lumen queue using Beanstalk. We have created a controller method that will push task into laravel queue and run the queued task using PHP Artisan from the command line, next time I will let you know how to schedule queued tasks background using the supervisor module.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
thanks for sharing, but i have a problem when i run php artisan queue:listen & command, i get no response, and when i cheched the lumen.log file i found this error : lumen.ERROR: exception 'PheanstalkExceptionConnectionException' with message 'Socket error 10061. Can you help me please
I think port is blocked,