WorkerRegistry
in package
Worker registry class
The read-side facade over a registry backend: who is running, which of them have gone quiet, and which look genuinely stuck.
Tags
Table of Contents
Properties
- $attached : array<string|int, mixed>
- Event managers this registry has already attached listeners to, keyed by spl_object_id. resolveEventManager() returns the SAME manager for every queue whenever the Application-level fallback is in play, and Manager::on() is additive - without this guard, N queues would install N copies of each listener on one manager and a single job would count N times. It also makes repeated setRegistry()/attachTo() calls safe.
- $record : WorkerRecord|null
- This process's own record, once registered. A WorkerRegistry instance represents this process's view of the registry, which is what lets the event listeners attached by attachTo() mutate the record by closing over $this.
- $registry : RegistryInterface
- Registry backend
Methods
- __construct() : mixed
- Constructor
- attachTo() : void
- Wire this registry's current-job and counter tracking onto a worker's queues, via the queue lifecycle events.
- countWorkers() : int
- How many workers are registered
- deregister() : void
- Remove this process's record. A no-op when not registered.
- getRecord() : WorkerRecord|null
- This process's own record, or null if it hasn't registered
- getRegistry() : RegistryInterface
- Get the underlying backend
- getStaleWorkers() : array<string|int, mixed>
- Workers whose heartbeat has gone quiet. Note this includes workers that are merely busy inside a long job - a worker executing a job cannot heartbeat, so use getStuckWorkers() to narrow to the ones that look genuinely wedged.
- getStuckWorkers() : array<string|int, mixed>
- Workers that are stale AND holding a job that has outlived its own timeout - the alerting signal
- getWorker() : WorkerRecord|null
- A single worker by ID, or null
- getWorkers() : array<string|int, mixed>
- Every registered worker, keyed by worker ID
- heartbeat() : void
- Refresh this process's heartbeat and flush its record. A no-op when not registered, so callers never need to guard.
- isRegistered() : bool
- Whether this process has registered
- prune() : int
- Remove records left behind by processes that are long gone
- register() : WorkerRecord
- Register this process, writing its record to the backend
- attachListeners() : void
- Attach this registry's listeners to one event manager, at most once per manager for the lifetime of this registry instance.
- resolveEventManager() : Manager
- Find the event manager a queue's events actually reach, without disturbing existing wiring.
Properties
$attached
Event managers this registry has already attached listeners to, keyed by spl_object_id. resolveEventManager() returns the SAME manager for every queue whenever the Application-level fallback is in play, and Manager::on() is additive - without this guard, N queues would install N copies of each listener on one manager and a single job would count N times. It also makes repeated setRegistry()/attachTo() calls safe.
protected
array<string|int, mixed>
$attached
= []
$record
This process's own record, once registered. A WorkerRegistry instance represents this process's view of the registry, which is what lets the event listeners attached by attachTo() mutate the record by closing over $this.
protected
WorkerRecord|null
$record
= null
$registry
Registry backend
protected
RegistryInterface
$registry
Methods
__construct()
Constructor
public
__construct(RegistryInterface $registry) : mixed
Parameters
- $registry : RegistryInterface
attachTo()
Wire this registry's current-job and counter tracking onto a worker's queues, via the queue lifecycle events.
public
attachTo(Worker $worker) : void
Tracking rides on the existing events rather than new plumbing because Queue::work() reserves AND runs a job internally - the Worker only receives it after it ran, so it structurally cannot record the current job before execution. queue.job.pre fires before execution, which is exactly the hook needed.
Parameters
- $worker : Worker
countWorkers()
How many workers are registered
public
countWorkers() : int
Return values
intderegister()
Remove this process's record. A no-op when not registered.
public
deregister() : void
getRecord()
This process's own record, or null if it hasn't registered
public
getRecord() : WorkerRecord|null
Return values
WorkerRecord|nullgetRegistry()
Get the underlying backend
public
getRegistry() : RegistryInterface
Return values
RegistryInterfacegetStaleWorkers()
Workers whose heartbeat has gone quiet. Note this includes workers that are merely busy inside a long job - a worker executing a job cannot heartbeat, so use getStuckWorkers() to narrow to the ones that look genuinely wedged.
public
getStaleWorkers([int $seconds = 90 ]) : array<string|int, mixed>
Parameters
- $seconds : int = 90
Return values
array<string|int, mixed>getStuckWorkers()
Workers that are stale AND holding a job that has outlived its own timeout - the alerting signal
public
getStuckWorkers([int $seconds = 90 ]) : array<string|int, mixed>
Parameters
- $seconds : int = 90
Return values
array<string|int, mixed>getWorker()
A single worker by ID, or null
public
getWorker(string $id) : WorkerRecord|null
Parameters
- $id : string
Return values
WorkerRecord|nullgetWorkers()
Every registered worker, keyed by worker ID
public
getWorkers() : array<string|int, mixed>
Return values
array<string|int, mixed>heartbeat()
Refresh this process's heartbeat and flush its record. A no-op when not registered, so callers never need to guard.
public
heartbeat() : void
isRegistered()
Whether this process has registered
public
isRegistered() : bool
Return values
boolprune()
Remove records left behind by processes that are long gone
public
prune([int $olderThanSeconds = 3600 ]) : int
Parameters
- $olderThanSeconds : int = 3600
Return values
intregister()
Register this process, writing its record to the backend
public
register([string|null $name = null ][, array<string|int, mixed> $queueNames = [] ][, string $mode = WorkerRecord::MODE_SINGLE_PASS ]) : WorkerRecord
Note: when a Worker is given this registry, it calls this for you at the right point in its lifecycle. Calling it directly opts you out of that - the Worker will see an existing registration, take no ownership, and never deregister it, so the record outlives the run and is only reaped by prune().
Parameters
- $name : string|null = null
-
optional operator-facing label
- $queueNames : array<string|int, mixed> = []
-
names of the queues being serviced
- $mode : string = WorkerRecord::MODE_SINGLE_PASS
-
WorkerRecord::MODE_DAEMON or MODE_SINGLE_PASS
Return values
WorkerRecordattachListeners()
Attach this registry's listeners to one event manager, at most once per manager for the lifetime of this registry instance.
protected
attachListeners(Manager $events) : void
Parameters
- $events : Manager
resolveEventManager()
Find the event manager a queue's events actually reach, without disturbing existing wiring.
protected
resolveEventManager(Queue $queue, Worker $worker) : Manager
Queue::triggerEvent() uses the queue's own manager if set, else the Application's - and setting a queue-level manager SUPPRESSES the Application fallback. So attaching to the wrong one, or installing a new one where a fallback was in play, would silently orphan a user's app-level listeners.