diff --git a/src/Client.php b/src/Client.php index f081191..3348ea8 100644 --- a/src/Client.php +++ b/src/Client.php @@ -45,6 +45,7 @@ * @method string workers(?string $workerName = null, ?int $start = null, ?int $last = null) * @method string workerJobs(string $worker, ?int $minScoreTime = null) * @method string subscription(string $queue, $op, $topic) + * @method string tags(int $offset = 0, int $count = 10000) * * @property-read JobsCollection $jobs * @property-read WorkersCollection $workers diff --git a/src/Jobs/Collection.php b/src/Jobs/Collection.php index c7e03e0..4826a12 100644 --- a/src/Jobs/Collection.php +++ b/src/Jobs/Collection.php @@ -229,4 +229,15 @@ public function offsetUnset($offset) { throw new UnsupportedFeatureException('Deleting a job is not supported using Jobs collection.'); } + + /** + * @param int $offset + * @param int $count + * + * @return array + */ + public function tagsList(int $offset = 0, int $count = 10000): array + { + return json_decode($this->client->tags($offset, $count), true) ?: []; + } } diff --git a/src/qless-core/qless.lua b/src/qless-core/qless.lua index 89e34b7..3f967f8 100644 --- a/src/qless-core/qless.lua +++ b/src/qless-core/qless.lua @@ -189,6 +189,16 @@ function Qless.subscription(now, queue, command, topic) end end +function Qless.tags(cursor, count) + local tags = redis.call('scan', cursor, 'match', 'ql:t:*', 'count', count) + + for i=1,#tags[2] do + tags[2][i] = tags[2][i]:gsub("ql:t:", "") + end + + return tags[2] +end + function Qless.tag(now, command, ...) assert(command, 'Tag(): Arg "command" must be "add", "remove", "get" or "top"') @@ -2194,6 +2204,10 @@ QlessAPI.track = function(now, command, jid) return cjson.encode(Qless.track(now, command, jid)) end +QlessAPI.tags = function(now, cursor, count) + return cjson.encode(Qless.tags(cursor, count)) +end + QlessAPI.tag = function(now, command, ...) return cjson.encode(Qless.tag(now, command, unpack(arg))) end diff --git a/tests/Jobs/CollectionTest.php b/tests/Jobs/CollectionTest.php index 0206f6e..18224d4 100644 --- a/tests/Jobs/CollectionTest.php +++ b/tests/Jobs/CollectionTest.php @@ -260,6 +260,18 @@ public function testItReturnsAllWorkerJobsByInvalidTimeFilter(): void self::assertCount(3, $jobs); } + public function testTagsList(): void + { + $queue = $this->client->queues['test-queue']; + $queue->put('SampleJobPerformClass', [], 'jid-1', 0, 0, 0, ['tag-a', 'tag-b']); + $queue->put('SampleJobPerformClass', [], 'jid-2', 0, 0, 0, ['tag-a', 'tag-c']); + + $data = $this->client->jobs->tagsList(); + sort($data); + + self::assertEquals(['tag-a', 'tag-b', 'tag-c'], $data); + } + private function put($jid, $opts = []): void {