A lightweight, database-driven job queue plugin for CakePHP 5.
Supports asynchronous task execution, retries, error tracking, admin view, and CLI commands.
- Simple job enqueueing:
$queue->createJob('MyTask', [...]);
- CLI workers:
bin/cake run_worker
- Task creation:
bin/cake bake task MyTask
- Error/retry support
- Pluggable access control via static callback
You can install this plugin directly from GitHub using Composer:
"repositories": [
{
"type": "vcs",
"url": "https://github.com/mahankals/CakePHP-QueueWorker"
}
]
composer require mahankals/cakephp-queueworker:dev-main
bin/cake plugin load QueueWorker
$this->addPlugin('QueueWorker');
bin/cake migrations migrate --plugin QueueWorker
In src/Application.php
, bootstrap method
\QueueWorker\Controller\QueueJobsController::$accessPolicy = fn($req) =>
$req->getAttribute('identity')?->get('role') === 'admin';
now queue jobs are available on http://localhost:8765/queue-worker/jobs
This plugin uses task classes located in src/Queue/Task/
to execute background jobs.
Create a file:
src/Queue/Task/MyTaskTask.php
<?php
namespace App\Queue\Task;
class MyTaskTask
{
public function run(array $data): void
{
// Example job logic
echo "Running MyTask with data: " . json_encode($data) . PHP_EOL;
}
}
You can use CLI to generate this:
bin/cake bake task MyTask
In any controller, command, or service:
use QueueWorker\QueueWorker;
$queue = new QueueWorker();
$queue->createJob('MyTask', ['user_id' => 42, 'message' => 'Welcome']);
This stores the job in the database table (queue_jobs) and marks it as queued.
Use the CLI to start processing jobs:
bin/cake queue start
The worker:
- Fetches one job at a time
- Executes the corresponding Task class
- Tracks attempts, errors, and status
export EDITOR=nano && crontab -e
* * * * * cd /var/www && bin/cake queue start --once >> ./logs/queue_start.log 2>&1
Create /etc/supervisor/conf.d/cake_queue_worker.conf
[program:cake_queue_worker]
directory=/var/www/your-app
command=/usr/bin/php bin/cake queue start
autostart=true
autorestart=true
stderr_logfile=/var/www/your-app/logs/worker_error.log
stdout_logfile=/var/www/your-app/logs/worker_output.log
user=www-data
numprocs=1
Enable & Start Supervisor
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start cake_queue_worker
Confirm Worker is Running
ps aux | grep 'queue start'
or
sudo supervisorctl status
Retry all failed jobs:
bin/cake queue retry
Retry a single job:
bin/cake queue retry 42
You can safely terminate queue worker manually:
bin/cake queue stop
You can publish templates for customization:
bin/cake queueworker publish