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

Declaration

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

Parameters

event

The event name

callback

callback function

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

Executue the callback function when Worker Process in the Server is stopped.

In the callback function registered on event workerStop, you can retrieve or release the resource for 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.

Callback function

<?php
function (Swoole\Server $server, int $worker_id) {

}

Example

Modify the process name when a new worker process starts.

<?php
$serv->on('WorkerStart', function ($serv, $worker_id){
    global $argv;
    if($worker_id >= $serv->setting['worker_num']) {
        swoole_set_process_name("php {$argv[0]} task worker");
    } else {
        swoole_set_process_name("php {$argv[0]} event worker");
    }
});