Part 1 : Testing Queue Worker
Using PHP Laravel and Beanstalk on Ubuntu 18.04
Sending a very large amount of emails, synchronizing applications or databases can be time consuming and costly for application to handle, which sooner or later, can make the server run out of resources if this condition continues. One of the solutions that I want to achieve is using an asynchronous tasks for handling these conditions. For that purpose, I use beanstalk
in my Laravel application.
This post is mainly focused to try using beanstalk
on my Laravel application.
In this post, I use a VPS on Vultr, and here’s what we gonna do :
- Install Beanstalk
- Create Laravel application
- Manage the tasks (queue worker) using Supervisor.
This post is based on https://www.vultr.com/docs/managing-laravel-work-queues-with-beanstalk-and-supervisor-on-ubuntu-16-04
Beanstalk
Beanstalk is a fast and simple work queue. It allows you to run time consuming tasks asynchronously, such as sending emails, connecting to external APIs or processing images. By doing so, you will reduce your web app latency. Laravel provides out-of-the-box support for beanstalk
. Here’s the steps for beanstalk
- Installation
sudo apt-get install beanstalkd
- Start the service
sudo systemctl start beanstalkd
sudo systemctl enable beanstalkd
sudo systemctl status beanstalkd
- Testing installation
By default, beanstalk using port 11300, we can test it using telnet.
telnet localhost 11300 // for access the beanstalk
list-tubes // try this command to see list of tasks
This is the result example :
- Exit
quit
Tubes in Beanstalk represent work queues. Beanstalk is composed basically by producers, consumers, jobs and tubes. Producers put jobs into a tube to be consumed (processed) by any number of consumers. Note that both producers and consumers are simply clients of the Beanstalk server and are totally independent of each other. In practical terms this means that by using Beanstalk you may produce your jobs in your PHP application and have it processed in a NodeJS app for example. Luckily, Laravel abstracts all of this and provides us with a very simple API to dispatch and handle jobs, as we will see next.
Laravel
I already have a public project on github, if you don’t want to installing Laravel, just copy my project and continue to the next section.
Well you already know what Laravel is. So, I assume that we already have a Laravel project installed, if you not, follow the Laravel documentation. After you have the project, here’s the steps to use Beanstalk :
- Composer install
apt-get install composer
composer install
- Require php beanstalk client
composer require pda/pheanstalk ~3.0
- Create a sample job
Here I use a built in Laravel job, we will create a job called FindFavoriteOS
, which will get favorite OS data on vultr
php artisan make:job FindFavoriteOS
Then modify FindFavoriteOS
to handle data fetch from vultr API.
In the FindFavoriteOS.php
the result will be printed into a Log in Laravel, it is stored in storage/logs/laravel.log
Testing the Application
If you not following the Laravel creation, you can clone or download my project on https://github.com/aryabawanta/learning-beanstalk-laravel.git
- Env Setting
Copy .env.example
and name it .env
and run this command to setting an app key :
php artisan key:generate
Add or update existing QUEUE_DRIVER
setting into beanstalkd
QUEUE_DRIVER=beanstalkd
Add or change QUEUE_CONNECTION
setting into beanstalkd
QUEUE_CONNECTION=beanstalkd
- Route creation
<?php
Route::get('/', function () {
for ($i = 0; $i < 50; $i++) {
\App\Jobs\FindFavoriteOS::dispatch();
}
return '50 Jobs dispatched!';
});
- Serving application
php artisan serve --host 0.0.0.0 --port 80
- Testing by accessing
http://[vps-ip-address]
- Handle the jobs
Run this script to handle jobs existed in the queue
php artisan queue:work --once
The result should be like this :
- Final result
As mentioned previously, all the job is logged in storage/logs/laravel.log
and can be opened using tail -f storage/logs/laravel.log
Final Thought
The implementation actually is pretty simple, just using pheanstalk
to manage job from PHP and beanstalk
. I will use this experience to create a more complex projects like sending emails, data scrapping, synchronizing, etc. Maybe will create a post about that later on.
Best regards,
I Gusti Ngurah Arya Bawanta