Starting the Laravel scheduler / cron from serversideup/docker-php image

Hi @fideloper-fly I have deployed my app with the new serversideup/php:8.1-fpm-nginx image.

I can confirm the contents for the cron job were copied correctly:

webuser@f4f201cb:/$ cat /etc/cron.d/laravel

MAILTO=""
* * * * * webuser /usr/bin/php /var/www/html/artisan schedule:run

I can confirm running service cron status gives cron is not running

Reading the info on the github page, the S6 Overlay is designed to run and monitor services.

Do I need to add a cron service to a docker-composer.yml for this to start ?

Thanks.

Hi!

Sorry you’re catching us mid-transition on using this style of container thing.

I’ve gotten most of the documentation updated (but not all of it) in a branch that’s not merged to the docs yet.

For now, check out this post on how to set it up:

  1. The recommended workflow sets you up to essentially run multiple instances of your application
  2. But you can also do this within one container, making use of S6

Check out the post I linked, and a few under it that go into trying to use it as a single container.

I’ll be verifying the single-container (vm) approach documenting that as well as the recommended use of [processes] within the fly.toml config.

Hi Chris, I have followed the recommend way, but now I need to adjust the VM scale settings. I tried changing the VM count setting as per flyio docs but this has only changed the “app” pool, do you know how to update scale count for ‘cron’ and ‘worker’ ?

      VM Memory: 2 GB
          Count: app=8 cron=1 worker=1 
 Max Per Region: app=0 cron=0 worker=0 

Also the worker / cron region deployment is “Stuck” on my primary region.

I want this balanced between my two regions (dfw and syd) :::

8017f39c	app    	13     	dfw   	run    	running	1 total, 1 passing	0       	1h7m ago 	
af8e20de	app    	13     	dfw   	run    	running	1 total, 1 passing	0       	1h7m ago 	
cedb0470	app    	13     	dfw   	run    	running	1 total, 1 passing	0       	1h7m ago 	
9652fa35	app    	13     	dfw   	run    	running	1 total, 1 passing	0       	1h7m ago 	
6f271410	app    	13     	dfw   	run    	running	1 total, 1 passing	0       	1h7m ago 	
1aceb95e	app    	13     	dfw   	run    	running	1 total, 1 passing	0       	1h7m ago 	
1e43e66b	app    	13     	dfw   	run    	running	1 total, 1 passing	0       	1h19m ago	
21ecdc27	cron   	13     	dfw   	run    	running	                  	0       	1h19m ago	
9afe1486	app    	13     	dfw   	run    	running	1 total, 1 passing	0       	1h19m ago	
e8a5dd0e	worker 	13     	dfw   	run    	running	                  	0       	1h19m ago

It’s semi-hidden in the fly.toml reference for processes, but there is a section per-process commands that shows some examples:

Thanks Chris, got the scheduler and 1 queue worker running !

This was achieved by setting the VM to a lower spec for the cron and queue VMs :::
fly scale vm shared-cpu-1x --group worker
and
fly scale vm shared-cpu-1x --group cron

The VMs all instantiated on my primary region at first, so this solve that :::
fly scale count --max-per-region=1 cron=2 worker=2

Then I noticed my app VMs were only instanting in my primary region (dfw) so…
fly scale count 2 --max-per-region=1

However the max-per-region doesn’t seem to be set for my app VMs…

VM Size: dedicated-cpu-1x
VM Memory: 2 GB
Count: app=2 cron=2 worker=2 
Max Per Region: app=0 cron=1 worker=1

and my app will only launch in my primary region now…

Instances
ID      	PROCESS	VERSION	REGION	DESIRED	STATUS 	HEALTH CHECKS     	RESTARTS	CREATED    
3816548c	app    	24     	dfw   	run    	running	1 total, 1 passing	0       	10m42s ago	
e3eb7e4a	worker 	24     	dfw   	run    	running	                  	0       	13m54s ago	
33f91dec	cron   	24     	syd   	run    	running	                  	0       	13m54s ago	
02e20f47	cron   	24     	dfw   	run    	running	                  	0       	13m54s ago	
bc275f96	worker 	24     	syd   	run    	running	                  	0       	13m54s ago	
9afe1486	app    	24     	dfw   	run    	running	1 total, 1 passing	0       	3h30m ago 

Hi @fideloper-fly I have moved to your S6 approach.

Here is my implementation, can you comment ?

I created queue and cron files in the docker folder as you suggested in this post

my copy logic :::

COPY . /var/www/html

RUN composer install --optimize-autoloader --no-dev \
    && mkdir -p storage/logs \
    && php artisan optimize:clear \
    && chown -R webuser:webgroup /var/www/html \
    && sed -i 's/protected \$proxies/protected \$proxies = "*"/g' app/Http/Middleware/TrustProxies.php \
    && echo "MAILTO=\"\"\n* * * * * webuser /usr/bin/php /var/www/html/artisan schedule:run" > /etc/cron.d/laravel \
    && rm -rf /etc/cont-init.d/* \
    && cp docker/queue /etc/services.d/queue \
    && cp docker/cron /etc/services.d/cron \
    && chmod +x /etc/services.d/queue \
    && chmod +x /etc/services.d/cron \
    && cp docker/nginx-websockets.conf /etc/nginx/conf.d/websockets.conf \
    && cp docker/nginx-default /etc/nginx/sites-available/default \
    && cp docker/entrypoint.sh /entrypoint \
    && chmod +x /entrypoint

Sanity check on server:

webuser@5c3aac95:/etc/services.d$ ls -l
total 16
-rwxr-xr-x 1 root root   38 Aug 17 05:32 cron
drwxr-xr-x 2 root root 4096 Aug 16 08:13 nginx
drwxr-xr-x 2 root root 4096 Aug 16 08:12 php-fpm
-rwxr-xr-x 1 root root   87 Aug 17 05:32 queue

I don’t observe any log messages pertaining to a running queue nor any evidence of my test jobs have run.

if it helps :::

webuser@42917438:~/storage/app$ ps -eaf | grep s6
root       515     1  0 06:37 ?        00:00:00 s6-svscan -t0 /var/run/s6/services
root       571   515  0 06:37 ?        00:00:00 s6-supervise s6-fdholderd
root       725   515  0 06:37 ?        00:00:00 s6-supervise nginx
root       726   515  0 06:37 ?        00:00:00 s6-supervise php-fpm
webuser    763   754  0 06:57 pts/0    00:00:00 grep s6

and additionally if it helps…

webuser@42917438:/etc/cron.d$ service cron status
 * cron is not running

I think I see what’s up there!

:one: Note that the items in /etc/services.d are directories, not files. The directories nginx and php-fpm contain a script named run.

:two: In your case, you should create directory queue and cron and within each directory, have a script named run.

:three: This means your Dockefile commands can use with mv to move those directories or cp -r (with the -r flag!) as needed with copying directories.

:red_circle: Lastly, don’t forget I also had a mistake in the original post, the queue:work command should be /var/www/html/artisan, not /var/www/html artisan (no space!).


From the linked post, to show what I mean exactly because I’m feeling pedantic apparently :joy::

Thanks, yep all working!

Can you point me in right direction to increase queue workers ? This is my old supervisor file :::


[program:laravel-queue]
user=app
numprocs=4
autostart=true
autorestart=true
redirect_stderr=true
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --sleep=3 --tries=3 --backoff=3 --max-time=3600
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:timezone-queue]
user=app
numprocs=1
autostart=true
autorestart=true
redirect_stderr=true
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --queue=timezone --sleep=3 --tries=3 --backoff=3 --max-time=3600
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

I have the same question to be honest!

Right now scaling that out to the number of workers (numprocs) you want is the way to go.

That means scaling more app instances instead of running multiple workers per app instance.

I have multiple queues running with the not so elegant addition to the Dockerfile :::

    && cp -r docker/queue /etc/services.d/queue1/run \
    && cp -r docker/queue /etc/services.d/queue2/run \
    && cp -r docker/queue /etc/services.d/queue3/run \
    && cp -r docker/queue /etc/services.d/queue4/run \
    && cp -r docker/timezonequeue /etc/services.d/timezonequeue/run \

contents of the queue and cron files if anyone interested:::

queue

#!/usr/bin/with-contenv bash

su webuser

/usr/bin/php /var/www/html/artisan queue:work --sleep=3 --tries=3 --backoff=3 --max-time=3600

timezonequeue

#!/usr/bin/with-contenv bash

su webuser

/usr/bin/php /var/www/html/artisan queue:work --queue=timezone --sleep=3 --tries=3 --backoff=3 --max-time=3600

cron

#!/usr/bin/with-contenv bash

cron -f
2 Likes