1 | <?php |
||||
2 | |||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
3 | namespace Hhxsv5\LaravelS\Swoole\Task; |
||||
4 | |||||
5 | use Swoole\Timer; |
||||
6 | |||||
7 | abstract class BaseTask |
||||
0 ignored issues
–
show
|
|||||
8 | { |
||||
9 | /** |
||||
10 | * The number of seconds before the task should be delayed. |
||||
11 | * @var int |
||||
0 ignored issues
–
show
|
|||||
12 | */ |
||||
13 | protected $delay = 0; |
||||
14 | |||||
15 | /** |
||||
16 | * The number of tries. |
||||
17 | * @var int |
||||
0 ignored issues
–
show
|
|||||
18 | */ |
||||
19 | protected $tries = 1; |
||||
20 | |||||
21 | /** |
||||
22 | * Delay in seconds, null means no delay. |
||||
23 | * @param int $delay |
||||
0 ignored issues
–
show
|
|||||
24 | * @return $this |
||||
0 ignored issues
–
show
|
|||||
25 | */ |
||||
26 | public function delay($delay) |
||||
27 | { |
||||
28 | if ($delay < 0) { |
||||
29 | throw new \InvalidArgumentException('The delay must be greater than or equal to 0'); |
||||
30 | } |
||||
31 | $this->delay = (int)$delay; |
||||
32 | return $this; |
||||
33 | } |
||||
34 | |||||
35 | /** |
||||
36 | * Return the delay time. |
||||
37 | * @return int |
||||
0 ignored issues
–
show
|
|||||
38 | */ |
||||
39 | public function getDelay() |
||||
40 | { |
||||
41 | return $this->delay; |
||||
42 | } |
||||
43 | |||||
44 | /** |
||||
45 | * Set the number of tries. |
||||
46 | * @param int $tries |
||||
0 ignored issues
–
show
|
|||||
47 | * @return $this |
||||
0 ignored issues
–
show
|
|||||
48 | */ |
||||
49 | public function setTries($tries) |
||||
50 | { |
||||
51 | if ($tries < 1) { |
||||
52 | throw new \InvalidArgumentException('The number of attempts must be greater than or equal to 1'); |
||||
53 | } |
||||
54 | $this->tries = (int)$tries; |
||||
55 | return $this; |
||||
56 | } |
||||
57 | |||||
58 | /** |
||||
59 | * Get the number of tries. |
||||
60 | * @return int |
||||
0 ignored issues
–
show
|
|||||
61 | */ |
||||
62 | public function getTries() |
||||
63 | { |
||||
64 | return $this->tries; |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * Deliver a task |
||||
69 | * @param mixed $task The task object |
||||
0 ignored issues
–
show
|
|||||
70 | * @return bool |
||||
0 ignored issues
–
show
|
|||||
71 | */ |
||||
72 | protected function task($task) |
||||
73 | { |
||||
74 | static $dispatch; |
||||
75 | if (!$dispatch) { |
||||
76 | $dispatch = static function ($task) { |
||||
77 | /**@var \Swoole\Http\Server $swoole */ |
||||
0 ignored issues
–
show
|
|||||
78 | $swoole = app('swoole'); |
||||
0 ignored issues
–
show
The function
app was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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 | } |