Adding a Queue Worker in Laravel

Queue in laravel are no different with other queue services, it use easy and common procedures: There is a place to store the queue information, a PHP process at runtime writes tasks, another guard PHP polling process queue information, will perform the required task execution to reach and remove. Because PHP is url-driven synchronous language itself is blocked, so laravel provide a daemon tool to query and execute queue information.

The queue configuration file is stored in app/config/queue.php. In this file you will find connection configurations for each of the queue drivers that are included with the framework, which includes a Beanstalkd, IronMQ, Amazon SQS, Redis, and synchronous (for local use) driver. We can find more information about this from Laravel Official Website.

Queue service need specialized class, as an independent class, they needn't inherit other class. Because that use PHP daemon to call an task execute in queue independent, if you wanna use other class before, also not go wrong.

Queue use is so easy, here is an example

use Queue;
Queue::push('CurlJsonQueue', [
    'url'   => $url,
    'json'  => $json
]);

At here, I put my queue class CurlJsonQueue.php in app/services/ folder, and this foler already register as classmap at autoload in composer.json, the namespace at the top level, so I can directly call it.

<?php

class CurlJsonQueue extends BaseController
{
    public function fire($job, $data)
    {
        $url  = $data['url'];
        $json = $data['json'];

        parent::base_post_curl($url, $json);

        $job->delete();
    }
}

Default method of this class is fire and parameters is $job and $data, now we created an custom task with queue and we can use following command to open daemon:

$ php artisan queue:listen

Find more about queue service configure reference articles:

Production-Ready Beanstalkd with Laravel Queues

0.00 avg. rating (0% score) - 0 votes