Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 1b420e2

Browse files
committed
improve isSwooleQueuePacket function
1 parent 69282bd commit 1b420e2

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace SwooleTW\Http\Concerns;
4+
5+
use Swoole\Table;
6+
use SwooleTW\Http\Table\SwooleTable;
7+
8+
trait InteractsWithSwooleQueue
9+
{
10+
/**
11+
* Indicates if a packet is swoole's queue job.
12+
*
13+
* @param mixed
14+
*/
15+
protected function isSwooleQueuePacket($packet)
16+
{
17+
if (! is_string($packet)) {
18+
return false;
19+
}
20+
21+
$decoded = json_decode($packet, true);
22+
23+
return JSON_ERROR_NONE === json_last_error() && isset($decoded['job']);
24+
}
25+
}

src/Server/Manager.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
use Illuminate\Contracts\Container\Container;
1515
use Illuminate\Contracts\Debug\ExceptionHandler;
1616
use SwooleTW\Http\Concerns\InteractsWithWebsocket;
17+
use SwooleTW\Http\Concerns\InteractsWithSwooleQueue;
1718
use SwooleTW\Http\Concerns\InteractsWithSwooleTable;
1819

1920
class Manager
2021
{
2122
use InteractsWithWebsocket,
2223
InteractsWithSwooleTable,
24+
InteractsWithSwooleQueue,
2325
WithApplication;
2426

2527
/**
@@ -233,12 +235,8 @@ public function onTask($server, $taskId, $srcWorkerId, $data)
233235
if ($this->isWebsocketPushPacket($data)) {
234236
$this->pushMessage($server, $data['data'] ?? []);
235237
// push async task to queue
236-
} elseif (is_string($data)) {
237-
$decoded = json_decode($data, true);
238-
239-
if (JSON_ERROR_NONE === json_last_error() && isset($decoded['job'])) {
240-
(new SwooleTaskJob($this->container, $server, $data, $taskId, $srcWorkerId))->fire();
241-
}
238+
} elseif ($this->isSwooleQueuePacket($data)) {
239+
(new SwooleTaskJob($this->container, $server, $data, $taskId, $srcWorkerId))->fire();
242240
}
243241
} catch (Throwable $e) {
244242
$this->logServerError($e);

0 commit comments

Comments
 (0)