How I actually run the Craft queue
On this page
Out of the box, Craft runs its queue from the control panel. When an admin loads a CP page, a bit of JavaScript fires queue/run over Ajax and the pending jobs execute inside that web request. The setting is runQueueAutomatically and it defaults to true. For a brochure site with one editor that is fine. For the hospital system site I have worked on at the agency since 2017 it was never fine, and the history of how we run that queue is most of what I know about Craft queues.
Why the web runner stopped being enough
That site pushes a lot into the queue: search indexing, syncs to a listings platform, provider change notifications, nightly exports to S3 for other teams. The jobs queued when someone saves an entry are the easy ones. The jobs that take an hour are the problem, because a job running inside a web request lives inside PHP's execution limit and the browser tab's patience. Jobs died halfway and were picked up again by whichever admin loaded a page next.
In June 2020 a teammate turned runQueueAutomatically off on staging and production, with the comment "Run queued tasks via CLI tool instead of through web requests", and the queue moved to a cron calling craft queue/run. Three months later the queue component's ttr went from 3,600 to 14,400 seconds. A job still running when its time-to-reserve expires gets handed to the next worker as if it had failed, and we had jobs that legitimately took longer than an hour.
Then in March 2021 came the lesson about payloads. A sync job was being pushed with the whole Entry as a property. Craft serializes job properties into the queue table, and a large entry with every field value attached is a very large row. The branch was named fix/large-record-job-serialization and the fix was to push three scalars and re-fetch inside the job:
$queue->push(new UpsertLargeData([
'id' => $event->sender->id,
'url' => $event->sender->url,
'title' => $event->sender->title,
]));
Moving it into the scheduler
In early 2023 the site's cron jobs came out of the CI tool and into the repo. The agency maintains a Yii port of Laravel's scheduler, one cron line runs it every minute, and config/schedule.php describes everything else: cache clears, garbage collection, the nightly exports. The queue became one more entry in that file. This is what it looks like today:
$schedule->command('queue/run')
->description('Run Queue')
->skip(fn () => Craft::$app->getQueue()->getHasReservedJobs() > 0)
->everyMinute();
The skip() is the whole trick. A cron fires every minute whether or not last minute's runner finished, and queue/run works through everything pending before it exits. Without the guard you get two runners on long jobs, then three. The guard asks whether any job is currently reserved and stays out of the way if so. It went in with the scheduler in February, went missing at some point, and was put back in April under the message "Force queue to run a single worker at a time". For a while the entry also carried ->onOneServer(), because the site runs as several identical containers and each one has the same crontab.
That is the recipe: one runner, started every minute, that declines to start if one is already going.
Make failures loud
My own contribution to this is small, and it is the one I would repeat on any site. The video platform integration for provider bios runs inside queue jobs, and its service methods did what a lot of integration code does: log the problem and return false. From the queue's point of view that job succeeded. Nothing showed in the CP queue widget, nothing reached New Relic, and the only trace was a line in a log file nobody reads. In December 2023 I changed those paths to throw:
if (! $entry) {
BrightcoveModule::$instance->logger->add(
'error',
'Error Removing YouTube tag from Brightcove Record',
"Could not locate the entry for provider with NPI $npi"
);
throw new \Exception("Could not locate the entry for provider with NPI $npi");
}
A thrown exception fails the job, the queue keeps the message where an admin can see it, and New Relic records it. The log line is still there for the detail. The difference is that someone finds out.
The short version
Turn runQueueAutomatically off on any environment that matters. We did it again last month on a nonprofit's Craft site, in the production block of general.php, for the same reasons.
Run queue/run from a scheduler every minute with a guard that skips when a job is reserved, or run queue/listen as a daemon if your host gives you one. Either way the queue runs because a process exists, not because an editor happened to log in.
Set ttr longer than your longest job, or the job will run twice.
Push ids into jobs, not elements. Re-fetch inside execute().
Throw inside jobs. A job that returns false on failure is a job that reports success.