The logs for the cron instance shows Preparing to run: /entrypoint cron -f as root and then nothing happens. No jobs are ever scheduled. I know @fideloper-fly has worked on some issues with this. Any ideas?
“No jobs are ever scheduled” implied that your cron tasks are creating queue jobs - is that right?
You can use fly ssh console -s and select an instance to SSH into it. They aren’t necessarily named obviously but in my experience, the 2nd will be cron, and the third instance will be worker.
You can run commands like ps aux | grep cron to see that CRON is a running process or ps aux | grep artisan to see if the queue worker is running.
Then perhaps head to /var/www/html and run php artisan schedule:run manually to see if it works.
Also be sure to check fly logs to see if there’s any error output.
Do you have env var setup for your queue connection? The default is sync, so they wouldn’t actually get run in a queue but whenever a job is dispatched (during the schedule:run command, if my guess is correct about the scheduled jobs firing jobs into the queue).
Be sure to check out the cron/queue docs to see if you missed anything, such as adjusting the php artisan queue:work command to work on the queue connection you need it to run on: Cron and Queues · Fly Docs
The evidence is that there are no jobs being scheduled.
My queue instance logs sit at INFO Processing jobs from the [default] queue. unless I manually use fly ssh console -s to SSH into the cron instance, go to /var/www/html and run php artisan schedule:run manually. Then jobs get scheduled and processed, as shown by the queue instance’s logs.
My env var is set properly to database which is what I use for my jobs.
I read over the “Cron and Queues” Fly Docs but I don’t see anything that I missed.
Wait a minute or 2, and then check /var/www/html/storage/logs/cron.log, you should see some output there. In a fresh Laravel app, I see:
root@3982c846:~# cat /var/www/html/storage/logs/cron.log
INFO No scheduled commands are ready to run.
INFO No scheduled commands are ready to run.
INFO No scheduled commands are ready to run.
Quick tip: Once you SSH in, run bash to start bash shell, which is a little friendlier/easier than the default sh
Interestingly though, there is nothing being added to the jobs table in the database and nothing is being logged by the worker, meaning jobs aren’t being dispatched to the right place. Note that this all works fine locally.
Running php artisan tinker and then running env("QUEUE_CONNECTION") returns "database", which is correct. It seems the cron instance is missing something…
I suspect other important env vars or secrets are not being picked up.
Here’s something annoying I haven’t found a great work around for yet:
cron doesn’t let Laravel read the VM environment variables that Fly sets.
However, the setup should be running artisan config:cache when the VM boots up, so your config should be in the confit cache.
Is your setup using env() directly in code instead of config()? (it’s okay and expected if config files use env(), but using env() elsewhere can lead to issues).
I SSH’d into the cron instance with fly ssh console -s, went to the app directory, and ran artisan config:cache. And a minute later my jobs started being scheduled and ran!
For some reason, this was clearly not being run when the VM booted. I’m not sure if the issue was with the Dockerfile or the entrypoint.sh script in the docker directory.
I created a new Laravel Fly app to see what was different, following the tutorial Fly.io Docs: Run a Laravel App and noticed the docker directory has been changed to .fly, and the scripts have been changed.
Long story short, I replaced my Dockerfile and docker directory with the Dockerfile and .fly directory from the tutorial and… everything started working!
I have gotten around this by running schedule:work in another process. It’s under a section called “Running scheduler locally” but it’s working fine so far.
I wrote a guide at Crontab with Supercronic · Fly Docs that shows how to get cron running in a Fly container. I think it’s comprehensive enough where you won’t have to try to avoid it
I haven’t played with this yet but if you get that working, send me a ping - I’d love to update the Laravel stuff with that if it solves the env issue.
Yep! You’re right, I think I feel asleep at the wheel. I’m going to update the docs. Thanks for running through them and sorry for sending you down a rabbit hole.
To resolve the issue with your Laravel cron job not executing, ensure your setup follows these steps:
1. Verify SSH Access: Ensure your hosting provider grants SSH access, as deploying Laravel with tools like FileZilla is not recommended due to dependency management issues.
2. Correct Cron Configuration:
fly.toml: Make sure your cron command is correctly defined.
Dockerfile: Ensure the cron setup is properly integrated.
Here’s a sample Dockerfile section for setting up cron:
Thank you for sharing the steps above, they’re very helpful, specially the common issues part. Also! Here at Fly, we already have a cron daemon pre-configured to run the scheduler command, so running the scheduler on a Laravel Fly app is as quick as declaring the cron daemon as a Fly process in its fly.toml file: