PHP Luminova: Run Database Queue Workers with Novakit
Enqueue, run, inspect, and manage database-backed background tasks with Novakit.
The Database Background Task execution in Novakit provides helper commands for working with the Luminova\Base\AbstractTaskQueue class. It allows you to queue and run background tasks using a database queue that supports:
- Delayed execution
- Retries on failure
- Task priority levels
- Recurring tasks (e.g., daily or hourly)
The CLI TaskQueue commands allows you to manage queued tasks directly from the terminal. You can:
- Add new tasks to the queue
- Pause, retry, or inspect existing tasks
- Run a background task worker using cron
- Monitor task execution in real time
For setup instructions and full examples, see the Background Task Queue System documentation.
List of Commands
To list all available commands run: php novakit list --command=task.
task:init Initialize the task queue system and create the required table in your database.
task:deinit Remove the task queue table and all tasks.
task:queue Add a new task to the queue.
task:list Show tasks in the queue. You can filter by status.
task:info Show full information of a task by ID.
task:delete Delete a task from the queue.
task:purge Clear all tasks with a specific status (like completed).
task:pause Pause a task. It will not run until resumed.
task:resume Resume a paused task.
task:retry Set a failed task back to pending.
task:status Manually change a task's status.
task:run Start a worker that processes tasks in the queue.
task:sig Send control signals to the task worker (stop or resume).
task:tail Show real-time logs while tasks are being processed.
task:export Export tasks from the queue with optional status filtering.
task:listenis an alias fortask:tail.
Task Status List
You can use these values to filter or update task status:
| Status | Type | Description |
|---|---|---|
all | string | Includes tasks of all statuses. |
forever | string | Task runs again and again. It will not be auto-deleted. |
pending | string | Task is waiting in the queue. |
paused | string | Task is paused. It will not run until resumed. |
running | string | Task is now being processed. |
failed | string | Task failed. Usually caused by an error or exception. |
completed | string | Task ran successfully. It may be deleted if configured. |
allandforeverwork as filters fortask:listandtask:purge.task:statusonly acceptspending,paused,running,failedandcompleted.
Custom Task Handler Class
Use --class (or -c) in any command to choose your own task queue class. If it is not set, the default class is used:
App\Tasks\TaskQueue.
You can pass a fully qualified class name, or just a class name inside App\Tasks. The class must extend Luminova\Base\AbstractTaskQueue.
Examples:
php novakit task:list --class=App\\Tasks\\MyTask
php novakit task:list -c=App\\Tasks\\MyTask
php novakit task:list -c=MyTaskEach class manages its own task group, so a command only affects the tasks of the class you select.
Command Help
To show all task related commands:
php novakit task --helpTo see help for one command:
php novakit task:list --helpTask Table Setup
Create the Table
Use this the first time you use the task system:
php novakit task:initDelete the Table
This removes all tasks and deletes the task table:
php novakit task:deinitIf a command fails and the table is missing, Novakit prints the commands to initialize or reset the queue.
Adding Tasks to the Queue
Add the tasks defined in your queue class, using tasks() and stage():
php novakit task:queueAdd a specific handler:
# Using instance method (@)
php novakit task:queue \
--task=App\\Utils\\Worker@clean \
--args='["temp"]' \
--priority=1 \
--schedule='+10 minutes'
# Using static method (::)
php novakit task:queue \
--task=App\\Utils\\Worker::clean \
--args='["temp"]' \
--priority=1 \
--schedule='+10 minutes'Option guide:
--taskor-t: The handler to run (example:Class@method,Class::methodor a function name)--argsor-a: Handler arguments as a JSON array, such as["temp"]. Omit the option to pass no arguments.--scheduleor-s: When to run (a UNIX timestamp, a date likeY-m-d H:i:s, or a relative time like+10 minutes)--priorityor-p: Number from 0 to 100 (lower runs earlier, default0)--foreveror-f: Repeat interval in minutes, minimum5(example:1440for once a day)--retriesor-r: Number of times to retry a failed task (default0, no retry)
Adding Tasks in Code (Programmatic Usage)
You can also add tasks inside your code:
use App\Utils\Worker;
use App\Tasks\TaskQueue;
$arguments = ['temp'];
$schedule = '+10 minutes';
$priority = 1;
$queue = TaskQueue::create();
// Static method
$queue->enqueue(Worker::class . '::clean', $arguments, $schedule, $priority);
// Instance method
$queue->enqueue(Worker::class . '@clean', $arguments, $schedule, $priority);
// Or an invokable class that implements Luminova\Interface\QueueableInterface
$queue->enqueue(Worker::class, $arguments, $schedule, $priority);CLI vs Code Usage
| Feature | CLI Example | Code Example |
|---|---|---|
| Context | Terminal, cron jobs | PHP scripts, controllers, events |
| Method format | -t Class@method or Class::method | 'Class@method' or 'Class::method' |
| Arguments format | JSON string | PHP array |
| Schedule format | String (e.g. +10 minutes, UNIX timestamp) | DateTimeInterface\|string\|int\|null |
| When to use | Manual or scheduled queue | Queue tasks inside your application flow |
Managing Tasks
# List tasks, optionally filtered by status, with a limit and offset
php novakit task:list --status=pending --limit=10 --offset=0
# Show full details and execution output of one task
php novakit task:info --id=10
# Pause, resume or retry a task
php novakit task:pause --id=10
php novakit task:resume --id=10
php novakit task:retry --id=10
# Set a status manually
php novakit task:status --id=10 --status=paused
# Delete one task, or purge all tasks with a status (default: all)
php novakit task:delete --id=10
php novakit task:purge --status=completed
# Export tasks to a file
php novakit task:export --dir=path/to/export/tasks.php --status=pendingNotes:
task:pauseworks onpendingorfailedtasks,task:resumeonpausedtasks, andtask:retryonfailedtasks.task:exportrequires--dir. The file is a PHP file that returns an array of task definitions.
Running the Task Worker
To start processing queued tasks:
php novakit task:runOptions:
--limitor-l: Maximum number of tasks to process per loop--sleepor-s: Initial delay between processing batches in microseconds (default100000). Idle polling uses a separate half-second wait.--idle: Maximum idle loops before the worker stops (default10)--idor-i: Run only the task with this ID--outputor-o: A log level (such asdebug) or a file path for the worker output--flock-workeror-f: Ask Novakit to acquire the queue’s worker lock
When processing a batch, the worker sorts by ascending priority, scheduled time, and task ID. It stops when it receives SIGTERM or SIGINT (requires pcntl), detects a configured stop signal, or reaches the idle limit. Tasks that were still running when it stopped are set back to pending.
You can also run the worker from your own PHP code:
$queue = new App\Tasks\TaskQueue();
$queue->returnAsTaskModel = false;
$queue->run(sleep: 100000, limit: 10, maxIdle: 3);
$results = $queue->getResult();Using Cron to Run Tasks Automatically
To run the worker every minute:
* * * * * cd /path/to/project/ && php novakit task:run >> /path/to/logs/task.log 2>&1You can also add more options:
* * * * * cd /path/to/project && php novakit task:run --class='App\Tasks\UserTaskQueue' --limit=10 --sleep=200000 --idle=3 --output=debug >> /var/log/task_worker.log 2>&1Explanation:
--limit=10: Process 10 tasks at most in one loop--sleep=200000: Set the initial processing delay to 0.2 seconds--idle=3: Stop after 3 loops with no tasks--output=debug: Send the worker output to thedebuglog level. Failed runs are logged at thecriticallevel.
To check your PHP path:
command -v phpMonitoring Tasks
Enable event logging in your queue class:
public bool $taskEventTailing = true;Then watch the events in real time:
php novakit task:tailEvents are written to writeable/logs/queues/event_{Class_Name}_task.log while tasks run through task:run. The log file is cleared each time a worker starts.
Options:
--jsonor-j: Show log lines as formatted JSON when possible--sinceor-s: Show logs from a time window or timestamp (e.g.10s,5m,2h)--grepor-g: Show only lines that contain the text--excludeor-x: Hide lines that contain the text--waitor-w: Wait time between reads, in microseconds--retryor-r: Retry attempts when the log file is not available--inotify: Watch the file with theinotifyextension, if installed
php novakit task:tail --grep=ERROR --exclude=DEBUG
php novakit task:tail --json --since=10mAvoid Running the Worker Twice
Sometimes a task worker is still running when another one starts. To stop this, use a locking system.
Using Cron File Lock
On hosts with the flock command, hold the lock for the entire worker invocation:
* * * * * flock -n /tmp/task.lock sh -c 'cd /path/to/project && php novakit task:run'Using --flock-worker with Novakit
This uses the queue class’s lock in writeable/locks/queues/. Novakit skips the run if its initial check finds the lock held. If lock acquisition fails after the initial check, the worker can report a file-lock error.
* * * * * cd /path/to/project/ && php novakit task:run --flock-workerUsing a Stop Signal File
You can use a stop signal file to stop the worker before its next queue check. Remove the signal before starting the worker again. Set the file path in your queue class:
protected ?string $stopSignalFile = 'writeable/locks/queues/stop_worker.lock';The worker only checks for the file when $stopSignalFile is set. Use the task:sig command to create or remove it:
php novakit task:sig --stop-workerphp novakit task:sig --resume-workerNote: You can also create or delete the file manually.