Swoole\Server->on('WorkerStop', fn)

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Server->on('WorkerStop', callable $callback)

Parameters

event

The event callback name.

callback

Callable event function.

Return

success

If success, it returns true, otherwise it returns false.

Description

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.

Example

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)
{
    // ...
});
Last updated on August 31, 2022