Luminova Framework

PHP Luminova: Run Database Queue Workers with Novakit

Last updated: 2026-11-28 20:57:12

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:listen is an alias for task:tail.


Task Status List

You can use these values to filter or update task status:

StatusTypeDescription
allstringIncludes tasks of all statuses.
foreverstringTask runs again and again. It will not be auto-deleted.
pendingstringTask is waiting in the queue.
pausedstringTask is paused. It will not run until resumed.
runningstringTask is now being processed.
failedstringTask failed. Usually caused by an error or exception.
completedstringTask ran successfully. It may be deleted if configured.

all and forever work as filters for task:list and task:purge. task:status only accepts pending, paused, running, failed and completed.


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=MyTask

Each 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 --help

To see help for one command:

php novakit task:list --help

Task Table Setup

Create the Table

Use this the first time you use the task system:

php novakit task:init

Delete the Table

This removes all tasks and deletes the task table:

php novakit task:deinit

If 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:queue

Add 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:

  • --task or -t: The handler to run (example: Class@method, Class::method or a function name)
  • --args or -a: Handler arguments as a JSON array, such as ["temp"]. Omit the option to pass no arguments.
  • --schedule or -s: When to run (a UNIX timestamp, a date like Y-m-d H:i:s, or a relative time like +10 minutes)
  • --priority or -p: Number from 0 to 100 (lower runs earlier, default 0)
  • --forever or -f: Repeat interval in minutes, minimum 5 (example: 1440 for once a day)
  • --retries or -r: Number of times to retry a failed task (default 0, 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

FeatureCLI ExampleCode Example
ContextTerminal, cron jobsPHP scripts, controllers, events
Method format-t Class@method or Class::method'Class@method' or 'Class::method'
Arguments formatJSON stringPHP array
Schedule formatString (e.g. +10 minutes, UNIX timestamp)DateTimeInterface\|string\|int\|null
When to useManual or scheduled queueQueue 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=pending

Notes:

  • task:pause works on pending or failed tasks, task:resume on paused tasks, and task:retry on failed tasks.
  • task:export requires --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:run

Options:

  • --limit or -l: Maximum number of tasks to process per loop
  • --sleep or -s: Initial delay between processing batches in microseconds (default 100000). Idle polling uses a separate half-second wait.
  • --idle: Maximum idle loops before the worker stops (default 10)
  • --id or -i: Run only the task with this ID
  • --output or -o: A log level (such as debug) or a file path for the worker output
  • --flock-worker or -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>&1

You 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>&1

Explanation:

  • --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 the debug log level. Failed runs are logged at the critical level.

To check your PHP path:

command -v php

Monitoring Tasks

Enable event logging in your queue class:

public bool $taskEventTailing = true;

Then watch the events in real time:

php novakit task:tail

Events 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:

  • --json or -j: Show log lines as formatted JSON when possible
  • --since or -s: Show logs from a time window or timestamp (e.g. 10s, 5m, 2h)
  • --grep or -g: Show only lines that contain the text
  • --exclude or -x: Hide lines that contain the text
  • --wait or -w: Wait time between reads, in microseconds
  • --retry or -r: Retry attempts when the log file is not available
  • --inotify: Watch the file with the inotify extension, if installed
php novakit task:tail --grep=ERROR --exclude=DEBUG
php novakit task:tail --json --since=10m

Avoid 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-worker

Using 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-worker
php novakit task:sig --resume-worker

Note: You can also create or delete the file manually.