Skip to content

Commit e811bc7

Browse files
committed
update cli 模式下使用 laravels Task 异步事件
1 parent 109478d commit e811bc7

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

laravel/app/Http/Controllers/Api/ApiExcelController.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace App\Http\Controllers\Api;
44

55
use App\Events\ApiExcelEvent;
6+
use App\Events\ApiExcelSwooleEvent;
67
use App\Http\Repository\ApiRepository;
78
use App\Http\Repository\ExcelRepository;
89
use App\Http\Requests\ApiExcel\Store;
910
use App\Http\Requests\ApiExcel\Update;
1011
use App\Models\ApiExcel;
1112
use App\Http\Controllers\Controller;
13+
use Hhxsv5\LaravelS\Swoole\Task\Event;
1214
use Illuminate\Http\Request;
1315
use Illuminate\Support\Facades\Storage;
1416

@@ -131,15 +133,33 @@ public function startTask()
131133
if (!$task || $task['state'] != 0) {
132134
return $this->out(4007);
133135
}
134-
$task->state = 1;
136+
135137
// 3. 更新表字段状态
136-
$task->save();
138+
$task->state = 1;
137139

138140
// 4. 写入事件中处理
139-
$task = $task->toArray();
140-
event(new ApiExcelEvent($task));
141+
$data = $task->toArray();
142+
143+
// 如果是 cli 模式使用 laravels Task 异步事件
144+
if (substr(PHP_SAPI, 0, 3) == 'cli') {
145+
// 触发事件--实例化并通过fire触发,此操作是异步的,触发后立即返回,由Task进程继续处理监听器中的handle逻辑
146+
$event = new ApiExcelSwooleEvent($data);
147+
// $event = new TestEvent('event data');
148+
// $event->delay(10); // 延迟10秒触发
149+
$event->setTries(2); // 出现异常时,累计尝试3次
150+
$success = Event::fire($event);
151+
// var_dump($success);// 判断是否触发成功
152+
} else {
153+
$success = event(new ApiExcelEvent($data));
154+
}
155+
$code = 200;
156+
if (!$success) {
157+
$code = 5000;
158+
$task->state = 0;
159+
}
160+
$task->save();
141161

142-
return $this->out(200, [], '任务加入成功,请稍后下载处理结果');
162+
return $this->out($code, [], '任务加入成功,请稍后下载处理结果');
143163
}
144164

145165
/**
@@ -191,7 +211,7 @@ public function show(ApiExcel $apiExcel)
191211
* Show the form for editing the specified resource.
192212
* 编辑展示数据
193213
*
194-
* @param int $id
214+
* @param int $id
195215
*
196216
* @return \Illuminate\Http\Response
197217
*/
@@ -205,8 +225,8 @@ public function edit($id)
205225
* Update the specified resource in storage.
206226
* 更新数据
207227
*
208-
* @param Update $request
209-
* @param int $id
228+
* @param Update $request
229+
* @param int $id
210230
* @return \Illuminate\Http\Response
211231
*/
212232
public function update(Update $request, $id)
@@ -226,7 +246,7 @@ public function update(Update $request, $id)
226246
/**
227247
* Remove the specified resource from storage.
228248
*
229-
* @param int $id
249+
* @param int $id
230250
* @return \Illuminate\Http\Response
231251
*/
232252
public function destroy($id)
@@ -269,7 +289,7 @@ public function forceDelete($id)
269289

270290
/**
271291
* 下载已完成数据
272-
*
292+
*
273293
* @return \Illuminate\Http\Response
274294
*/
275295
public function downloadLog()

0 commit comments

Comments
 (0)