workerman icon indicating copy to clipboard operation
workerman copied to clipboard

Timer::add in master process is impossible ? I think it can be

Open webrobot1 opened this issue 6 months ago • 1 comments

At some situation i need add Timer at master process, but it look like imposible because master have monitorWorkersForLinux method with pcntl_wait function what will be block master process until worker will not shutdown or master will get somу signal. While it was blocking no one Event library can't run timer...but...

Some Event library has own pcntl_wait analogs , like Swoole\Coroutine\System::wait in Swoole (https://wiki.swoole.com/#/coroutine/system?id=wait). It will do same things as pcntl_wait but additional Timer can be work in master process

Is it possible change monitorWorkersForLinux $pid = \pcntl_wait($status, \WUNTRACED);->static::$globalEvent->wait() ? And EventInterface need be abstract with share method wait by default have pcntl_wait (except Swoole) and return array ['pid'=>$pid, 'signal'=>$status] (like a Swoole\Coroutine\System::wait return array's format) or stay like Interface but need have abstract method wait():array (but in this realisation each Event need wait method with pcntl_wait except Swoole)

Because not it impossible change monitorWorkersForLinux by extends class , because it have private property , additionaly this method have a lot of code, but only need change one line of code

@walkor

webrobot1 avatar Jan 06 '24 10:01 webrobot1

Hi webrobot1

Workerman support timer in the master process, and the code is similar to this

$worker = new Worker('http://0.0.0.0:1234');
// The timer will only run in master process
Timer::add(1, function() {
    echo "hello";
});
Worker::runAll();

take care The timer of the main process only supports second level. It is not recommended to initialize database or Redis resource class connections in the main process, as these connection resources may be inherited by child processes, leading to unforeseen problems

walkor avatar Jan 12 '24 02:01 walkor