Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 25.x
Latest version:
pecl install openswoole-25.2.0
<?php Swoole\Server->on('WorkerStop', callable $callback)
The event callback name.
Callable event function.
If success, it returns true, otherwise it returns false.
Execute the callback function when a Worker Process is stopped by the server. This event occurs when a worker process gets terminated, within this WorkerStop event you can use this to recover from a worker being restarted or stopped. It gives you chance to process any resources before the worker is either restarted or terminated completely. This event can happen when Swoole is restarting a worker or when the server has been shutdown normally.
In the callback function registered for the WorkerStop event, you can retrieve or release resources from the worker process which is stopping.
The abnormal stop of a worker process doesn't trigger the event WorkerStop, such as fatal error or core dump doesn't trigger WorkerStop event.
Before these examples, if you need to see how a full working server is setup see the onStart event.
Within this event callback you should not use any asynchronous or coroutine code as the worker process does not have access to any event loop facilities at this stage.
<?php
$server = new Swoole\Server("127.0.0.1", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
// This event callback gets passed the server object and the worker process ID (Not the PID)
$server->on('WorkerStop', function(Swoole\Server $server, int $workerId)
{
// ...
});