DEV Community

Dendi Handian
Dendi Handian

Posted on • Updated on

PHP-Worker in Laradock

Laravel has queue functionality to enable the app to do asynchronous processes. If we use only laradock's workspace container to perform php artisan queue:work, you might want to open a new workspace bash session to do something else. We need something/service that runs the queue jobs in the background to make our life easier. Of course, Laradock has it and it's called php-worker

Prerequisites

If you have no idea what Laradock is, then please play with it first and set up your laravel application in it by following this guide and then try setup mailhog server, how to configure it and set up a basic artisan command to send a mail here. These two guides will help you (also me to cut the current post length) to get the basic needed to follow this post.

Create a worker configuration for your project

If you follow my first post in the series about the laradock introduction, then we can use the same directories example:

- projects
  |_ my-awesome-laravel-app 
  |_ laradock
Enter fullscreen mode Exit fullscreen mode

further inside the laradock directory, you will find this file php-worker\supervisord.d\laravel-worker.conf.example. Duplicate this file into my-awesome-laravel-app-worker.conf (the name could be different, as long as you end it with .conf).

- projects
  |_ my-awesome-laravel-app 
  |_ laradock
     |_ php-worker
        |_ supervisord.d
           |_ laravel-worker.conf.example
           |_ my-awesome-laravel-app-worker.conf
Enter fullscreen mode Exit fullscreen mode

Then modify the my-awesome-laravel-app-worker.conf content into:

[program:my-awesome-laravel-app-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/my-awesome-laravel-app/artisan queue:work --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
numprocs=8
user=laradock
redirect_stderr=true
Enter fullscreen mode Exit fullscreen mode

And that's it for the worker configuration.

Using the database queue connection

For a simple demonstration, we can use database for queue connection. So set the value in the laravel project .env like this:

...

QUEUE_CONNECTION=database

...
Enter fullscreen mode Exit fullscreen mode

If you haven't created the jobs table, go ahead enter the laradock's workspace bash and execute php artisan queue:table and php artisan migrate to create it.

Now execute the simple mail sender command php artisan example:send-mail as it's instructed in my post about mailhog. If it succeeds, you should see the job enqueued inside the jobs table in your database.

Run the PHP-Worker container

Say no more, run this command inside your laradock directory:

docker-compose up -d php-worker
Enter fullscreen mode Exit fullscreen mode

Now the queue should do the magic, you should see the email in the mailhog inbox.

Have fun experimenting with php-worker in Laradock

laravel version used: 6.0 LTS
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
bawa_geek profile image
Lakh Bawa

Hi Dendi, I can see, the job is added inside the jobs table, but why it's not being executed?

Collapse
 
dendihandian profile image
Dendi Handian

See the logs of php-worker container, maybe something went wrong.

Collapse
 
bawa_geek profile image
Lakh Bawa

hmm Thanks, I had to reload the supervisors inside the worker container

Collapse
 
williamsduarte profile image
William Duarte

Thank You!

Collapse
 
dendihandian profile image
Dendi Handian

Have fun!