renoki-co / laravel-helm-demo

Example of a horizontally-scaled Laravel 8 app that runs on Kubernetes with NGINX Ingress Controller.
https://github.com/renoki-co/charts/tree/master/charts/laravel
62 stars 16 forks source link

[feature] Laravel Task Scheduling #31

Closed korridor closed 1 year ago

korridor commented 2 years ago

I started using your helm charts a while ago and I really like them. I was wondering how you are using the Laravel task scheduler with these charts? My plan was to modify the worker chart a bit by adding a CronJob resource, but I would love to have your input.

rennokki commented 2 years ago

Cronjob would be the only accepted way but it has some issues. This is related to the speed of deployment.

In case the specific Kubernetes node does not have the image, it requires it to be downloaded.

For slow networks (to download the image from registry), if it takes > 60s, it is a pain. So the cost (in time) for having a CronJob and running the container each minute is pretty high and might overlap with other cronjobs. However, this can be easily be mitigated if somehow we could achieve a way to have a cronjob up and running for a long time. This way, we don't have to spin up a pod with the needed container and run scripts each minute.

My choice to go for such things would be spatie/laravel-cronless-schedule and have it running alongside the app. The deploy.sh script will be needed to run only once (at deploy), not each time (like in a K8s CronJob), and will run instantly. Of course, you may still use all the features that Task Schedule offers you, like running in the background or avoiding overlapping. For K8s CronJob, you can't use the run-in-background functionality since it forks the process and the container will be stopped when the task hits exit code 0. :)

For example, you may have a container (not an init container) that would run this command beside it:

php artisan schedule:run-cronless --command="schedule:run" --frequency=60
rennokki commented 2 years ago

This might be a good idea to implement. ⚡

rennokki commented 2 years ago

Just found out that schedule:work is another command that can be long-lived. 🤔