solophp / task-queue
Simple PHP task queue with database storage
v1.0.0
2025-01-25 14:13 UTC
Requires
- php: >=8.2
- ext-json: *
- solophp/database: ^2.5
README
Simple PHP task queue using Solo Database.
Installation
composer require solophp/task-queue
Setup
use Solo\Queue\TaskQueue; $queue = new TaskQueue($db); $queue->install(); // Create tasks table
Usage
Add task:
$taskId = $queue->addTask( 'email_notification', ['user_id' => 123, 'template' => 'welcome'], new DateTimeImmutable('tomorrow') );
Process tasks:
$queue->processPendingTasks(function (string $name, array $payload) { match($name) { 'email_notification' => sendEmail($payload), 'push_notification' => sendPush($payload), default => throw new RuntimeException("Unknown task: $name") }; });
Methods
install()
- Create tasks tableaddTask(string $name, array $payload, DateTimeImmutable $scheduledAt)
- Add task to queuegetPendingTasks(int $limit = 10)
- Get pending tasks ready for executionmarkInProgress(int $taskId)
- Mark task as in progressmarkCompleted(int $taskId)
- Mark task as completedmarkFailed(int $taskId, string $error = '')
- Mark task as failed with error messageprocessPendingTasks(callable $callback, int $limit = 10)
- Process pending tasks with callback
License
MIT