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

Why sse server sometimes can stop itself and how to prevent it? #964

Closed shizzic closed 9 months ago

shizzic commented 9 months ago
2023-10-11 11:00:01 pid:2289644 Workerman[yii] start in DAEMON mode
2023-10-11 11:00:01 pid:2289644 Workerman[yii] already running
2023-10-11 11:02:17 pid:2234040 Workerman[yii] stopping ...
2023-10-11 11:02:17 pid:2234040 Workerman[yii] has been stopped
2023-10-11 11:15:01 pid:2291347 Workerman[yii] start in DAEMON mode
2023-10-11 11:15:01 pid:2291347 Workerman[yii] already running
2023-10-11 11:30:01 pid:2292200 Workerman[yii] start in DAEMON mode
2023-10-11 11:30:01 pid:2292200 Workerman[yii] already running

Firstly, my crontab trying to start the server each 15 minutes. The thing is, as you can see, sometimes it just stops without a reason. There is no problem with functions inside, my logs for errors empty and by that time there weren't any connections (develop stage). And i even commented them for testing, but it continued to happen. Normally, when you trying to stop the server you see this:

2023-10-11 12:45:01 pid:2298829 Workerman[yii] stop 
2023-10-11 12:45:01 pid:2298829 Workerman[yii] is stopping ...
2023-10-11 12:45:01 pid:2298829 Workerman[yii] stop success

But when that strange behaviour occurs, there is:

2023-10-11 11:02:17 pid:2234040 Workerman[yii] stopping ...
2023-10-11 11:02:17 pid:2234040 Workerman[yii] has been stopped

And after, it says "already running", but it doesn't work actually (there is an error on client side about "CONNECTION REFUSED"). Only after manual stop through cmd it can be started again with proper work.

My server:

<?php

namespace backend\modules\sse\controllers;

use backend\modules\v1\models\User;
use Workerman\Worker;
use Workerman\Protocols\Http\Response;
use Workerman\Protocols\Http\ServerSentEvents;
use Workerman\Connection\TcpConnection;
use Workerman\Lib\Timer;
use Yii;
use Exception;

require_once '/var/www/www-root/data/www/myDomain/vendor/autoload.php';

class MainController extends \yii\console\Controller
{
    private $cons;
    private $worker;

    public function actionRun()
    {
        $this->worker = new Worker('http://0.0.0.0:444);
        $this->worker->name = 'sse';
        $this->worker->user = 'www-data';
        $this->worker->count     = 1;
        $this->worker->reloadable = true;

        $this->worker->onMessage = function ($connection, $request) {
            try {
                if ($request->header('accept') === 'text/event-stream') {
                    $user = User::find()->where(['access_token' => $request->cookie()])->one();

                    if ($user) {
                        $response = new Response(
                            200,
                            [
                                'Access-Control-Allow-Origin' => rtrim($request->header('Referer'), '/'),
                                'Access-Control-Allow-Credentials' => 'true',
                                'Content-Type' => 'text/event-stream'
                            ],
                            "\r\n"
                        );
                        $connection->send($response);
                    }
                }
            } catch (Exception $e) {
                print_r($e->getMessage() . "\n");
            }
        };

        Worker::$logFile    = '/var/www/www-root/data/www/myDomain/backend/modules/sse/main.log';
        Worker::$stdoutFile = '/var/www/www-root/data/www/myDomain/backend/modules/sse/custom.log';
        Worker::$daemonize  = true;
        Worker::runAll();
    }
}

Maybe i don't understand an issue, help me pls.