WorkerRecord
in package
Worker record class
A point-in-time snapshot of one worker process: who it is, what it is servicing, what it is working on right now, and when it was last heard from. Deliberately plain scalars and arrays only, so it serializes as JSON and the registry carries no unserialize() object-injection surface.
Tags
Table of Contents
Constants
- MODE_DAEMON = 'daemon'
- Worker modes
- MODE_SINGLE_PASS = 'single-pass'
Properties
- $currentJobId : string|null
- ID of the job currently being executed, if any
- $currentJobStartedAt : int|null
- When the current job started
- $currentJobTimeout : int|null
- The current job's own timeout, stored so isLikelyStuck() can be answered without loading the job payload
- $currentQueue : string|null
- Name of the queue the current job came from
- $host : string
- Hostname
- $id : string
- Unique worker instance ID
- $jobsFailed : int
- Jobs failed since registration
- $jobsProcessed : int
- Jobs completed since registration
- $lastSeenAt : int
- Last heartbeat
- $mode : string
- Worker mode - MODE_DAEMON or MODE_SINGLE_PASS
- $name : string|null
- Optional operator-facing label
- $pid : int
- Process ID
- $queues : array<string|int, mixed>
- Names of the queues being serviced
- $startedAt : int
- When this worker registered
Methods
- __construct() : mixed
- Constructor
- clearCurrentJob() : WorkerRecord
- create() : WorkerRecord
- Create a record for the current process
- fromArray() : WorkerRecord
- Rebuild from a stored array
- getCurrentJobDuration() : int|null
- How long the current job has been running, or null when idle
- getCurrentJobId() : string|null
- getCurrentJobStartedAt() : int|null
- getCurrentJobTimeout() : int|null
- getCurrentQueue() : string|null
- getHost() : string
- getId() : string
- getJobsFailed() : int
- getJobsProcessed() : int
- getLastSeenAt() : int
- getMode() : string
- getName() : string|null
- getPid() : int
- getQueues() : array<string|int, mixed>
- getStartedAt() : int
- incrementFailed() : WorkerRecord
- incrementProcessed() : WorkerRecord
- isLikelyStuck() : bool
- Whether this worker looks stuck: its heartbeat is stale AND it has a job in flight that has outlived its own timeout (or, when the job set no timeout, the staleness threshold itself as the fallback yardstick).
- isStale() : bool
- Whether this worker's heartbeat is older than the given threshold
- setCurrentJob() : WorkerRecord
- Record the job about to be executed
- setCurrentJobStartedAt() : WorkerRecord
- Set the current job's start time directly (used when restoring a record, and by tests that need a deterministic duration)
- setMode() : WorkerRecord
- setName() : WorkerRecord
- setQueues() : WorkerRecord
- toArray() : array<string|int, mixed>
- Flatten to a plain array for storage
- touch() : WorkerRecord
- Refresh the heartbeat
Constants
MODE_DAEMON
Worker modes
public
mixed
MODE_DAEMON
= 'daemon'
MODE_SINGLE_PASS
public
mixed
MODE_SINGLE_PASS
= 'single-pass'
Properties
$currentJobId
ID of the job currently being executed, if any
protected
string|null
$currentJobId
= null
$currentJobStartedAt
When the current job started
protected
int|null
$currentJobStartedAt
= null
$currentJobTimeout
The current job's own timeout, stored so isLikelyStuck() can be answered without loading the job payload
protected
int|null
$currentJobTimeout
= null
$currentQueue
Name of the queue the current job came from
protected
string|null
$currentQueue
= null
$host
Hostname
protected
string
$host
$id
Unique worker instance ID
protected
string
$id
$jobsFailed
Jobs failed since registration
protected
int
$jobsFailed
= 0
$jobsProcessed
Jobs completed since registration
protected
int
$jobsProcessed
= 0
$lastSeenAt
Last heartbeat
protected
int
$lastSeenAt
$mode
Worker mode - MODE_DAEMON or MODE_SINGLE_PASS
protected
string
$mode
= self::MODE_SINGLE_PASS
$name
Optional operator-facing label
protected
string|null
$name
= null
$pid
Process ID
protected
int
$pid
$queues
Names of the queues being serviced
protected
array<string|int, mixed>
$queues
= []
$startedAt
When this worker registered
protected
int
$startedAt
Methods
__construct()
Constructor
public
__construct(string $id, string $host, int $pid, int $startedAt, int $lastSeenAt) : mixed
Timestamps are explicit parameters rather than defaulted to time() so time-dependent behavior is testable without sleeping.
Parameters
- $id : string
- $host : string
- $pid : int
- $startedAt : int
- $lastSeenAt : int
clearCurrentJob()
public
clearCurrentJob() : WorkerRecord
Return values
WorkerRecordcreate()
Create a record for the current process
public
static create([string|null $name = null ][, array<string|int, mixed> $queues = [] ][, string $mode = self::MODE_SINGLE_PASS ]) : WorkerRecord
The ID carries a random suffix so a recycled PID can never collide with an older record from the same host.
Parameters
- $name : string|null = null
- $queues : array<string|int, mixed> = []
- $mode : string = self::MODE_SINGLE_PASS
Return values
WorkerRecordfromArray()
Rebuild from a stored array
public
static fromArray(array<string|int, mixed> $data) : WorkerRecord
Parameters
- $data : array<string|int, mixed>
Return values
WorkerRecordgetCurrentJobDuration()
How long the current job has been running, or null when idle
public
getCurrentJobDuration() : int|null
Return values
int|nullgetCurrentJobId()
public
getCurrentJobId() : string|null
Return values
string|nullgetCurrentJobStartedAt()
public
getCurrentJobStartedAt() : int|null
Return values
int|nullgetCurrentJobTimeout()
public
getCurrentJobTimeout() : int|null
Return values
int|nullgetCurrentQueue()
public
getCurrentQueue() : string|null
Return values
string|nullgetHost()
public
getHost() : string
Return values
stringgetId()
public
getId() : string
Return values
stringgetJobsFailed()
public
getJobsFailed() : int
Return values
intgetJobsProcessed()
public
getJobsProcessed() : int
Return values
intgetLastSeenAt()
public
getLastSeenAt() : int
Return values
intgetMode()
public
getMode() : string
Return values
stringgetName()
public
getName() : string|null
Return values
string|nullgetPid()
public
getPid() : int
Return values
intgetQueues()
public
getQueues() : array<string|int, mixed>
Return values
array<string|int, mixed>getStartedAt()
public
getStartedAt() : int
Return values
intincrementFailed()
public
incrementFailed() : WorkerRecord
Return values
WorkerRecordincrementProcessed()
public
incrementProcessed() : WorkerRecord
Return values
WorkerRecordisLikelyStuck()
Whether this worker looks stuck: its heartbeat is stale AND it has a job in flight that has outlived its own timeout (or, when the job set no timeout, the staleness threshold itself as the fallback yardstick).
public
isLikelyStuck([int $staleSeconds = 90 ]) : bool
A stale worker with NO current job is deliberately not "stuck" - it is wedged idle, which getStaleWorkers() surfaces instead.
Parameters
- $staleSeconds : int = 90
Return values
boolisStale()
Whether this worker's heartbeat is older than the given threshold
public
isStale([int $seconds = 90 ]) : bool
Parameters
- $seconds : int = 90
Return values
boolsetCurrentJob()
Record the job about to be executed
public
setCurrentJob(string $jobId[, string|null $queue = null ][, int|null $timeout = null ]) : WorkerRecord
Parameters
- $jobId : string
- $queue : string|null = null
- $timeout : int|null = null
Return values
WorkerRecordsetCurrentJobStartedAt()
Set the current job's start time directly (used when restoring a record, and by tests that need a deterministic duration)
public
setCurrentJobStartedAt(int|null $startedAt) : WorkerRecord
Parameters
- $startedAt : int|null
Return values
WorkerRecordsetMode()
public
setMode(string $mode) : WorkerRecord
Parameters
- $mode : string
Return values
WorkerRecordsetName()
public
setName(string|null $name) : WorkerRecord
Parameters
- $name : string|null
Return values
WorkerRecordsetQueues()
public
setQueues(array<string|int, mixed> $queues) : WorkerRecord
Parameters
- $queues : array<string|int, mixed>
Return values
WorkerRecordtoArray()
Flatten to a plain array for storage
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>touch()
Refresh the heartbeat
public
touch() : WorkerRecord