MyQEE\Server\Worker::timeTick PHP Method

timeTick() protected method

如果你有一个定时器任务会在每个进程上运行, 但是又不希望所有的定时器在同一刹那执行, 那么用这个方法非常适合, 它可以根据进程数将定时器执行的时间分散开. 例如你启动了10个worker进程, 定时器是间隔10秒执行1次, 那么正常情况下, 这10个进程会在同1秒执行, 在下一个10秒又同时执行... 而通过本方法添加的定时器是这样执行的: 进程1会在 00, 10, 20, 30, 40, 50秒执行, 进程2会在 01, 11, 21, 31, 41, 51秒执行, .... 进程9会在 09, 19, 29, 39, 49, 59秒执行. 每个进程运行的间隔仍旧是10秒钟, 但是它不会和其它进程在同一时间执行
protected timeTick ( integer $interval, string | array | Closure $callback, mixed | null $params = null )
$interval integer 时间间隔, 单位: 毫秒
$callback string | array | Closure 回调函数
$params mixed | null
    protected function timeTick($interval, $callback, $params = null)
    {
        $aTime = intval($interval * $this->id / $this->server->setting['worker_num']);
        $mTime = intval(microtime(1) * 1000);
        $aTime += $interval * ceil($mTime / $interval) - $mTime;
        # 增加一个延迟执行的定时器
        swoole_timer_after($aTime, function () use($interval, $callback, $params) {
            # 添加定时器
            swoole_timer_tick($interval, $callback, $params);
        });
    }