Documentation

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
category

Pop

author

Nick Sagona, III nick@popphp.org

copyright

Copyright (c) 2009-2026 Nick Sagona, III

license

https://www.popphp.org/license New BSD License

version
3.0.0

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

$jobsFailed

Jobs failed since registration

protected int $jobsFailed = 0

$jobsProcessed

Jobs completed since registration

protected int $jobsProcessed = 0

$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

$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

create()

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
WorkerRecord

fromArray()

Rebuild from a stored array

public static fromArray(array<string|int, mixed> $data) : WorkerRecord
Parameters
$data : array<string|int, mixed>
Return values
WorkerRecord

getCurrentJobDuration()

How long the current job has been running, or null when idle

public getCurrentJobDuration() : int|null
Return values
int|null

getCurrentJobId()

public getCurrentJobId() : string|null
Return values
string|null

getCurrentJobStartedAt()

public getCurrentJobStartedAt() : int|null
Return values
int|null

getCurrentJobTimeout()

public getCurrentJobTimeout() : int|null
Return values
int|null

getCurrentQueue()

public getCurrentQueue() : string|null
Return values
string|null

getHost()

public getHost() : string
Return values
string

getJobsFailed()

public getJobsFailed() : int
Return values
int

getJobsProcessed()

public getJobsProcessed() : int
Return values
int

getLastSeenAt()

public getLastSeenAt() : int
Return values
int

getMode()

public getMode() : string
Return values
string

getName()

public getName() : string|null
Return values
string|null

getQueues()

public getQueues() : array<string|int, mixed>
Return values
array<string|int, mixed>

getStartedAt()

public getStartedAt() : int
Return values
int

isLikelyStuck()

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
bool

isStale()

Whether this worker's heartbeat is older than the given threshold

public isStale([int $seconds = 90 ]) : bool
Parameters
$seconds : int = 90
Return values
bool

setCurrentJob()

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
WorkerRecord

setCurrentJobStartedAt()

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
WorkerRecord

toArray()

Flatten to a plain array for storage

public toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

        
On this page

Search results