workerman icon indicating copy to clipboard operation
workerman copied to clipboard

How detect stop server event ?

Open micromotores opened this issue 3 years ago • 4 comments

I want to listen for a server stop event to take an action if this happens

micromotores avatar Jun 16 '22 13:06 micromotores

Use Worker::$onMasterStop like this.

Worker::$onMasterStop = function(){
    echo 'stoped';
};

walkor avatar Jun 16 '22 14:06 walkor

I tried it from php but it doesn't execute anything, I think what you give me is focused on javascript or I'm wrong

micromotores avatar Jun 16 '22 17:06 micromotores

test.php

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Protocols\Http\Request;
use Workerman\Worker;
use Workerman\Connection\TcpConnection;

Worker::$onMasterStop = function () {
    echo "stoped\n";
};
$worker = new Worker('http://127.0.0.1:1234');
$worker->onMessage = function (TcpConnection $connection, Request $request) {
    $connection->send('hello');
};
Worker::runAll();

Run command php start.php start. image

Run command php start.php stop in another console. image

The first console printed 'stoped' image

walkor avatar Jun 17 '22 02:06 walkor

Thank you very much, do you know if there is any way to automatically initialize the server if it stops due to an error or for any reason?

micromotores avatar Jun 21 '22 12:06 micromotores