Swoole Server shutdown

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Server->shutdown(): void

Parameters

none

Return

success

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

Description

Shutdown the master server process and turn off the server, this function can be called in worker processes.

Signal Shutdowns

It is possible to shutdown a server by sending the system signal SIGTERM:

kill -15 MASTER_PID

Example

<?php
$server = new Swoole\Server("127.0.0.1", 9501);

$server->on('connect', function ($server, $fd)
{
    echo "New connection established: #{$fd}.\n";
});

$server->on('receive', function ($server, $fd, $from_id, $data)
{
    if(trim($data) == "shutdown")
    {
        // Turn off the server
        $server->shutdown();
    }
    else
    {
        $server->send($fd, "Echo to #{$fd}: \n".$data);
        $server->close($fd);
    }
});

// Shutdown event is triggered
$server->on('Shutdown', function($server, $worker_id)
{
    echo "Shutting down server...";
});

$server->on('WorkerStop', function($server, $worker_id)
{
    echo $worker_id . " stop\n";
});

$server->on('close', function ($server, $fd)
{
    echo "Connection closed: #{$fd}.\n";
});

$server->start();
Last updated on August 31, 2022