-
-
Notifications
You must be signed in to change notification settings - Fork 0
ForkManager
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.
- Full name:
\FastForward\Fork\Manager\ForkManager - This class is marked as final and can't be subclassed
- This class implements:
\FastForward\Fork\Manager\ForkManagerInterface - This class is a Final class
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): mixedThe 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
Determines whether the current environment supports process forking.
public isSupported(): boolAll required PHP functions MUST be available for safe execution.
Return Value:
true if forking is supported; otherwise false
Forks one or more worker processes.
public fork(callable $workerCallback, int $workerCount = 1): \FastForward\Fork\Worker\WorkerGroupInterfaceThe 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
-
LogicExceptionif worker count is invalid -
InvalidArgumentExceptionif worker creation fails RuntimeException
Waits for one or more workers or worker groups to finish execution.
public wait(\FastForward\Fork\Worker\WorkerInterface|\FastForward\Fork\Worker\WorkerGroupInterface $workers): voidIf 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. |
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): voidIf 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. |
Retrieves the PID of the master process.
public getMasterPid(): intReturn Value:
the master process identifier
Determines whether the current process is the master process.
public isMaster(): boolReturn Value:
true if master; otherwise false
Determines whether the current process is a worker process.
public isWorker(): boolReturn Value:
true if worker; otherwise false