From 948957fdd495f9dc33a3d3ccf8fdd7c83d66eb43 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 19 Oct 2022 14:47:06 +0200 Subject: [PATCH 1/3] feat: remove redis swoole connection --- README.md | 4 +- src/Queue/Connection/RedisSwoole.php | 174 -------------------------- tests/Queue/servers/Swoole/worker.php | 2 +- 3 files changed, 3 insertions(+), 177 deletions(-) delete mode 100644 src/Queue/Connection/RedisSwoole.php diff --git a/README.md b/README.md index db010fd..9a8355f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ require_once __DIR__ . '/../../vendor/autoload.php'; use Utopia\Queue; use Utopia\Queue\Message; -$connection = new Queue\Connection\RedisSwoole('redis'); +$connection = new Queue\Connection\Redis('redis'); $adapter = new Queue\Adapter\Swoole($connection, 12, 'swoole'); $server = new Queue\Server($adapter); @@ -50,7 +50,7 @@ $server // Enqueue messages to the worker using swoole adapter -$connection = new RedisSwoole('redis', 6379); +$connection = new Redis('redis', 6379); run(function () use ($connection) { $client = new Client('swoole', $connection); go(function () use ($client) { diff --git a/src/Queue/Connection/RedisSwoole.php b/src/Queue/Connection/RedisSwoole.php deleted file mode 100644 index 7fe035f..0000000 --- a/src/Queue/Connection/RedisSwoole.php +++ /dev/null @@ -1,174 +0,0 @@ -host = $host; - $this->port = $port; - $this->user = $user; - $this->password = $password; - } - - public function rightPopLeftPushArray(string $queue, string $destination, int $timeout): array|false - { - $response = $this->rightPopLeftPush($queue, $destination, $timeout); - - if (!$response) { - return false; - } - - return json_decode($response, true); - } - public function rightPopLeftPush(string $queue, string $destination, int $timeout): string|false - { - $response = $this->getRedis()->bRPopLPush($queue, $destination, $timeout); - - if (($response ?? false) === false) { - return false; - } - - return $response; - } - public function rightPushArray(string $queue, array $value): bool - { - return !!$this->getRedis()->rPush($queue, json_encode($value)); - } - - public function rightPush(string $queue, string $value): bool - { - return !!$this->getRedis()->rPush($queue, $value); - } - - public function leftPushArray(string $queue, array $value): bool - { - return !!$this->getRedis()->lPush($queue, json_encode($value)); - } - - public function leftPush(string $queue, string $value): bool - { - return !!$this->getRedis()->lPush($queue, $value); - } - - public function rightPopArray(string $queue, int $timeout): array|false - { - $response = $this->rightPop($queue, $timeout); - - if ($response === false) { - return false; - } - - return json_decode($response, true); - } - - public function rightPop(string $queue, int $timeout): string|false - { - $response = $this->getRedis()->brPop([$queue], $timeout); - - if (($response ?? false) === false) { - return false; - } - return $response[1]; - } - - public function leftPopArray(string $queue, int $timeout): array|false - { - $response = $this->getRedis()->blPop($queue, $timeout); - - if ($response === false) { - return false; - } - - return json_decode($response, true); - } - - public function leftPop(string $queue, int $timeout): string|false - { - $response = $this->getRedis()->blPop($queue, $timeout); - - if (($response ?? false) === false) { - return false; - } - - return $response[1]; - } - - public function listRemove(string $queue, string $key): bool - { - return !!$this->getRedis()->lRem($queue, $key, 1); - } - - public function remove(string $key): bool - { - return !!$this->getRedis()->del($key); - } - - public function move(string $queue, string $destination): bool - { - return $this->getRedis()->move($queue, $destination); - } - - public function setArray(string $key, array $value): bool - { - return $this->set($key, json_encode($value)); - } - - public function set(string $key, string $value): bool - { - return $this->getRedis()->set($key, $value); - } - - public function get(string $key): array|string|null - { - return $this->getRedis()->get($key); - } - - public function listSize(string $key): int - { - return $this->getRedis()->lSize($key); - } - - public function increment(string $key): int - { - return $this->getRedis()->incr($key); - } - - public function decrement(string $key): int - { - return $this->getRedis()->decr($key); - } - - public function listRange(string $key, int $total, int $offset): array - { - $start = $offset - 1; - $end = ($total + $offset) - 1; - $results = $this->getRedis()->lrange($key, $start, $end); - - return array_map(fn (array $job) => new Message($job), $results); - } - - protected function getRedis(): Redis - { - if ($this->redis) { - return $this->redis; - } - - $this->redis = new Redis(); - - $this->redis->connect($this->host, $this->port); - - return $this->getRedis(); - } -} diff --git a/tests/Queue/servers/Swoole/worker.php b/tests/Queue/servers/Swoole/worker.php index bb58173..39bc33c 100644 --- a/tests/Queue/servers/Swoole/worker.php +++ b/tests/Queue/servers/Swoole/worker.php @@ -6,7 +6,7 @@ use Utopia\Queue; use Utopia\Queue\Message; -$connection = new Queue\Connection\RedisSwoole('redis'); +$connection = new Queue\Connection\Redis('redis'); $adapter = new Queue\Adapter\Swoole($connection, 12, 'swoole'); $server = new Queue\Server($adapter); From ff4b9e29852eab6651cc7d90b802a7ccedfc40cc Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 19 Oct 2022 14:48:03 +0200 Subject: [PATCH 2/3] tests: fix tests --- tests/Queue/e2e/AdapterTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Queue/e2e/AdapterTest.php b/tests/Queue/e2e/AdapterTest.php index fbef00e..8251fc2 100644 --- a/tests/Queue/e2e/AdapterTest.php +++ b/tests/Queue/e2e/AdapterTest.php @@ -5,7 +5,6 @@ use PHPUnit\Framework\TestCase; use Utopia\Queue\Client; use Utopia\Queue\Connection\Redis; -use Utopia\Queue\Connection\RedisSwoole; use function Swoole\Coroutine\go; use function Swoole\Coroutine\run; @@ -81,7 +80,7 @@ public function testEvents(): void public function testSwoole(): void { - $connection = new RedisSwoole('redis', 6379); + $connection = new Redis('redis', 6379); run(function () use ($connection) { $client = new Client('swoole', $connection); From ab23b7dd73d88f892e6c7202d887ffe1974601c5 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 19 Oct 2022 15:15:25 +0200 Subject: [PATCH 3/3] fix: readme --- README.md | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 9a8355f..f9f2254 100644 --- a/README.md +++ b/README.md @@ -11,17 +11,19 @@ Although this library is part of the [Utopia Framework](https://github.com/utopi ## Getting Started Install using composer: + ```bash composer require utopia-php/queue ``` Init in your application: + ```php start(); -// Enqueue messages to the worker using swoole adapter +// Enqueue messages to the worker using the Redis adapter $connection = new Redis('redis', 6379); -run(function () use ($connection) { - $client = new Client('swoole', $connection); - go(function () use ($client) { - $client->resetStats(); - - $client->enqueue([ - 'type' => 'test_number', - 'value' => 123 - ]); - }); -}); +$client = new Client('swoole', $connection); +$client->resetStats(); + +$client->enqueue([ + 'type' => 'test_number', + 'value' => 123 +]); ``` ## System Requirements