loading...

PHP-Worker in Laradock

dendihandian profile image Dendi Handian ・2 min read

Explore Laradock Services (8 Part Series)

1) Laradock - A PHP Developer's best friend. 2) Redis in Laradock 3 ... 6 3) Mailhog in Laradock 4) PHP-Worker in Laradock 5) PHP-Worker for Scheduler in Laradock 6) RabbitMQ in Laradock 7) Elasticsearch in Laradock 8) HTTPS Local Development using Caddy 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 basics 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

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 can 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

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

And that's it for the worker configuration.

Using the database queue connection

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

...

QUEUE_CONNECTION=database

...

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

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

Explore Laradock Services (8 Part Series)

1) Laradock - A PHP Developer's best friend. 2) Redis in Laradock 3 ... 6 3) Mailhog in Laradock 4) PHP-Worker in Laradock 5) PHP-Worker for Scheduler in Laradock 6) RabbitMQ in Laradock 7) Elasticsearch in Laradock 8) HTTPS Local Development using Caddy in Laradock

Posted on by:

dendihandian profile

Dendi Handian

@dendihandian

Please teach me how to prettify database schema, implement any design patterns and do TDD in Drupal / Wordpress and how to make them not slow as ever.

Discussion

markdown guide