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

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\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 OpenSwoole 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 OpenSwoole\Server("127.0.0.1", 9501, OpenSwoole\Server::POOL_MODE, OpenSwoole\Constant::SOCK_TCP);

// This event callback gets passed the server object and the worker process ID (Not the PID)
$server->on('WorkerStop', function(OpenSwoole\Server $server, int $workerId)
{
    // ...
});
Last updated on September 20, 2022