Skip to content

Commit d5992c4

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents d6b41bb + 274e42f commit d5992c4

File tree

6 files changed

+128
-4
lines changed

6 files changed

+128
-4
lines changed

laravel/app/Events/TestEvent.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* 创建事件类--异步事件
4+
* 此特性依赖Swoole的AsyncTask,必须先设置config/laravels.php的swoole.task_worker_num。
5+
* 异步事件的处理能力受Task进程数影响,需合理设置task_worker_num。
6+
* User: lisgroup
7+
* Date: 19-4-13
8+
* Time: 下午10:23
9+
*/
10+
11+
namespace App\Events;
12+
13+
use Hhxsv5\LaravelS\Swoole\Task\Event;
14+
15+
class TestEvent extends Event
16+
{
17+
private $data;
18+
19+
public function __construct($data)
20+
{
21+
$this->data = $data;
22+
}
23+
24+
public function getData()
25+
{
26+
return $this->data;
27+
}
28+
}

laravel/app/Http/Controllers/AuthController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use App\Events\LoginEvent;
99
use App\Http\Repository\UserRepository;
10+
use Hhxsv5\LaravelS\Swoole\Task\Event;
1011
use Jenssegers\Agent\Agent;
1112

1213
class AuthController extends Controller
@@ -52,6 +53,7 @@ public function login()
5253

5354
// 登录成功,触发事件
5455
event(new LoginEvent(auth('api')->user(), new Agent(), \Request::getClientIp(), time()));
56+
// Event::fire(new LoginEvent(auth('api')->user(), new Agent(), \Request::getClientIp(), time()));
5557

5658
$data = [
5759
'access_token' => $token,

laravel/app/Listeners/LoginListener.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
namespace App\Listeners;
44

55
use App\Events\LoginEvent;
6+
use Hhxsv5\LaravelS\Swoole\Task\Event;
7+
use Hhxsv5\LaravelS\Swoole\Task\Listener;
68
use Illuminate\Contracts\Queue\ShouldQueue;
79
use Illuminate\Support\Facades\DB;
810
use Zhuzhichao\IpLocationZh\Ip;
911

1012
class LoginListener implements ShouldQueue
1113
{
14+
// 声明没有参数的构造函数
15+
public function __construct()
16+
{
17+
}
18+
1219
/**
1320
* 失败重试次数
1421
*
@@ -21,7 +28,7 @@ class LoginListener implements ShouldQueue
2128
*
2229
* @param LoginEvent $event
2330
*/
24-
public function handle(LoginEvent $event)
31+
public function handle(Event $event)
2532
{
2633
//获取事件中保存的信息
2734
$user = $event->getUser();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* 创建监听器类
5+
* User: lisgroup
6+
* Date: 19-4-13
7+
* Time: 下午10:28
8+
*/
9+
10+
namespace App\Listeners;
11+
12+
use App\Tasks\TestTask;
13+
use Hhxsv5\LaravelS\Swoole\Task\Task;
14+
use Hhxsv5\LaravelS\Swoole\Task\Event;
15+
use Hhxsv5\LaravelS\Swoole\Task\Listener;
16+
17+
class TestListener extends Listener
18+
{
19+
// 声明没有参数的构造函数
20+
public function __construct()
21+
{
22+
}
23+
24+
public function handle(Event $event)
25+
{
26+
\Log::info(__CLASS__.':handle start', [$event->getData()]);
27+
sleep(2);// 模拟一些慢速的事件处理
28+
// 监听器中也可以投递Task,但不支持Task的finish()回调。
29+
// 注意:
30+
// 1.参数2需传true
31+
// 2.config/laravels.php中修改配置 task_ipc_mode 为1或2,参考 https://wiki.swoole.com/wiki/page/296.html
32+
// $ret = Task::deliver(new TaskEvent('task data'), true);
33+
// var_dump($ret);
34+
// throw new \Exception('an exception');// handle时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
35+
}
36+
}

laravel/app/Tasks/TestTask.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

laravel/config/laravels.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,29 @@
2929
'processes' => [
3030
],
3131
'timer' => [
32-
'enable' => false,
32+
'enable' => false, // 开启定时任务类
3333
'jobs' => [
34+
// 启用LaravelScheduleJob来执行`php artisan schedule:run`,每分钟一次,替代Linux Crontab
35+
// \Hhxsv5\LaravelS\Illuminate\LaravelScheduleJob::class,
36+
// 两种配置参数的方式:
37+
// [\App\Jobs\Timer\TestCronJob::class, [1000, true]], // 注册时传入参数
38+
\App\Jobs\Timer\TestCronJob::class, // 重载对应的方法来返回参数
3439
// Enable LaravelScheduleJob to run `php artisan schedule:run` every 1 minute, replace Linux Crontab
3540
//\Hhxsv5\LaravelS\Illuminate\LaravelScheduleJob::class,
3641
// Two ways to configure parameters:
3742
// [\App\Jobs\XxxCronJob::class, [1000, true]], // Pass in parameters when registering
3843
// \App\Jobs\XxxCronJob::class, // Override the corresponding method to return the configuration
3944
],
45+
'max_wait_time' => 5, // Reload 时最大等待时间
4046
],
41-
'events' => [
47+
// 绑定事件与监听器,一个事件可以有多个监听器,多个监听器按顺序执行
48+
'events' => [
49+
\App\Events\TestEvent::class => [
50+
\App\Listeners\TestListener::class,
51+
],
52+
// App\Events\LoginEvent::class => [
53+
// App\Listeners\LoginListener::class,
54+
// ],
4255
],
4356
'swoole_tables' => [
4457
],
@@ -56,7 +69,7 @@
5669
'dispatch_mode' => 2,
5770
'reactor_num' => function_exists('\swoole_cpu_num') ? \swoole_cpu_num() * 2 : 4,
5871
'worker_num' => function_exists('\swoole_cpu_num') ? \swoole_cpu_num() * 2 : 8,
59-
//'task_worker_num' => function_exists('\swoole_cpu_num') ? \swoole_cpu_num() * 2 : 8,
72+
'task_worker_num' => function_exists('\swoole_cpu_num') ? \swoole_cpu_num() * 2 : 8,
6073
'task_ipc_mode' => 1,
6174
'task_max_request' => 5000,
6275
'task_tmpdir' => @is_writable('/dev/shm/') ? '/dev/shm' : '/tmp',

0 commit comments

Comments
 (0)