Skip to content

ForkManager

github-actions edited this page Apr 4, 2026 · 1 revision

Coordinates the lifecycle of worker processes using process forking.

This class acts as the central orchestrator responsible for:

  • Creating worker processes
  • Managing their lifecycle and state
  • Handling inter-process communication via streams
  • Propagating and handling system signals

Implementations MUST ensure that process control functions are available. The manager SHALL operate as a master process and MUST NOT be reused inside worker processes.


Methods

__construct

Initializes the manager, validates environment support, and registers signal handlers.

public __construct(\FastForward\Fork\Signal\SignalHandlerInterface $signalHandler = new \FastForward\Fork\Signal\DefaultSignalHandler(), ?\Psr\Log\LoggerInterface $logger = null): mixed

The implementation MUST verify that all required extensions are available. If the environment does not support forking, a RuntimeException MUST be thrown.

Parameters:

Parameter Type Description
$signalHandler \FastForward\Fork\Signal\SignalHandlerInterface handler responsible for reacting to system signals
$logger ?\Psr\Log\LoggerInterface logger used for lifecycle and output events

Throws:

if the environment does not support process forking


isSupported

Determines whether the current environment supports process forking.

public isSupported(): bool

All required PHP functions MUST be available for safe execution.

Return Value:

true if forking is supported; otherwise false


fork

Forks one or more worker processes.

public fork(callable $workerCallback, int $workerCount = 1): \FastForward\Fork\Worker\WorkerGroupInterface

The manager MUST NOT allow forking from within a worker process. The number of workers MUST be greater than zero.

In case of failure during creation, all previously created workers SHALL be terminated.

Parameters:

Parameter Type Description
$workerCallback callable callback executed inside each worker process
$workerCount int number of workers to spawn

Return Value:

a group containing all created workers

Throws:

if called from a worker process


wait

Waits for one or more workers or worker groups to finish execution.

public wait(\FastForward\Fork\Worker\WorkerInterface|\FastForward\Fork\Worker\WorkerGroupInterface $workers): void

If no workers are provided, the manager SHALL wait for all managed workers.

Parameters:

Parameter Type Description
$workers \FastForward\Fork\Worker\WorkerInterface|\FastForward\Fork\Worker\WorkerGroupInterface Workers or groups to wait for.

kill

Sends a signal to one or more workers.

public kill(\FastForward\Fork\Signal\Signal $signal = \FastForward\Fork\Signal\Signal::Terminate, \FastForward\Fork\Worker\WorkerInterface|\FastForward\Fork\Worker\WorkerGroupInterface $workers): void

If no workers are provided, the signal SHALL be sent to all managed workers.

Parameters:

Parameter Type Description
$signal \FastForward\Fork\Signal\Signal signal to send (default: SIGTERM)
$workers \FastForward\Fork\Worker\WorkerInterface|\FastForward\Fork\Worker\WorkerGroupInterface Target workers or groups.

getMasterPid

Retrieves the PID of the master process.

public getMasterPid(): int

Return Value:

the master process identifier


isMaster

Determines whether the current process is the master process.

public isMaster(): bool

Return Value:

true if master; otherwise false


isWorker

Determines whether the current process is a worker process.

public isWorker(): bool

Return Value:

true if worker; otherwise false


Clone this wiki locally