File tree Expand file tree Collapse file tree 3 files changed +39
-29
lines changed Expand file tree Collapse file tree 3 files changed +39
-29
lines changed Original file line number Diff line number Diff line change 9
9
10
10
namespace App \Listeners ;
11
11
12
- use App \Tasks \TaskEvent ;
12
+ use App \Tasks \TestTask ;
13
13
use Hhxsv5 \LaravelS \Swoole \Task \Task ;
14
14
use Hhxsv5 \LaravelS \Swoole \Task \Event ;
15
15
use Hhxsv5 \LaravelS \Swoole \Task \Listener ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * 创建异步任务类
4
+ * User: lisgroup
5
+ * Date: 19-4-13
6
+ * Time: 下午10:23
7
+ */
8
+
9
+ namespace App \Tasks ;
10
+
11
+ use Hhxsv5 \LaravelS \Swoole \Task \Task ;
12
+
13
+ class TestTask extends Task
14
+ {
15
+ private $ data ;
16
+ private $ result ;
17
+
18
+ public function __construct ($ data )
19
+ {
20
+ $ this ->data = $ data ;
21
+ }
22
+
23
+ // 处理任务的逻辑,运行在Task进程中,不能投递任务
24
+ public function handle ()
25
+ {
26
+ \Log::info (__CLASS__ .':handle start ' , [$ this ->data ]);
27
+ sleep (2 );// 模拟一些慢速的事件处理
28
+ // throw new \Exception('an exception');// handle时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
29
+ $ this ->result = 'the result of ' .$ this ->data ;
30
+ }
31
+
32
+ // 可选的,完成事件,任务处理完后的逻辑,运行在Worker进程中,可以投递任务
33
+ public function finish ()
34
+ {
35
+ \Log::info (__CLASS__ .':finish start ' , [$ this ->result ]);
36
+ // Task::deliver(new TestTask2('task2')); // 投递其他任务
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments