Worker

Class
implements WorkerInterface
Read only: Yes Final: Yes

Represents a single forked worker process.

Description

This class encapsulates the lifecycle of a worker created via process forking. It is responsible for:

  • Spawning the child process
  • Executing the provided callback in the child context
  • Capturing output and errors
  • Synchronizing state with the master process

Instances of this class MUST be created exclusively by a manager implementation. Consumers SHOULD NOT instantiate this class directly.

Table of Contents

Interfaces

WorkerInterface

Defines the contract for a single worker process.

Constants

INTERRUPTED_SYSTEM_CALL_ERROR

POSIX error code for interrupted system calls.

 = 4
NO_CHILD_PROCESS_ERROR

POSIX error code for missing child processes.

 = 10
NO_SUCH_PROCESS_ERROR

POSIX error code for non-existent processes.

 = 3

Properties

$callback

Stores the callback executed inside the child process.

 : callable(WorkerInterface): mixed
 : LoggerInterface|null
 : WorkerState

Methods

__construct()

Initializes the worker and immediately forks the process.

 : mixed
getErrorOutput()

Returns the captured error output produced by this worker.

 : string
getExitCode()

Returns the worker exit code when it exits normally.

 : int|null
getOutput()

Returns the captured standard output produced by this worker.

 : string
getPid()

Returns the process identifier (PID) of this worker.

 : int
getStatus()

Returns the raw status reported by the operating system, when available.

 : int|null
getTerminationSignal()

Returns the signal that terminated the worker.

 : Signal|null
isRunning()

Indicates whether this worker is still running.

 : bool
kill()

Sends a signal to this worker.

 : void
wait()

Waits until this worker has finished execution.

 : void
detectCurrentPid()

Detects the current process identifier (PID).

 : int
executeCallback()

Executes the worker callback and normalizes the exit code.

 : int
isCurrentWorkerProcess()

Determines whether the current process corresponds to this worker.

 : bool
isInterruptedWait()

Determines whether a wait failure was caused by an interrupted system call.

 : bool
isNoChildError()

Determines whether no child processes remain.

 : bool
isNoSuchProcessError()

Determines whether the process no longer exists.

 : bool
normalizeExitCode()

Normalizes a callback result into a valid exit code.

 : int
pollState()

Performs a non-blocking state synchronization with the worker process.

 : void
startForkedWorker()

Forks the current process and initializes parent/child execution paths.

 : void
Constants

Constants

INTERRUPTED_SYSTEM_CALL_ERROR

Private

POSIX error code for interrupted system calls.

private mixed INTERRUPTED_SYSTEM_CALL_ERROR = 4

NO_CHILD_PROCESS_ERROR

Private

POSIX error code for missing child processes.

private mixed NO_CHILD_PROCESS_ERROR = 10

NO_SUCH_PROCESS_ERROR

Private

POSIX error code for non-existent processes.

private mixed NO_SUCH_PROCESS_ERROR = 3
Properties

$callback

Private

Stores the callback executed inside the child process.

private callable(WorkerInterface): mixed $callback

Description

The callback MUST accept the current worker instance as its only argument.

$logger

Private
private LoggerInterface|null $logger = null

$state

Private
private WorkerState $state
Methods

__construct()

Public

Initializes the worker and immediately forks the process.

public __construct(ForkManagerInterface  $manager, WorkerState  $state, callable(WorkerInterface): mixed  $callback[, LoggerInterface|null  $logger = null]) : mixed

Description

The constructor MUST trigger process forking and configure both parent and child execution contexts accordingly.

Parameters

Description

manager responsible for this worker

$state : WorkerState

Description

shared mutable state between parent and child

$callback : callable(WorkerInterface): mixed

Description

callback executed in the child process

$logger : LoggerInterface|null = null

Description

logger used for lifecycle events

getErrorOutput()

Public

Returns the captured error output produced by this worker.

public getErrorOutput() : string
Return values
string

Description

the captured error output

getExitCode()

Public

Returns the worker exit code when it exits normally.

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

Description

the exit code or null if not applicable

getOutput()

Public

Returns the captured standard output produced by this worker.

public getOutput() : string
Return values
string

Description

the captured output

getPid()

Public

Returns the process identifier (PID) of this worker.

public getPid() : int
Return values
int

Description

the worker process identifier

getStatus()

Public

Returns the raw status reported by the operating system, when available.

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

Description

the raw process status or null if unavailable

getTerminationSignal()

Public

Returns the signal that terminated the worker.

public getTerminationSignal() : Signal|null
Return values
Signal|null

Description

the terminating signal or null if not applicable

isRunning()

Public

Indicates whether this worker is still running.

public isRunning() : bool
Return values
bool

Description

true if the worker is running; otherwise false

kill()

Public

Sends a signal to this worker.

public kill([Signal  $signal = Signal::Terminate]) : void

Description

This method attempts to send a POSIX signal to the worker process. If the process no longer exists, the worker SHALL be marked as detached.

Parameters

Description

the signal to send to the worker

wait()

Public

Waits until this worker has finished execution.

public wait() : void

Description

This method SHALL block until the worker finishes execution. It MUST NOT be invoked from within the same worker process.

detectCurrentPid()

Private

Detects the current process identifier (PID).

private detectCurrentPid() : int
Tags
throws

Description

if the PID cannot be determined

Return values
int

Description

the current process ID

executeCallback()

Private

Executes the worker callback and normalizes the exit code.

private executeCallback() : int

Description

Output buffering MUST be used to capture standard output and forward it to the parent process. Errors SHALL be intercepted and written to the worker error output channel.

Return values
int

Description

normalized process exit code (0–255)

isCurrentWorkerProcess()

Private

Determines whether the current process corresponds to this worker.

private isCurrentWorkerProcess() : bool
Return values
bool

Description

true if executing inside this worker process

isInterruptedWait()

Private

Determines whether a wait failure was caused by an interrupted system call.

private isInterruptedWait( int  $error) : bool
Parameters
$error : int

Description

error code returned by pcntl

Return values
bool

Description

true if interrupted

isNoChildError()

Private

Determines whether no child processes remain.

private isNoChildError( int  $error) : bool
Parameters
$error : int

Description

error code returned by pcntl

Return values
bool

Description

true if no child processes exist

isNoSuchProcessError()

Private

Determines whether the process no longer exists.

private isNoSuchProcessError( int  $error) : bool
Parameters
$error : int

Description

error code returned by POSIX APIs

Return values
bool

Description

true if the target process is gone

normalizeExitCode()

Private

Normalizes a callback result into a valid exit code.

private normalizeExitCode( mixed  $result) : int

Description

Integers MUST be clamped to the range 0–255. A boolean false SHALL be converted to exit code 1. Any other value SHALL result in exit code 0.

Parameters
$result : mixed

Description

callback return value

Return values
int

Description

normalized exit code

pollState()

Private

Performs a non-blocking state synchronization with the worker process.

private pollState() : void

Description

This method MUST only operate in the master process context.

Tags
throws

Description

if waiting for the worker fails unexpectedly

startForkedWorker()

Private

Forks the current process and initializes parent/child execution paths.

private startForkedWorker() : void

Description

The parent process SHALL retain control and track the worker. The child process MUST execute the provided callback and terminate.

Tags
throws

Description

if the fork operation fails