1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/vuongxuongminh/yii2-async |
4
|
|
|
* @copyright Copyright (c) 2019 Vuong Xuong Minh |
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace vxm\async; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use Closure; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
use yii\base\Component; |
15
|
|
|
|
16
|
|
|
use Spatie\Async\Pool; |
17
|
|
|
use Spatie\Async\Process\Runnable; |
18
|
|
|
|
19
|
|
|
use vxm\async\event\Event; |
20
|
|
|
use vxm\async\event\ErrorEvent; |
21
|
|
|
use vxm\async\event\SuccessEvent; |
22
|
|
|
use vxm\async\runtime\ParentRuntime; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Support run code async. To use it, you just config it to your application components in configure file: |
26
|
|
|
* |
27
|
|
|
* ```php |
28
|
|
|
* 'components' => [ |
29
|
|
|
* 'async' => 'vxm\async\Async' |
30
|
|
|
* ] |
31
|
|
|
* |
32
|
|
|
* ``` |
33
|
|
|
* |
34
|
|
|
* And after that you can run an async code: |
35
|
|
|
* |
36
|
|
|
* ```php |
37
|
|
|
* |
38
|
|
|
* Yii::$app->async->run(function () { |
39
|
|
|
* |
40
|
|
|
* sleep(15); |
41
|
|
|
* }); |
42
|
|
|
* |
43
|
|
|
* ``` |
44
|
|
|
* |
45
|
|
|
* If you want to wait until task done, you just to call [[wait]]. |
46
|
|
|
* |
47
|
|
|
* ```php |
48
|
|
|
* |
49
|
|
|
* Yii::$app->async->run(function () { |
50
|
|
|
* |
51
|
|
|
* sleep(15); |
52
|
|
|
* })->wait(); |
53
|
|
|
* |
54
|
|
|
* ``` |
55
|
|
|
* |
56
|
|
|
* Run multi tasks: |
57
|
|
|
* |
58
|
|
|
* ```php |
59
|
|
|
* |
60
|
|
|
* Yii::$app->async->run(function () { |
61
|
|
|
* |
62
|
|
|
* sleep(15); |
63
|
|
|
* }); |
64
|
|
|
* |
65
|
|
|
* Yii::$app->async->run(function () { |
66
|
|
|
* |
67
|
|
|
* sleep(15); |
68
|
|
|
* }); |
69
|
|
|
* |
70
|
|
|
* Yii::$app->async->wait(); // sleep 30s |
71
|
|
|
* ``` |
72
|
|
|
* |
73
|
|
|
* @property string $autoload path of autoload file for runtime task execute environment. |
74
|
|
|
* @property int $sleepTimeWait time to sleep on wait tasks execute. |
75
|
|
|
* @property int $concurrency tasks executable. |
76
|
|
|
* @property int $timeout of task executable. |
77
|
|
|
* |
78
|
|
|
* @author Vuong Minh <[email protected]> |
79
|
|
|
* @since 1.0.0 |
80
|
|
|
*/ |
81
|
|
|
class Async extends Component |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @event SuccessEvent an event that is triggered when task done. |
86
|
|
|
*/ |
87
|
|
|
const EVENT_SUCCESS = 'success'; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @event ErrorEvent an event that is triggered when task error. |
91
|
|
|
*/ |
92
|
|
|
const EVENT_ERROR = 'error'; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @event \yii\base\Event an event that is triggered when task timeout. |
96
|
|
|
*/ |
97
|
|
|
const EVENT_TIMEOUT = 'timeout'; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var Pool handling tasks. |
101
|
|
|
*/ |
102
|
|
|
protected $pool; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var string a file config of an application run in child process. |
106
|
|
|
* Note: If an autoload file's set, it will not affect, you need to invoke an app in your autoload if needed. |
107
|
|
|
*/ |
108
|
|
|
protected $appConfigFile; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Async constructor. |
112
|
|
|
* |
113
|
|
|
* @param array $config |
114
|
|
|
* @throws \yii\base\InvalidConfigException |
115
|
|
|
*/ |
116
|
8 |
|
public function __construct($config = []) |
117
|
|
|
{ |
118
|
8 |
|
$pool = $this->pool = Yii::createObject(Pool::class); |
119
|
8 |
|
$pool->autoload(__DIR__ . '/RuntimeAutoload.php'); |
120
|
|
|
|
121
|
8 |
|
parent::__construct($config); |
122
|
8 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Execute async task. |
126
|
|
|
* |
127
|
|
|
* @param callable|\Spatie\Async\Task|Task $callable need to execute. |
128
|
|
|
* @param array $callbacks event. Have key is an event name, value is a callable triggered when event happen, |
129
|
|
|
* have three events `error`, `success`, `timeout`. |
130
|
|
|
* @return static |
131
|
|
|
*/ |
132
|
8 |
|
public function run($callable, array $callbacks = []): self |
133
|
|
|
{ |
134
|
8 |
|
$process = $this->createProcess($callable); |
135
|
|
|
|
136
|
8 |
|
foreach ($callbacks as $name => $callback) { |
137
|
|
|
|
138
|
4 |
|
switch (strtolower($name)) { |
139
|
4 |
|
case 'success': |
140
|
2 |
|
$process->then(Closure::fromCallable($callback)); |
141
|
2 |
|
break; |
142
|
2 |
|
case 'error': |
143
|
1 |
|
case 'catch': |
144
|
1 |
|
$process->catch(Closure::fromCallable($callback)); |
145
|
1 |
|
break; |
146
|
1 |
|
case 'timeout': |
147
|
1 |
|
$process->timeout(Closure::fromCallable($callback)); |
148
|
1 |
|
break; |
149
|
|
|
default: |
150
|
4 |
|
break; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
8 |
|
$this->pool->add($process); |
|
|
|
|
156
|
|
|
|
157
|
8 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* This method is called when task executed success. |
162
|
|
|
* When overriding this method, make sure you call the parent implementation to ensure the |
163
|
|
|
* event is triggered. |
164
|
|
|
* |
165
|
|
|
* @param mixed $output of task executed. |
166
|
|
|
* @throws \yii\base\InvalidConfigException |
167
|
|
|
*/ |
168
|
3 |
|
public function success($output): void |
169
|
|
|
{ |
170
|
3 |
|
$event = Yii::createObject([ |
171
|
3 |
|
'class' => SuccessEvent::class, |
172
|
3 |
|
'output' => $output |
173
|
|
|
]); |
174
|
|
|
|
175
|
3 |
|
$this->trigger(self::EVENT_SUCCESS, $event); |
176
|
3 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* This method is called when task executed error. |
180
|
|
|
* When overriding this method, make sure you call the parent implementation to ensure the |
181
|
|
|
* event is triggered. |
182
|
|
|
* |
183
|
|
|
* @param Throwable $throwable when executing task. |
184
|
|
|
* @throws \yii\base\InvalidConfigException |
185
|
|
|
*/ |
186
|
2 |
|
public function error(Throwable $throwable): void |
187
|
|
|
{ |
188
|
2 |
|
$event = Yii::createObject([ |
189
|
2 |
|
'class' => ErrorEvent::class, |
190
|
2 |
|
'throwable' => $throwable |
191
|
|
|
]); |
192
|
|
|
|
193
|
2 |
|
$this->trigger(self::EVENT_ERROR, $event); |
194
|
2 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* This method is called when task executed timeout. |
198
|
|
|
* When overriding this method, make sure you call the parent implementation to ensure the |
199
|
|
|
* event is triggered. |
200
|
|
|
* |
201
|
|
|
* @throws \yii\base\InvalidConfigException |
202
|
|
|
*/ |
203
|
2 |
|
public function timeout(): void |
204
|
|
|
{ |
205
|
2 |
|
$event = Yii::createObject(Event::class); |
206
|
|
|
|
207
|
2 |
|
$this->trigger(self::EVENT_TIMEOUT, $event); |
208
|
2 |
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Wait until all tasks done. |
212
|
|
|
*/ |
213
|
7 |
|
public function wait(): void |
214
|
|
|
{ |
215
|
7 |
|
$this->pool->wait(); |
216
|
7 |
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Set concurrency process do tasks. |
220
|
|
|
* |
221
|
|
|
* @param int $concurrency |
222
|
|
|
*/ |
223
|
|
|
public function setConcurrency(int $concurrency): void |
224
|
|
|
{ |
225
|
|
|
$this->pool->concurrency($concurrency); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set timeout of task when execute. |
230
|
|
|
* |
231
|
|
|
* @param int $timeout |
232
|
|
|
*/ |
233
|
8 |
|
public function setTimeout(int $timeout): void |
234
|
|
|
{ |
235
|
8 |
|
$this->pool->timeout($timeout); |
236
|
8 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set sleep time when wait tasks execute. |
240
|
|
|
* |
241
|
|
|
* @param int $sleepTimeWait |
242
|
|
|
*/ |
243
|
|
|
public function setSleepTimeWait(int $sleepTimeWait): void |
244
|
|
|
{ |
245
|
|
|
$this->pool->sleepTime($sleepTimeWait); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Set autoload for environment tasks execute. |
250
|
|
|
* @param string $autoload it can use at an alias. |
251
|
|
|
*/ |
252
|
|
|
public function setAutoload(string $autoload): void |
253
|
|
|
{ |
254
|
|
|
$this->pool->autoload(Yii::getAlias($autoload)); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Set an application config file for invoke in child runtime process. |
259
|
|
|
* |
260
|
|
|
* @param string $appConfigFile it can use at an alias. |
261
|
|
|
*/ |
262
|
8 |
|
public function setAppConfigFile(string $appConfigFile): void |
263
|
|
|
{ |
264
|
8 |
|
$this->appConfigFile = Yii::getAlias($appConfigFile); |
|
|
|
|
265
|
8 |
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Create an async process with hooked events. |
269
|
|
|
* |
270
|
|
|
* @param callable|\Spatie\Async\Task|Task $callable need to execute. |
271
|
|
|
* @return Runnable process. |
272
|
|
|
*/ |
273
|
8 |
|
protected function createProcess($callable): Runnable |
274
|
|
|
{ |
275
|
8 |
|
return ParentRuntime::createProcess([$callable, $this->appConfigFile]) |
276
|
8 |
|
->then(Closure::fromCallable([$this, 'success'])) |
277
|
8 |
|
->catch(Closure::fromCallable([$this, 'error'])) |
278
|
8 |
|
->timeout(Closure::fromCallable([$this, 'timeout'])); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: