The title is pretty self explanatory. We know how to use Queues inside Laravel. But Laravel components are also usable outside Laravel itself. Today we’re going to see how to implement our own workers on top of the “Illuminate\Queue” package.
Installing the required components:
What do we need?
In our example, we shall be using “Beanstalk” for message queueing. So please make sure you have it installed. This is how to install it on OS X using homebrew:
1 |
brew install beanstalk |
Then we need to install these following PHP packages. We can use Composer to install them from the Packagist repository.
- “illuminate/queue” – the queue component
- “pda/pheanstalk” – we shall use the beanstalk driver, so we need the beanstalk client
- “illuminate/encryption” – some drivers need the encryption class
So this how our composer.json looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{ "name": "masnun/testqueue", "authors": [ { "name": "Abu Ashraf Masnun", "email": "masnun@gmail.com" } ], "require": { "illuminate/queue": "5.0.28", "pda/pheanstalk": "~3.0", "illuminate/encryption": "5.0.28" } } |
Now we need to make sure that all the required packages are installed.
1 |
composer install |
Let’s write the codes:
We first create a file named “loader.php” which will contain the common part. In this file, we shall construct a Queue object and add connection to Beanstalk. Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php require_once 'vendor/autoload.php'; use Illuminate\Queue\Capsule\Manager as Queue; $queue = new Queue; // Some drivers need it $queue->getContainer()->bind('encrypter', function() { return new Illuminate\Encryption\Encrypter('foobar'); }); // Configure the connection to Beanstalk // Note: The second parameter is the connection name // We also define a queue name // We will need these two names later $queue->addConnection([ 'driver' => 'beanstalkd', 'host' => 'localhost', 'queue' => 'default', ], 'default'); |
Let’s say we shall have some emailing tasks which would send email to our users. Let’s create a new file named “handlers.php” and create a class named “SendEmail”. This class must have a “fire()” method which can take the Job instance and data as arguments. Here’s sample codes:
1 2 3 4 5 6 7 8 9 10 |
<?php class SendEmail { public function fire($job, $data) { echo "Sending email to: {$data['email']}" . PHP_EOL; } } |
OK, we just created our awesome email sending class. Our task handler is ready. Now we need to create workers which would keep running and handle tasks from the queue. We create a sample worker named “worker.php”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php require_once 'loader.php'; require_once 'handlers.php'; use Illuminate\Queue\Worker; $worker = new Worker($queue->getQueueManager(), null, null); // Run indefinitely while (true) { // Parameters: // 'default' - connection name // 'default' - queue name // delay // time before retries // max number of tries $worker->pop('default', 'default', 0, 3, 1); } |
We have handlers written and workers ready for some action. Let’s launch the worker:
1 |
php worker.php |
The workers would fire up and keep listening to the queue. Whenever a new task would arrive, it would take it off the queue and process it. So now we need to queue some tasks. Let’s do that now:
1 2 3 4 5 6 7 8 9 10 11 |
<?php use Illuminate\Queue\Capsule\Manager as Queue; require_once 'loader.php'; // Make this Capsule instance available globally via static methods... (optional) $queue->setAsGlobal(); Queue::push('SendEmail', array('email' => 'masnun@gmail.com')); Queue::push('SendEmail', array('email' => 'steve@apple.com')); Queue::push('SendEmail', array('email' => 'otwell@laravel.com')); |
Now if you look at the terminal window where you ran the worker, you would notice the output from the job handlers.
6 replies on “Using Laravel Queues Standalone (Outside Laravel)”
Hi, good job for you.
What is the solution if i wanna set the driver with redis or rabbitmq or other any driver?
Thanks
Dear MASNUN,
I want to create queue to call a service to multiple site client to update my plugin on site client. Queue write in zend framework 2.
I’m try step by step above, however i have error:
PHP Fatal error: Uncaught Pheanstalk\Exception\ConnectionException: Socket error 10061: No connection could be made because the target machine actively refused it.
(connecting to 127.0.0.1:11300) in D:\xampp7\htdocs\queue3\vendor\pda\pheanstalk\src\Socket\NativeSocket.php:45
Please help me.
I understand this a year old question but, as the error it self mentions it, your machine ( your Beanstalk Server ) is not listening on port 11300, which is the default port for beanstalk, if any one else faces the same problem, try restarting beanstalk service.
For debian based machines you can use
sudo service beanstalkd restart
Time has passed since 2015 and in 2017 people have tried this, see here:
https://laracasts.com/discuss/channels/laravel/running-laravel-queue-standalone-54
Hello can you update this code it’s not working
Can you update your code ? it’s not working.