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

Declaration

<?php
Swoole\Server->on('shutdown', 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 the Server is shuttng down.

Before the stage shutdown started, these steps are finished:

  • All threads are stopped.
  • All the worker processed are stopped.
  • The TCP/UDP ports is closed.
  • The reactor is closed.

Force killing a process using kill -9, doesn't trigger the callback function on shutdown. Use signal SIGTREM and kill -15 instead.

Example

<?php
$server = new Swoole\Server("127.0.0.1", 9501);
$server->on('start', function ($server){
    echo "Server is started.\n";
});
$server->on('shutdown', function ($server){
    echo "Server is shuttng down.\n";
});
$server->on('connect', function ($server, $fd){
    echo "New connection established: #{$fd}.\n";
});
$server->on('receive', function ($server, $fd, $from_id, $data) {
    $server->send($fd, "Echo to #{$fd}: \n".$data);
    $server->close($fd);
});
$server->on('close', function ($server, $fd) {
    echo "Connection closed: #{$fd}.\n";
});
$server->start();