schmittjoh / JMSJobQueueBundle

Run and Schedule Symfony Console Commands as Background Jobs
http://jmsyst.com/bundles/JMSJobQueueBundle
335 stars 254 forks source link

Running jms-job-queue:run without supervisord #205

Closed vellmur closed 6 years ago

vellmur commented 6 years ago

Thanks for this awesome bundle!

Right now I am using it on a shared hosting and I don`t have access to a supervisord. So my question is: "Can I somehow use it with cron tasks?". Will it work if I add to a shared hosting cron task that will active a bundle for a whole day? Cron will start bundle once a day and in a parameter I want to set max time for 24h - "php bin/console jms-job-queue:run --max-runtime=86400".

dnagy commented 6 years ago

It might work, but in case you encounter some kind of error, you might end up without job queue running. Ideally the job queue will work through the day, but sometimes you can have unforseen problems, simple as lock timeout which might cause the queue to stop working. I'd suggest to write a small bash script to see if there's a job queue running and if it's not, then it would start. And make a cron job which starts every minute. I guess this would be the closest thing to supervisord. And I'd still suggest to leave the --max-runtime on the default value.

Sybio commented 6 years ago

As @dnagy said, I added a cron executing a custom bash script every minute.

To prevent duplicate console commands running at the same time, you need to check if jms-job-queue is currently running, and if not, execute once.

Here my snippet :

my_project/job_queue_launch.sh :

#!/bin/sh

if [ -z `pgrep -f -x "/opt/plesk/php/7.1/bin/php bin/console jms-job-queue:run --env=prod"` ]
then
        /opt/plesk/php/7.1/bin/php bin/console jms-job-queue:run --env=prod
fi
exit

Don't forget to give permission :

chmod +x my_project/job_queue_launch.sh

Then add your cron :

crontab -e
* * * * * /path/to/my_project/job_queue_launch.sh

And job is done !

dnagy commented 6 years ago

That's really helpful @Sybio, thanks for sharing!

schmittjoh commented 6 years ago

I would recommend shorter intervals for starting the scheduling command like every minute for example as that reduces the potential offline time significantly.

Otherwise closing this here as there is nothing specific to do.