Queue

Reference for the Queue module: register queues and process their items. Two queue types are supported. Lock-based queues, registered with QueueConfig::AddQueue, are drained in-process using row locking. Task-queues, registered with QueueTaskConfig::AddQueue, lease work to external, anonymous-but-authenticated workers and reconcile a lease mirror against the application's own pending set.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes

QueueConfig

Register a lock-based queue for processing and set how often the queue list is re-polled. Registering a queue only records it; processing is exposed as API endpoints by the separate QueueApi module. Each queue supplies two callbacks: one that locks and returns a batch of item ids, and one that processes a single item within a transaction.

Properties

PropertyTypeDescription
QueueConfig::$listPollSecondsintHow often the driver should re-poll the queue list, in seconds; defaults to 30.
QueueConfig::$queuesarrayThe registered queues, keyed by 'moduleName_queueName'.

AddQueue

QueueConfig::AddQueue(string $moduleName, string $queueName, int $maxProcessesPerSecond, int $maxProcessesInFlight, callable $lockNextCallback, callable $processItemCallback, int $idleRecheckSeconds = 5): void

Register a lock-based queue for processing under a module and queue name.

NameTypeDescription
$moduleNamestringModule name, used for error logging.
$queueNamestringThe queue name identifier.
$maxProcessesPerSecondintMaximum number of processes per second.
$maxProcessesInFlightintMaximum number of processes in flight.
$lockNextCallbackcallableLocks and returns a batch of item ids; takes an int limit, returns an array of ints.
$processItemCallbackcallableProcesses the item with the given id, within the transaction.
$idleRecheckSecondsintWhen drained and idle, how long before re-probing; defaults to 5.

Returns void

SetListPollSeconds

QueueConfig::SetListPollSeconds(int $seconds): void

Override how often the driver re-polls the queue list.

NameTypeDescription
$secondsintThe new list poll interval, in seconds.

Returns void

QueueConfigQueueData

The registration record for one lock-based queue, created by QueueConfig::AddQueue. Its fields are readonly. This class has no functions beyond its constructor.

PropertyTypeDescription
$queue->moduleNamestringModule name, used for error logging.
$queue->queueNamestringThe queue name identifier.
$queue->maxProcessesPerSecondintMaximum number of processes per second.
$queue->maxProcessesInFlightintMaximum number of processes in flight.
$queue->idleRecheckSecondsintWhen drained and idle, how long before re-probing.
$queue->lockNextCallbackmixedThe callback that locks and returns a batch of item ids.
$queue->processItemCallbackmixedThe callback that processes a single item.

QueueDatabase

Count, list and process the items of registered lock-based queues. Counting and processing both go through the queue's own lock callback inside a transaction; processing captures console output and records a result on success or failure.

CountQueueItems

QueueDatabase::CountQueueItems(QueueConfigQueueData $queue): int

Count the items in a queue using its lock callback, capped at 100 for performance.

NameTypeDescription
$queueQueueConfigQueueDataThe queue configuration.

Returns int The number of items, capped at 100.

CountResults

QueueDatabase::CountResults(?object $where): int

Count result rows, all rows when the where object is null or empty, otherwise filtered.

NameTypeDescription
$where?objectFields to filter by, or null to count all rows.

Returns int The number of matching result rows.

GetConfig

QueueDatabase::GetConfig(string $moduleName, string $queueName): QueueConfigQueueData

Get a registered queue's configuration by module name and queue name. Throws when not found.

NameTypeDescription
$moduleNamestringThe module name.
$queueNamestringThe queue name.

Returns QueueConfigQueueData The queue configuration.

GetQueueOptional

QueueDatabase::GetQueueOptional(string $moduleName, string $queueName): ?QueueConfigQueueData

Get a registered queue by module name and queue name, or null when there is no match.

NameTypeDescription
$moduleNamestringThe module name.
$queueNamestringThe queue name.

Returns ?QueueConfigQueueData The queue, or null.

GetQueues

QueueDatabase::GetQueues(): array

All registered lock-based queues.

Returns array A list of QueueConfigQueueData objects.

ListQueues

QueueDatabase::ListQueues(): array

List every registered queue with its process interval and item count calculated. Queues that error are included with fallback values.

Returns array A list of QueueInfoData objects.

ProcessQueueItem

QueueDatabase::ProcessQueueItem(QueueConfigQueueData $queue): void

Lock and process the next item in a queue within a transaction, recording a success or failure result. Returns early when there is nothing to do; throws and rolls back when processing fails.

NameTypeDescription
$queueQueueConfigQueueDataThe queue configuration.

Returns void

QueueInfo

Convert queue information between JSON and QueueInfoData objects, for one entry or a list.

FromJson

QueueInfo::FromJson(string $json): QueueInfoData

Parse a single QueueInfoData object from a JSON string.

NameTypeDescription
$jsonstringThe JSON string to parse.

Returns QueueInfoData The parsed queue info.

ListFromJson

QueueInfo::ListFromJson(string $json): array

Parse an array of QueueInfoData objects from a JSON string, calling FromJson on each entry.

NameTypeDescription
$jsonstringThe JSON string to parse.

Returns array A list of QueueInfoData objects.

ListToJson

QueueInfo::ListToJson(array $queueInfoDataList): array

Convert an array of QueueInfoData objects to stdClass objects ready for JSON encoding.

NameTypeDescription
$queueInfoDataListarrayThe QueueInfoData objects to convert.

Returns array A list of stdClass objects.

QueueInfoData

A snapshot of one queue's configuration and current item count, used when listing queues and moving that information over JSON. Its fields are readonly.

PropertyTypeDescription
$info->moduleNamestringThe owning module name.
$info->queueNamestringThe queue name.
$info->processIntervalMsintThe interval between processes, in milliseconds.
$info->countintThe item count, capped at 100.
$info->maxInFlightintMaximum number of processes in flight; defaults to 10.
$info->maxRatePerSecondintMaximum number of processes per second; defaults to 1.
$info->idleRecheckSecondsintWhen drained and idle, how long before re-probing; defaults to 5.
$info->countFetchTimeMsfloatHow long the count fetch took, in milliseconds; defaults to 0.0.
Functions

ToJson

QueueInfoData::ToJson(): stdClass

Convert this queue info to a stdClass object ready for JSON encoding.

Returns stdClass The queue info as an object.

QueueRateLimitError

Raised when a queue's rate limit is exceeded. Extends Exception. It adds no members of its own.

QueueResultDatabase

Record the outcome of processing a lock-based queue item, whether it succeeded or failed.

Functions
Add

Add

QueueResultDatabase::Add(string $moduleName, string $queueName, int $rowId, bool $success, string $description, ?string $consoleOutput): void

Insert a queue result entry recording a success or error for one processed row.

NameTypeDescription
$moduleNamestringThe owning module name.
$queueNamestringThe queue name.
$rowIdintThe id of the processed row.
$successboolWhether processing succeeded.
$descriptionstringA description of the outcome.
$consoleOutput?stringRaw captured console output, or null.

Returns void

QueueTaskConfig

Register task-queues, the second queue type, and look them up by key. A task-queue is processed by external, anonymous-but-authenticated workers that pull tasks, report progress and return a result; the application supplies the callbacks. The queue key is 'Module.queueName'.

Properties

PropertyTypeDescription
QueueTaskConfig::$queuesarrayThe registered task-queues, keyed by 'Module.queueName'.

AddQueue

QueueTaskConfig::AddQueue(string $moduleName, string $queueName, int $reassignSeconds, ?int $overallTimeoutSeconds, callable $candidateCallback, callable $payloadCallback, callable $applyResultCallback, callable $applyFailureCallback, ?callable $applyProgressCallback = null): void

Register a task-queue once at boot, with its worker callbacks and timeouts.

NameTypeDescription
$moduleNamestringThe owning application module.
$queueNamestringThe queue name.
$reassignSecondsintThe no-contact window before a new worker may be assigned.
$overallTimeoutSeconds?intSeconds from entry into the queue to permanent failure, or null for never.
$candidateCallbackcallableReturns pending record ids; takes a limit, does no locking.
$payloadCallbackcallableReturns the data a worker needs for a record.
$applyResultCallbackcallableIdempotent result write; returns true only when this call set it.
$applyFailureCallbackcallableWrites the permanent-failure sentinel for a record.
$applyProgressCallback?callableOptional; persists domain-relevant progress bits.

Returns void

All

QueueTaskConfig::All(): array

All registered task-queues.

Returns array A list of QueueTaskConfigData objects.

Get

QueueTaskConfig::Get(string $key): QueueTaskConfigData

Get a registered task-queue by its key. Throws when the key is unknown.

NameTypeDescription
$keystringThe queue key, 'Module.queueName'.

Returns QueueTaskConfigData The task-queue registration.

GetOptional

QueueTaskConfig::GetOptional(string $key): ?QueueTaskConfigData

Get a registered task-queue by its key, or null when the key is unknown.

NameTypeDescription
$keystringThe queue key, 'Module.queueName'.

Returns ?QueueTaskConfigData The task-queue registration, or null.

QueueTaskConfigData

The registration record for one task-queue, created by QueueTaskConfig::AddQueue. Its fields are readonly.

PropertyTypeDescription
$queue->moduleNamestringThe owning application module.
$queue->queueNamestringThe queue name.
$queue->reassignSecondsintThe rolling no-contact window before a new worker may be assigned.
$queue->overallTimeoutSeconds?intSeconds from entry into the queue to permanent failure, or null for never.
$queue->candidateCallbackmixedThe callback returning pending record ids.
$queue->payloadCallbackmixedThe callback returning a worker's payload for a record.
$queue->applyResultCallbackmixedThe idempotent result-write callback.
$queue->applyFailureCallbackmixedThe permanent-failure callback.
$queue->applyProgressCallbackmixedThe optional progress callback, or null.
Functions
Key

Key

QueueTaskConfigData::Key(): string

The queue key used everywhere, formed as 'Module.queueName'.

Returns string The queue key.

QueueTaskDatabase

The task-queue engine. Workers claim, heartbeat, complete and fail tasks; the scheduler runs a maintenance pass that reconciles the lease mirror against the application's pending set and samples gauges; admin helpers list and count raw task rows. Every operation is a single autocommit statement or a short independent sequence, so no transaction is held.

Constants

ConstantValueDescription
QueueTaskDatabase::CANDIDATE_LIMIT10000Upper bound on candidate ids pulled per Process pass.

Claim

QueueTaskDatabase::Claim(QueueTaskConfigData $queue): ?object

Atomically lease the next claimable task to a new worker reference.

NameTypeDescription
$queueQueueTaskConfigDataThe task-queue registration.

Returns ?object An object with workerRef, recordId and leaseSeconds, or null when nothing is claimable.

Complete

QueueTaskDatabase::Complete(QueueTaskConfigData $queue, int $recordId, string $workerRef, object $result): object

Record a terminal success. Not ref-gated: any worker's result is accepted and the idempotent result callback makes the first writer win. Removes the task.

NameTypeDescription
$queueQueueTaskConfigDataThe task-queue registration.
$recordIdintThe application record id.
$workerRefstringThe reporting worker's reference.
$resultobjectThe result to apply.

Returns object An object with accepted (always true) and first (whether this call set the result).

CountTasks

QueueTaskDatabase::CountTasks(string $queueKey): int

Count task rows, all rows when the key is empty, otherwise filtered to one queue key.

NameTypeDescription
$queueKeystringThe queue key to filter by, or empty for all.

Returns int The number of task rows.

DistinctQueueKeysAssoc

QueueTaskDatabase::DistinctQueueKeysAssoc(): array

The distinct queue keys currently present in the task table, as a value-to-value map for filtering.

Returns array A map of queue key to queue key.

Fail

QueueTaskDatabase::Fail(QueueTaskConfigData $queue, int $recordId, string $workerRef, bool $permanent, string $message): object

Record a terminal failure. A permanent failure is ref-gated and writes the sentinel; a transient failure releases the lease early for immediate re-claim.

NameTypeDescription
$queueQueueTaskConfigDataThe task-queue registration.
$recordIdintThe application record id.
$workerRefstringThe reporting worker's reference.
$permanentboolWhether the failure is permanent.
$messagestringThe failure message, for logging.

Returns object An object with accepted, true only when the current holder's failure counted.

ListTasks

QueueTaskDatabase::ListTasks(string $queueKey, int $page, int $limit): array

A page of raw task rows, optionally filtered by queue key, newest first.

NameTypeDescription
$queueKeystringThe queue key to filter by, or empty for all.
$pageintThe page number, from 1.
$limitintThe number of rows per page.

Returns array A list of raw task row objects.

Process

QueueTaskDatabase::Process(QueueTaskConfigData $queue): object

Run one maintenance pass: fail tasks past their overall timeout, reconcile the mirror against the application's candidate list, then sample and log gauges.

NameTypeDescription
$queueQueueTaskConfigDataThe task-queue registration.

Returns object A summary with queue, added, removed, failed, pending, inFlight and oldestPendingSeconds.

Progress

QueueTaskDatabase::Progress(QueueTaskConfigData $queue, int $recordId, string $workerRef, ?object $progress): string

Heartbeat, progress and cancellation in one call. Extends the lease and records progress for the current holder; a superseded worker is told to continue but has no authority.

NameTypeDescription
$queueQueueTaskConfigDataThe task-queue registration.
$recordIdintThe application record id.
$workerRefstringThe reporting worker's reference.
$progress?objectProgress bits to record, or null.

Returns string 'continue' while the task still exists, or 'stop' once it is gone.

Stats

QueueTaskDatabase::Stats(QueueTaskConfigData $queue): object

Live counts for a task-queue, read straight from the task table with no logging.

NameTypeDescription
$queueQueueTaskConfigDataThe task-queue registration.

Returns object An object with queue, moduleName, queueName, pending, inFlight and oldestPendingSeconds.

QueueTaskStats

Log per-queue health statistics for task-queues via LogCounter. Counts are recorded as summed values; measures record a duration or gauge. Stats must never break queue operations, so all failures are swallowed.

Constants

ConstantValueDescription
QueueTaskStats::PREFIX'Queue.task.'The prefix for every logged metric identifier.
Functions

Count

QueueTaskStats::Count(QueueTaskConfigData $queue, string $event, float $n = 1.0): void

Log an event count for a queue. Does nothing when the count is not positive.

NameTypeDescription
$queueQueueTaskConfigDataThe task-queue registration.
$eventstringThe event name, appended to the prefix.
$nfloatThe count to add; defaults to 1.0.

Returns void

Measure

QueueTaskStats::Measure(QueueTaskConfigData $queue, string $metric, float $value): void

Log a measured value for a queue, such as a duration in seconds or a sampled gauge.

NameTypeDescription
$queueQueueTaskConfigDataThe task-queue registration.
$metricstringThe metric name, appended to the prefix.
$valuefloatThe measured value.

Returns void