diff --git a/README.md b/README.md index 4ab26cc..f87c4b8 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,13 @@ use Utopia\Queue; use Utopia\Queue\Message; $connection = new Queue\Connection\Redis('redis'); + +if ($connection->ping()) { + var_dump('Connection is ready.'); +} else { + var_dump('Connection is not ready.'); +} + $adapter = new Queue\Adapter\Swoole($connection, 12, 'my-queue'); $server = new Queue\Server($adapter); diff --git a/src/Queue/Connection.php b/src/Queue/Connection.php index 7049e24..6f37505 100644 --- a/src/Queue/Connection.php +++ b/src/Queue/Connection.php @@ -24,4 +24,5 @@ public function get(string $key): array|string|null; public function setArray(string $key, array $value): bool; public function increment(string $key): int; public function decrement(string $key): int; + public function ping(): bool; } diff --git a/src/Queue/Connection/Redis.php b/src/Queue/Connection/Redis.php index 93792b0..68bc0ae 100644 --- a/src/Queue/Connection/Redis.php +++ b/src/Queue/Connection/Redis.php @@ -159,6 +159,17 @@ public function listRange(string $key, int $total, int $offset): array return array_map(fn (array $job) => new Message($job), $results); } + public function ping(): bool + { + try { + $this->getRedis()->ping(); + + return true; + } catch (\Exception $e) { + return false; + } + } + protected function getRedis(): \Redis { if ($this->redis) { diff --git a/tests/Queue/e2e/AdapterTest.php b/tests/Queue/e2e/AdapterTest.php index 8251fc2..ee09894 100644 --- a/tests/Queue/e2e/AdapterTest.php +++ b/tests/Queue/e2e/AdapterTest.php @@ -62,9 +62,12 @@ public function testEvents(): void { $connection = new Redis('redis', 6379); + $this->assertTrue($connection->ping()); + $client = new Client('workerman', $connection); $client->resetStats(); + foreach ($this->payloads as $payload) { $this->assertTrue($client->enqueue($payload)); }