66
77use Illuminate \Console \Command ;
88use Illuminate \Support \Facades \Redis ;
9+ use YanGusik \BalancedQueue \Support \RedisKeys ;
910
1011class BalancedQueueClearCommand extends Command
1112{
@@ -16,11 +17,11 @@ class BalancedQueueClearCommand extends Command
1617
1718 protected $ description = 'Clear balanced queue jobs ' ;
1819
19- protected string $ prefix ;
20+ protected RedisKeys $ keys ;
2021
2122 public function handle (): int
2223 {
23- $ this ->prefix = config ('balanced-queue.redis.prefix ' , 'balanced-queue ' );
24+ $ this ->keys = new RedisKeys ( config ('balanced-queue.redis.prefix ' , 'balanced-queue ' ) );
2425 $ queueName = $ this ->argument ('queue ' );
2526 $ partition = $ this ->option ('partition ' );
2627 $ force = $ this ->option ('force ' );
@@ -36,10 +37,10 @@ public function handle(): int
3637
3738 protected function clearPartition ($ redis , string $ queueName , string $ partition , bool $ force ): int
3839 {
39- $ queueListKey = $ this ->getPartitionQueueKey ($ queueName , $ partition );
40- $ activeKey = $ this ->getActiveKey ($ queueName , $ partition );
41- $ metricsKey = $ this ->getMetricsKey ($ queueName , $ partition );
42- $ partitionsKey = $ this ->getPartitionsKey ($ queueName );
40+ $ queueListKey = $ this ->keys -> partitionQueue ($ queueName , $ partition );
41+ $ activeKey = $ this ->keys -> active ($ queueName , $ partition );
42+ $ metricsKey = $ this ->keys -> metrics ($ queueName , $ partition );
43+ $ partitionsKey = $ this ->keys -> partitions ($ queueName );
4344
4445 $ pending = (int ) $ redis ->llen ($ queueListKey );
4546 $ active = (int ) $ redis ->hlen ($ activeKey );
@@ -71,7 +72,7 @@ protected function clearPartition($redis, string $queueName, string $partition,
7172
7273 protected function clearAll ($ redis , string $ queueName , bool $ force ): int
7374 {
74- $ partitionsKey = $ this ->getPartitionsKey ($ queueName );
75+ $ partitionsKey = $ this ->keys -> partitions ($ queueName );
7576 $ partitions = $ redis ->smembers ($ partitionsKey );
7677
7778 if (empty ($ partitions )) {
@@ -84,8 +85,8 @@ protected function clearAll($redis, string $queueName, bool $force): int
8485 $ partitionStats = [];
8586
8687 foreach ($ partitions as $ partition ) {
87- $ queueListKey = $ this ->getPartitionQueueKey ($ queueName , $ partition );
88- $ activeKey = $ this ->getActiveKey ($ queueName , $ partition );
88+ $ queueListKey = $ this ->keys -> partitionQueue ($ queueName , $ partition );
89+ $ activeKey = $ this ->keys -> active ($ queueName , $ partition );
8990
9091 $ pending = (int ) $ redis ->llen ($ queueListKey );
9192 $ active = (int ) $ redis ->hlen ($ activeKey );
@@ -116,49 +117,19 @@ protected function clearAll($redis, string $queueName, bool $force): int
116117
117118 // Clear all
118119 foreach ($ partitions as $ partition ) {
119- $ redis ->del ($ this ->getPartitionQueueKey ($ queueName , $ partition ));
120- $ redis ->del ($ this ->getActiveKey ($ queueName , $ partition ));
121- $ redis ->del ($ this ->getDelayedKey ($ queueName , $ partition ));
122- $ redis ->del ($ this ->getMetricsKey ($ queueName , $ partition ));
120+ $ redis ->del ($ this ->keys -> partitionQueue ($ queueName , $ partition ));
121+ $ redis ->del ($ this ->keys -> active ($ queueName , $ partition ));
122+ $ redis ->del ($ this ->keys -> delayed ($ queueName , $ partition ));
123+ $ redis ->del ($ this ->keys -> metrics ($ queueName , $ partition ));
123124 }
124125
125126 $ redis ->del ($ partitionsKey );
126127
127- // Clear round-robin state (uses queueName without 'queues:' prefix)
128- $ rrStateKey = "{$ this ->prefix }:rr-state: {$ queueName }" ;
129- $ redis ->del ($ rrStateKey );
128+ // Clear round-robin state
129+ $ redis ->del ($ this ->keys ->roundRobinState ($ queueName ));
130130
131131 $ this ->info ("✓ Cleared {$ totalPending } pending and {$ totalActive } active jobs from " . count ($ partitions ) . " partitions. " );
132132
133133 return Command::SUCCESS ;
134134 }
135-
136- // =========================================================================
137- // Redis Key Helpers (mirrors BalancedRedisQueue)
138- // =========================================================================
139-
140- protected function getPartitionsKey (string $ queueName ): string
141- {
142- return "{$ this ->prefix }:queues: {$ queueName }:partitions " ;
143- }
144-
145- protected function getPartitionQueueKey (string $ queueName , string $ partition ): string
146- {
147- return "{$ this ->prefix }:queues: {$ queueName }: {$ partition }" ;
148- }
149-
150- protected function getActiveKey (string $ queueName , string $ partition ): string
151- {
152- return "{$ this ->prefix }:queues: {$ queueName }: {$ partition }:active " ;
153- }
154-
155- protected function getMetricsKey (string $ queueName , string $ partition ): string
156- {
157- return "{$ this ->prefix }:metrics: {$ queueName }: {$ partition }" ;
158- }
159-
160- protected function getDelayedKey (string $ queueName , string $ partition ): string
161- {
162- return "{$ this ->prefix }:queues: {$ queueName }: {$ partition }:delayed " ;
163- }
164135}
0 commit comments