walkor / workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
http://www.workerman.net
MIT License
11.03k stars 2.25k forks source link

Kill worker in linux #1005

Closed xenion54 closed 5 months ago

xenion54 commented 5 months ago

In linux, this code not killing worker

<?php

use Workerman\Connection\TcpConnection;
use Workerman\Timer;

require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Libs' . DIRECTORY_SEPARATOR . 'init.php';

global $argv;

$argv[1] = 'start';
$argv[2] = '-d';

$sw = new \Workerman\Worker('websocket://0.0.0.0:7856' );
$sw->count = 1;

$sw->onWorkerStart = function (\Workerman\Worker $worker) {

    Timer::add(5, function () {
        \Workerman\Worker::stopAll();
    });

};

\Workerman\Worker::runAll();

How to kill him from inside?

xenion54 commented 5 months ago

as workaround, now using

$master_pid = file_get_contents(\Workerman\Worker::$pidFile);
unlink(\Workerman\Worker::$pidFile);
posix_kill($master_pid, SIGKILL);
walkor commented 5 months ago

The meaning of \Workerman\Worker::stopAll() is to stop all worker instances of the current process. If it runs in a child process, this means stopping the current child process. If you want to stop all processes, you can use posix_kill(posix_getppid(), SIGINT);.

xenion54 commented 5 months ago

The meaning of \Workerman\Worker::stopAll() is to stop all worker instances of the current process. If it runs in a child process, this means stopping the current child process. If you want to stop all processes, you can use posix_kill(posix_getppid(), SIGINT);.

Thx for answer. It looks better, than my workaround. Maby add Workerman::stopParent() method with this realization?

Intuitively, when I call the stopAll method, I expect everyone, including the parent process, to be stopped. Can you adjust the behavior of the application? Like if its child process, call "posix_kill(posix_getppid(), SIGINT);"

walkor commented 5 months ago

In order to be compatible with older versions, I am unable to adjust the behavior of Worker::stopAll(). Perhaps in the future, we may consider adding an interface similar to stopParent().