Laravel queues and scheduler on fortrabbit

Published

Updated

Reviewedbyfl

⏰

Work in the background.

Laravel queues and the scheduler run as jobs on fortrabbit. Use the database queue driver, add a worker with queue:work and a cron with schedule:run every minute. Both need the Jobs component, SM for the two together.

Queue

fortrabbit offers no Redis, so the database driver is the queue of choice. It uses the MySQL database. Laravel 11 and later ship the jobs table migration; older versions create it:

# Create a migration for the jobs table locally
php artisan queue:table

# Apply the migration locally
$ php artisan migrate

# Add, commit and push the migration file
$ git add .
$ git commit -m "queue migration"
$ git push

# Run the migration on the App via SSH remote execution
$ ssh {{app-env-id}}@ssh.{{region}}.frbit.app php artisan migrate --force
shell

Set QUEUE_CONNECTION=database in the ENV vars. Then add a worker with this command:

php artisan queue:work --sleep=3 --tries=3 --max-time=3600
shell

Use queue:work, not queue:listen: queue:listen boots Laravel for every job, queue:work boots it once and keeps running. --max-time restarts the worker every hour, which frees memory.

The worker and the scheduler cron are two jobs. Jobs XS holds one job, so both need Jobs SM or larger.

Scheduler

Laravel's scheduler lets you define recurring tasks in code instead of managing many separate crontab entries. Add a single cron job that runs every minute and let Laravel decide what's actually due:

* * * * * php artisan schedule:run
shell

See job usage for adding the cron job in the dashboard.

Two things to watch:

  • Avoid ->runInBackground(), it will not work at all with schedule:run. We have to keep the platform performant for everyone, so we kill any process still running after the job that started it exits - see "do not detach". If you have to use runInBackground(), then it works better with a non stop job that calls artisan schedule:work instead, because that parent job stays running.
  • Use ->withoutOverlapping() on tasks that might run long. It stops Laravel from starting a new instance of a task while a previous run is still in progress, keeping schedule:run itself fast. Without it, slow tasks pile up invocation after invocation and can hit the platform's own limit on concurrent instances of the same cron job - see "overlapping jobs".

Written by humans and AI in collaboration.

AI use & editorial processEdit on GitHub ↗