forked from anlityli/yii2-swoole-async-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwooleAsyncTimerComponent.php
56 lines (52 loc) · 1.88 KB
/
SwooleAsyncTimerComponent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* 异步组件
* $Id: SwooleAsyncComponent.php 9507 2016-09-29 06:48:44Z mevyen $
* $Date: 2016-09-29 14:48:44 +0800 (Wed, 07 Sep 2016) $
* $Author: mevyen $
*/
namespace anlity\swooleAsyncTimer;
use Yii;
use anlity\swooleAsyncTimer\src\SCurl;
class SwooleAsyncTimerComponent extends \yii\base\Component
{
/**
* 异步执行入口
* $data.data 定义需要执行的任务列表,其中如果指定多个任务(以数组形式),则server将顺序执行
* $data.finish 定义了data中的任务执行完成后的回调任务,执行方式同$data.data
* @param [json] $data 结构如下
* [
* 'data' => [
* [
* 'a' => 'test1/mail1' #要执行的console控制器和action
* 'p' => ['p1','p2','p3'] // action参数列表
* ],
* [
* 'a' => 'test2/mail2' #要执行的console控制器和action
* 'p' => ['p1','p2','p3'] // action参数列表
* ]
* ],
* 'finish' => [
* [
* 'a' => 'test3/mail3' #要执行的console控制器和action
* 'p' => ['p1','p2','p3'] // action参数列表
* ],
* [
* 'a' => 'test4/mail4' #要执行的console控制器和action
* 'p' => ['p1','p2','p3'] // action参数列表
* ]
* ]
* ]
* @return [type] [description]
*/
public function async($data)
{
$settings = Yii::$app->params['swooleAsyncTimer'];
$curl = new SCurl();
$curl->setOption(CURLOPT_POSTFIELDS, ["data"=>$data]);
$curl->setOption(CURLOPT_TIMEOUT, $settings['client_timeout']);
$times = 0;
$response = $curl->post("http://".$settings['host'].":".$settings['port']);
return $response===false ? false : true;
}
}