Conditions | 6 |
Paths | 4 |
Total Lines | 24 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
72 | protected function task($task) |
||
73 | { |
||
74 | static $dispatch; |
||
75 | if (!$dispatch) { |
||
76 | $dispatch = static function ($task) { |
||
77 | /**@var \Swoole\Http\Server $swoole */ |
||
78 | $swoole = app('swoole'); |
||
79 | // The worker_id of timer process is -1 |
||
80 | if ($swoole->worker_id === -1 || $swoole->taskworker) { |
||
81 | $workerNum = isset($swoole->setting['worker_num']) ? $swoole->setting['worker_num'] : 0; |
||
82 | $availableId = mt_rand(0, $workerNum - 1); |
||
83 | return $swoole->sendMessage($task, $availableId); |
||
84 | } |
||
85 | $taskId = $swoole->task($task); |
||
86 | return $taskId !== false; |
||
87 | }; |
||
88 | } |
||
89 | |||
90 | if ($this->delay > 0) { |
||
91 | Timer::after($this->delay * 1000, $dispatch, $task); |
||
92 | return true; |
||
93 | } |
||
94 | |||
95 | return $dispatch($task); |
||
96 | } |
||
97 | } |