I'm using Laravel sail. I want to use supervisor and cronjob. But I don't understand how I can configure these things with sail.
I can't find any examples on how to do this.
I'm using Laravel sail. I want to use supervisor and cronjob. But I don't understand how I can configure these things with sail.
I can't find any examples on how to do this.
Because sail uses a Ubuntu container you can follow these instructions: https://serverspace.io/support/help/automate-tasks-with-cron-ubuntu-20-04/
Typically they are stored in /var/spool/cron/crontabs so you might want to create a volume to persist them (otherwise they will be removed on the next restart of your container).
You can connect to your container with docker-compose exec laravel.test bash I believe.
I solved my problem another way. I added this codes to sail's supervisor.conf file.
process_name=%(program_name)s
command=/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan horizon
user=sail
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/www/html/horizon.log
stopwaitsecs=3600
[program:cron-job]
process_name=%(program_name)s
command=crond -f
user=root ; crond should be started as root at all times
autostart=true
autorestart=true
numprocs=1
redirect_stderr=false
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Maybe it is not best solution but it's worked.
To setup Supervisor for Laravel queues, this is what I tried within the Docker container for Laravel Sail.
To enter the container:
docker-compose exec laravel.test bash
Run inside the container:
apt update
apt install nano
cd /etc/supervisor/conf.d
touch laravel-worker.conf
nano laravel-worker.conf
Paste in:
[supervisord]
nodaemon=false
user=root
logfile=/var/log/supervisor/laravel-worker.log
pidfile=/var/run/laravel-worker.pid
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=sail
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/storage/logs/worker.log
stopwaitsecs=3600
startsecs=0
[supervisorctl]
Save with Nano:
ctrl + o
ctrl + x
To run the queue workers:
supervisord -c /etc/supervisor/conf.d/laravel-worker.conf
To restart the queue workers if your application code changes:
service supervisor restart
Remember, queue workers, are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers. In addition, remember that any static state created or modified by your application will not be automatically reset between jobs. Queues - Laravel - Running the Queue Worker
Note: if the Docker container is restarted, you'll need to manually run the supervisord command again.
From digging into Laravel Sail a bit, it looks like you shouldn't change /etc/supervisor/conf.d/supervisord.conf since it's so tied into Docker. For example, if the supervisor process is killed, the container stops i.e. the webserver stops and you're logged out. This makes it more difficult to change the queue worker configuration since it requires restarting the process.
See sail/runtimes/8.0/start-container.
Final thoughts: Overall, if I had to start over again knowing what I know now, I might have configured Supervisor (and cron) outside of the Docker container on the host system. It's less complicated and probably less prone to breaking in the future in case of Sail updates.
Laravel Sail is designed as a development environment and as such I believe Laravel have purposefully omitted automatic running of cron jobs and queue workers in favour of giving developers control over running them using the appropriate artisan commands.
To use the Laravel Scheduler inside Sail you can simply run:
sail artisan schedule:work
This command will run in the foreground and invoke the scheduler every minute until you terminate the command.
Similarly, if you want to run the Queue worker inside Sail you can run the following:
sail artisan queue:work
Running these commands locally gives the developer complete control rather than having tasks and queues running automatically inside the container.
I would strongly recommend sticking with running these commands manually so that your app doesn't unexpectedly fire out a million emails that are sat in the queue or run other unintended jobs or tasks!
If, against this advice, you did want to have the Laravel scheduler run automatically inside the Sail docker container then the steps would be something like this:
sail artisan sail:publishRUN apt-get install -y cron/etc/cron.d/laravelRUN /usr/sbin/service cron startsail build --no-cacheSimilarly, after publishing the Sail assets you could also edit the provided supervisord.conf to automatically run queues using supervisord.