Skip to content

Commit 5a9ab5e

Browse files
committed
FEATURE: Extend the QueueInterface with countReserved and countFailed
Also add countReady() and deprecate the count() command to clearify the interface
1 parent 279ee75 commit 5a9ab5e

5 files changed

Lines changed: 114 additions & 16 deletions

File tree

Classes/Command/QueueCommandController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
* source code.
1212
*/
1313

14+
use Flowpack\JobQueue\Common\Exception;
1415
use Flowpack\JobQueue\Common\Queue\QueueManager;
1516
use Neos\Flow\Annotations as Flow;
1617
use Neos\Flow\Cli\CommandController;
18+
use Neos\Flow\Mvc\Exception\StopActionException;
1719
use Neos\Utility\TypeHandling;
1820

1921
/**
@@ -40,14 +42,15 @@ class QueueCommandController extends CommandController
4042
* Displays all configured queues, their type and the number of messages that are ready to be processed.
4143
*
4244
* @return void
45+
* @throws Exception
4346
*/
4447
public function listCommand()
4548
{
4649
$rows = [];
4750
foreach ($this->queueConfigurations as $queueName => $queueConfiguration) {
4851
$queue = $this->queueManager->getQueue($queueName);
4952
try {
50-
$numberOfMessages = $queue->count();
53+
$numberOfMessages = $queue->countReady();
5154
} catch (\Exception $e) {
5255
$numberOfMessages = '-';
5356
}
@@ -63,6 +66,7 @@ public function listCommand()
6366
*
6467
* @param string $queue Name of the queue to describe (e.g. "some-queue")
6568
* @return void
69+
* @throws Exception
6670
*/
6771
public function describeCommand($queue)
6872
{
@@ -83,6 +87,8 @@ public function describeCommand($queue)
8387
*
8488
* @param string $queue Name of the queue to initialize (e.g. "some-queue")
8589
* @return void
90+
* @throws Exception
91+
* @throws StopActionException
8692
*/
8793
public function setupCommand($queue)
8894
{
@@ -106,6 +112,8 @@ public function setupCommand($queue)
106112
* @param string $queue Name of the queue to flush (e.g. "some-queue")
107113
* @param bool $force This flag is required in order to avoid accidental flushes
108114
* @return void
115+
* @throws Exception
116+
* @throws StopActionException
109117
*/
110118
public function flushCommand($queue, $force = false)
111119
{
@@ -134,6 +142,7 @@ public function flushCommand($queue, $force = false)
134142
* @param string $payload Arbitrary payload, for example a serialized instance of a class implementing JobInterface
135143
* @param string $options JSON encoded, for example '{"some-option": "some-value"}'
136144
* @return void
145+
* @throws Exception
137146
*/
138147
public function submitCommand($queue, $payload, $options = null)
139148
{

Classes/Queue/FakeQueue.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,31 @@ public function peek($limit = 1)
146146
/**
147147
* @inheritdoc
148148
*/
149-
public function count()
149+
public function count():int
150+
{
151+
return $this->countReady();
152+
}
153+
154+
/**
155+
* @inheritdoc
156+
*/
157+
public function countReady(): int
158+
{
159+
return 0;
160+
}
161+
162+
/**
163+
* @inheritdoc
164+
*/
165+
public function countReserved(): int
166+
{
167+
return 0;
168+
}
169+
170+
/**
171+
* @inheritdoc
172+
*/
173+
public function countFailed(): int
150174
{
151175
return 0;
152176
}

Classes/Queue/QueueInterface.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,33 @@ public function finish($messageId);
102102
public function peek($limit = 1);
103103

104104
/**
105-
* Count ready messages in the queue
105+
* Get a count of ready messages currently in the queue.
106106
*
107-
* Get a count of messages currently in the queue.
107+
* @return int The number of ready messages in the queue
108+
* @deprecated Will be removed with the next major. Use countReady instead.
109+
*/
110+
public function count(): int;
111+
112+
/**
113+
* Get a count of ready messages currently in the queue.
114+
*
115+
* @return int The number of ready messages in the queue
116+
*/
117+
public function countReady(): int;
118+
119+
/**
120+
* Get a count of reserved messages currently in the queue.
121+
*
122+
* @return int The number of reserved messages in the queue
123+
*/
124+
public function countReserved(): int;
125+
126+
/**
127+
* Get a count of failed messages currently in the queue.
108128
*
109-
* @return integer The number of messages in the queue
129+
* @return int The number of failed messages in the queue
110130
*/
111-
public function count();
131+
public function countFailed(): int;
112132

113133
/**
114134
* Removes all messages from this queue
@@ -119,4 +139,4 @@ public function count();
119139
* @return void
120140
*/
121141
public function flush();
122-
}
142+
}

Tests/Functional/AbstractQueueTest.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ public function releasePutsMessageBackToQueue()
144144
$messageId = $this->queue->submit('A message');
145145

146146
$this->queue->waitAndReserve(1);
147-
$this->assertSame(0, $this->queue->count());
147+
$this->assertSame(0, $this->queue->countReady());
148148

149149
$this->queue->release($messageId);
150-
$this->assertSame(1, $this->queue->count());
150+
$this->assertSame(1, $this->queue->countReady());
151151
}
152152

153153
/**
@@ -179,16 +179,32 @@ public function abortRemovesMessageFromActiveQueue()
179179
$this->queue->waitAndReserve(1);
180180

181181
$this->queue->abort($messageId);
182-
$this->assertSame(0, $this->queue->count());
182+
$this->assertSame(0, $this->queue->countReady());
183183
$this->assertNull($this->queue->waitAndTake(1));
184184
}
185185

186186
/**
187187
* @test
188188
*/
189-
public function countReturnsZeroByDefault()
189+
public function countReadyReturnsZeroByDefault()
190190
{
191-
$this->assertSame(0, $this->queue->count());
191+
$this->assertSame(0, $this->queue->countReady());
192+
}
193+
194+
/**
195+
* @test
196+
*/
197+
public function countFailedReturnsZeroByDefault()
198+
{
199+
$this->assertSame(0, $this->queue->countFailed());
200+
}
201+
202+
/**
203+
* @test
204+
*/
205+
public function countReservedReturnsZeroByDefault()
206+
{
207+
$this->assertSame(0, $this->queue->countReserved());
192208
}
193209

194210
/**
@@ -199,6 +215,6 @@ public function countReturnsNumberOfReadyJobs()
199215
$this->queue->submit('First message');
200216
$this->queue->submit('Second message');
201217

202-
$this->assertSame(2, $this->queue->count());
218+
$this->assertSame(2, $this->queue->countReady());
203219
}
204-
}
220+
}

Tests/Unit/Fixtures/TestQueue.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ class TestQueue implements QueueInterface
3030
/**
3131
* @var string[]
3232
*/
33-
protected $processingMessages = [];
33+
protected $reservedMessages = [];
3434

3535
/**
3636
* @var string[]
3737
*/
3838
protected $failedMessages = [];
3939

40+
/**
41+
* @var string[]
42+
*/
43+
protected $processingMessages = [];
44+
4045
/**
4146
* @var int[]
4247
*/
@@ -205,11 +210,35 @@ public function peek($limit = 1)
205210
/**
206211
* @inheritdoc
207212
*/
208-
public function count()
213+
public function count(): int
214+
{
215+
return $this->countReady();
216+
}
217+
218+
/**
219+
* @inheritdoc
220+
*/
221+
public function countReady():int
209222
{
210223
return count($this->readyMessages);
211224
}
212225

226+
/**
227+
* @inheritdoc
228+
*/
229+
public function countReserved(): int
230+
{
231+
return count($this->reservedMessages);
232+
}
233+
234+
/**
235+
* @inheritdoc
236+
*/
237+
public function countFailed(): int
238+
{
239+
return count($this->failedMessages);
240+
}
241+
213242
/**
214243
* @inheritdoc
215244
*/

0 commit comments

Comments
 (0)