|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole\Task; |
|
4
|
|
|
|
|
5
|
|
|
use Swoole\Timer; |
|
6
|
|
|
|
|
7
|
|
|
abstract class BaseTask |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The number of seconds before the task should be delayed. |
|
11
|
|
|
* @var int |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $delay = 0; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The number of tries. |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $tries = 1; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Delay in seconds, null means no delay. |
|
23
|
|
|
* @param int $delay |
|
24
|
|
|
* @return $this |
|
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 |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getDelay() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->delay; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set the number of tries. |
|
46
|
|
|
* @param int $tries |
|
47
|
|
|
* @return $this |
|
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 |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getTries() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->tries; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Deliver a task |
|
69
|
|
|
* @param mixed $task The task object |
|
70
|
|
|
* @return bool|mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function task($task) |
|
73
|
|
|
{ |
|
74
|
|
|
$deliver = function () use ($task) { |
|
75
|
|
|
/**@var \Swoole\Http\Server $swoole */ |
|
76
|
|
|
$swoole = app('swoole'); |
|
77
|
|
|
if ($swoole->taskworker) { |
|
78
|
|
|
$taskWorkerNum = isset($swoole->setting['task_worker_num']) ? (int)$swoole->setting['task_worker_num'] : 0; |
|
79
|
|
|
if ($taskWorkerNum < 2) { |
|
80
|
|
|
throw new \InvalidArgumentException('LaravelS: async task needs to set task_worker_num >= 2'); |
|
81
|
|
|
} |
|
82
|
|
|
$workerNum = isset($swoole->setting['worker_num']) ? $swoole->setting['worker_num'] : 0; |
|
83
|
|
|
$totalNum = $workerNum + $taskWorkerNum; |
|
84
|
|
|
|
|
85
|
|
|
$getAvailableId = function ($startId, $endId, $excludeId) { |
|
86
|
|
|
$ids = range($startId, $endId); |
|
87
|
|
|
$ids = array_flip($ids); |
|
88
|
|
|
unset($ids[$excludeId]); |
|
89
|
|
|
return array_rand($ids); |
|
90
|
|
|
}; |
|
91
|
|
|
$availableId = $getAvailableId($workerNum, $totalNum - 1, $swoole->worker_id); |
|
92
|
|
|
return $swoole->sendMessage($task, $availableId); |
|
93
|
|
|
} else { |
|
94
|
|
|
$taskId = $swoole->task($task); |
|
95
|
|
|
return $taskId !== false; |
|
96
|
|
|
} |
|
97
|
|
|
}; |
|
98
|
|
|
if ($this->delay > 0) { |
|
99
|
|
|
Timer::after($this->delay * 1000, $deliver); |
|
100
|
|
|
return true; |
|
101
|
|
|
} else { |
|
102
|
|
|
return $deliver(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |