Queues for the CodeIgniter 4 framework with support for Database and Symfony Messenger backends.
Note
A queue system is typically used to handle resource-intensive or time-consuming tasks (e.g., image processing, sending emails) that are to be run in the background. It can also be a way to postpone certain activities that are to be executed automatically later.
Install via composer:
composer require esoftdream/queueMigrate your database (if using the database handler):
php spark migrate --allPublish the configuration file to your app/Config directory:
php spark queue:publishThis will create app/Config/Queue.php. You can then customize your queue settings, such as the default handler and job handlers.
Generate a new job class:
php spark queue:job SendEmailThis will create app/Jobs/SendEmail.php. Open it and implement the process() method:
<?php
namespace App\Jobs;
use Esoftdream\Queue\BaseJob;
class SendEmail extends BaseJob
{
public function process(): void
{
$recipient = $this->data['email'];
$subject = $this->data['subject'];
// Your logic to send email
$email = \Config\Services::email();
$email->setTo($recipient);
$email->setSubject($subject);
$email->setMessage('Hello from Queue!');
$email->send();
}
}Register the job handler in app/Config/Queue.php:
// ...
use App\Jobs\SendEmail;
public array $jobHandlers = [
'send-email' => SendEmail::class
];
// ...You can use the service('queue') to push a new job from your Controller or Model:
service('queue')->push(
'default', // Queue name
'send-email', // Job handler alias
['email' => '[email protected]', 'subject' => 'Welcome!'] // Job data
);To start processing jobs, run the worker command:
php spark queue:work defaultYou can set the priority for a job before pushing it to the queue. Higher numbers are processed first.
service('queue')
->setPriority(10)
->push('default', 'send-email', ['email' => '[email protected]']);You can also define queue priorities in your app/Config/Queue.php:
public array $queuePriorities = [
'default' => [10, 5, 1],
];And run the worker with specific priorities:
php spark queue:work default -priority 10,5Postpone job execution by a specific number of seconds:
service('queue')
->setDelay(60) // Delay for 60 seconds
->push('default', 'send-email', ['email' => '[email protected]']);queue:publish- Publish configuration file.queue:job- Generate a new job class.queue:work- Start the worker.queue:failed- List failed jobs.queue:retry- Retry a failed job.queue:forget- Remove a failed job.queue:flush- Flush all failed jobs.queue:clear- Clear all jobs from a queue.queue:stop- Signal the worker to stop.
We accept and encourage contributions from the community. See the CONTRIBUTING.md file for details.
This project is licensed under the MIT License.