OpenSwoole Server shutdown

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

Declaration

<?php OpenSwoole\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 OpenSwoole\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)
{
    echo "Shutting down server...";
});

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

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

$server->start();
Last updated on January 15, 2023