Download the PHP package nails/module-queue without Composer
On this page you can find all versions of the php package nails/module-queue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package module-queue
Queue Module for Nails
A lightweight, database-backed job queue for the Nails framework. It provides a simple API to enqueue tasks, a worker to process them, retry/backoff handling, and maintenance commands.
- Storage: MySQL tables managed by migrations
- Concurrency: safe reservation using
SELECT ... FOR UPDATE SKIP LOCKED - Ordering: strict FIFO per queue (lowest
idfirst amongavailable_at <= NOW()) - Retries: per-task maximum with exponential backoff + jitter
- Extensibility: plug-in queues via an interface with optional
setup/refreshhooks
Installation
Install via composer:
Setup required tables using migrations as normal:
Tables created:
nails_queue_worker: active workers and their heartbeatsnails_queue_job: queued jobs and their lifecycle
High-level Overview
Core concepts:
- Queue: a logical stream of jobs. Implemented as a class that can run
setupandrefreshhooks. There are built-in queuesDefaultQueueandPriorityQueue. - Job: a unit of work defined by a
Taskclass and aDatapayload. Jobs transition throughPENDING → RUNNING → COMPLETE | FAILED. - Worker: a long-running process which polls one or more queues and executes jobs. Each worker registers itself and sends periodic heartbeats.
Flow:
- Application enqueues a job:
Manager->push(Task, Data, Queue|alias|class, availableAt) - Worker (
nails queue:work) reserves a job atomically - Worker runs the
Task->run(Data)method - On success →
COMPLETE; on exception → eitherFAILEDor scheduled for retry depending onTask::getMaxRetries() - Maintenance (
nails queue:clean) resets stuck jobs, removes stale workers, and rotates old job rows
Configuration
Set via configuration class (e.g. environment variables, Config::get()). Defaults are shown in parentheses.
QUEUE_WORKER_WAIT_TIME(500): Initial wait time in milliseconds between polls when no work is found. Doubles up to 5000ms; a small random jitter is added each sleep.QUEUE_WORKER_REFRESH_INTERVAL(300): Seconds between invokingQueue::refresh()for each active queue (approx; called in the idle loop). This is effectively "~5 minutes" when jobs are running back-to-back, as the loop only checks between jobs; when idle, it can be called more frequently.QUEUE_WORKER_HEARTBEAT_STALE(300): Seconds after which a worker without heartbeat is considered stale and eligible for deletion byqueue:clean.QUEUE_JOB_ROTATE_COMPLETE_DAYS(7): Days to retainCOMPLETEjobs. Set 0 to disable deletion.QUEUE_JOB_ROTATE_FAILED_DAYS(30): Days to retainFAILEDjobs. Set 0 to disable deletion.
Interfaces
Implement these in user-land to add new queues and jobs.
Queue
Built-in queues:
Nails\Queue\Queue\Queues\DefaultQueueNails\Queue\Queue\Queues\PriorityQueue
These are registered under the aliases default and priority respectively.
Task
Retry behaviour:
- On exception, if
attempts < getMaxRetries(), the job is rescheduled toPENDINGwith anavailable_atdelay computed by exponential backoff (+/- 20% jitter):- base 5s, factor 2, capped at 5 minutes
- When attempts exceed max, the job is marked
FAILEDand error is recorded.
Data
The module provides a simple factory to rehydrate Data from JSON when jobs are loaded.
Manager Service API (selected)
Notes:
- Passing
nullor'default'as the queue argument uses the default queue alias; you can also pass a fully-qualified queue class name (FQCN) or an instance. - FIFO is approximated by selecting the smallest available
idwhereavailable_at <= NOW(). unworkedQueues()is the list shown by the admin dashboard alert. A worker counts when its heartbeat is newer thanQUEUE_WORKER_HEARTBEAT_STALE(300 seconds by default) and it was started for that queue class.isProcessed()is true when a live worker lists the queue. For a discovered queue, that is the opposite of appearing inunworkedQueues().
Creating user-land queues and jobs
1) Define a Queue (optional)
If you need custom startup or periodic maintenance, implement your own queue class and register an alias at bootstrap.
You can place your own Queues wherever you like, but for auto-discovery you should place them in the App\Queue\Queues namespace.
Optionally, register an alias:
2) Define a Task
3) Define a Data class
The supplied Data factory is usually sufficient for defining the payload for each job:
However, you are free to use your own implementation if you wish. In the following example we ensure that our payload is in the right shape and also validate that our headers match our rows:
4) Enqueue a job
Running the worker
Start a worker to process jobs from one or more queues.
Basic usage (current dir is the web root):
Options:
--queue=<QueueFQCN-or-alias>: Process a specific queue. Repeat to listen to multiple queues. Defaults to the default queue if not provided.
Examples:
What the worker does:
- Registers itself and prints its token/ID
- Calls
Queue::setup()once per queue at startup - Event loop:
- Attempts to reserve the next job (
PENDING,available_at <= NOW()) using SKIP LOCKED - On success: marks
RUNNING, executesTask->run(Data), then marksCOMPLETEor schedules retry / marksFAILED - On idle: sleeps with exponential backoff (ms) and jitter up to 5000ms, then polls again
- Periodically calls
Queue::refresh()per queue (approx everyQUEUE_WORKER_REFRESH_INTERVALseconds when idle) - Updates its heartbeat and flushes DB cache each iteration
- Attempts to reserve the next job (
Graceful shutdown:
- Where possible, the worker unregisters itself on destruct. When a process is terminated the worker may be left registered. This is cleaned up using the
queue:cleancommand, detailed below.
Maintenance command
Clean up workers and jobs:
Performs:
- Delete stale workers whose heartbeat is older than
QUEUE_WORKER_HEARTBEAT_STALEseconds - Reset stuck jobs:
RUNNINGwith noworker_idand nofinishedback toPENDING - Rotate old jobs:
- Remove
COMPLETEolder thanQUEUE_JOB_ROTATE_COMPLETE_DAYS - Remove
FAILEDolder thanQUEUE_JOB_ROTATE_FAILED_DAYS
- Remove
This command auto registers itself into cron and runs every 5 minutes.
Daemonising the worker (supervisord example)
Keep the queue proccess running in the background (with auto-restart) using a process manager. Common examples:
systemd
/etc/systemd/system/nails-queue.service
Commands:
supervisord
/etc/supervisor/conf.d/queue-worker.conf:
Commands:
Tips:
- Run one program block per queue, or a single process handling multiple queues with repeated
--queueflags - Ensure your application’s environment is loaded for the worker process (env vars, config, DB access)
All versions of module-queue with dependencies
nails/common Version dev-develop
nails/module-console Version dev-develop
nails/module-cron Version dev-develop