Skip to content

Commit 6b4e846

Browse files
committed
getChannels()
1 parent bd99943 commit 6b4e846

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Windwalker\Queue\Driver;
6+
7+
interface ChannelAwareDriverInterface
8+
{
9+
/**
10+
* @return iterable<string>
11+
*/
12+
public function getChannels(): iterable;
13+
}

packages/queue/src/Driver/DatabaseQueueDriver.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @since 3.2
2222
*/
23-
class DatabaseQueueDriver implements QueueDriverInterface
23+
class DatabaseQueueDriver implements QueueDriverInterface, ChannelAwareDriverInterface
2424
{
2525
use UuidDriverTrait;
2626

@@ -288,6 +288,16 @@ public function disconnect(): static
288288
return $this;
289289
}
290290

291+
public function getChannels(): iterable
292+
{
293+
$query = $this->db->createQuery();
294+
295+
$query->selectRaw('DISTINCT channel')
296+
->from($this->table);
297+
298+
return $this->db->prepare($query)->loadColumn();
299+
}
300+
291301
protected function checkCanSkipLocked(): bool
292302
{
293303
if ($this->canSkipLocked !== null) {

packages/queue/src/Driver/PdoQueueDriver.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @since 3.3
1919
*/
20-
class PdoQueueDriver implements QueueDriverInterface
20+
class PdoQueueDriver implements QueueDriverInterface, ChannelAwareDriverInterface
2121
{
2222
use UuidDriverTrait;
2323

@@ -217,6 +217,16 @@ public function defer(QueueMessage $message): static
217217
return $this;
218218
}
219219

220+
public function getChannels(): iterable
221+
{
222+
$sql = 'SELECT DISTINCT channel FROM ' . $this->table;
223+
224+
$stat = $this->pdo->prepare($sql);
225+
$stat->execute();
226+
227+
return $stat->fetchAll(PDO::FETCH_COLUMN);
228+
}
229+
220230
/**
221231
* Method to get property Table
222232
*

packages/queue/test/QueueDatabaseTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,28 @@ public function testGetDriver(): void
131131
self::markTestIncomplete(); // TODO: Complete this test
132132
}
133133

134+
/**
135+
* @see ChannelAwareDriverInterface::getChannels
136+
*/
137+
public function testGetChannels(): void
138+
{
139+
$this->instance->push(new TestJob(['Foo']), 0, 'foo-channel');
140+
$this->instance->push(new TestJob(['Bar']), 0, 'bar-channel');
141+
142+
$channels = [...$this->instance->getDriver()->getChannels()];
143+
144+
sort($channels);
145+
146+
self::assertEquals(
147+
['bar-channel', 'default', 'foo-channel'],
148+
$channels
149+
);
150+
151+
self::$db->delete('queue_jobs')
152+
->whereIn('channel', ['foo-channel', 'bar-channel'])
153+
->execute();
154+
}
155+
134156
/**
135157
* @see Queue::__construct
136158
*/

0 commit comments

Comments
 (0)