Skip to content

Commit acb68e9

Browse files
Merge pull request #29 from daniellienert/feature/extend-interface
!!! FEATURE: Extend the QueueInterface with countReserved and countFailed
2 parents db672b1 + bdffb4a commit acb68e9

6 files changed

Lines changed: 125 additions & 18 deletions

File tree

Classes/Command/JobCommandController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Neos\Cache\Frontend\VariableFrontend;
1919
use Neos\Flow\Annotations as Flow;
2020
use Neos\Flow\Cli\CommandController;
21+
use Neos\Flow\Mvc\Exception\StopActionException;
2122

2223
/**
2324
* Job command controller
@@ -62,6 +63,7 @@ class JobCommandController extends CommandController
6263
* @param int $limit If set, only the given amount of jobs are processed (successful or not) before the script exits
6364
* @param bool $verbose Output debugging information
6465
* @return void
66+
* @throws StopActionException
6567
*/
6668
public function workCommand($queue, $exitAfter = null, $limit = null, $verbose = false)
6769
{
@@ -123,11 +125,12 @@ public function workCommand($queue, $exitAfter = null, $limit = null, $verbose =
123125
* @param string $queue The name of the queue
124126
* @param integer $limit Number of jobs to list (some queues only support a limit of 1)
125127
* @return void
128+
* @throws JobQueueException
126129
*/
127130
public function listCommand($queue, $limit = 1)
128131
{
129132
$jobs = $this->jobManager->peek($queue, $limit);
130-
$totalCount = $this->queueManager->getQueue($queue)->count();
133+
$totalCount = $this->queueManager->getQueue($queue)->countReady();
131134
foreach ($jobs as $job) {
132135
$this->outputLine('<b>%s</b>', [$job->getLabel()]);
133136
}

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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,23 @@ public function peek($limit = 1)
146146
/**
147147
* @inheritdoc
148148
*/
149-
public function count()
149+
public function countReady(): int
150+
{
151+
return 0;
152+
}
153+
154+
/**
155+
* @inheritdoc
156+
*/
157+
public function countReserved(): int
158+
{
159+
return 0;
160+
}
161+
162+
/**
163+
* @inheritdoc
164+
*/
165+
public function countFailed(): int
150166
{
151167
return 0;
152168
}

Classes/Queue/QueueInterface.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,25 @@ 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+
*/
109+
public function countReady(): int;
110+
111+
/**
112+
* Get a count of reserved messages currently in the queue.
113+
*
114+
* @return int The number of reserved messages in the queue
115+
*/
116+
public function countReserved(): int;
117+
118+
/**
119+
* Get a count of failed messages currently in the queue.
108120
*
109-
* @return integer The number of messages in the queue
121+
* @return int The number of failed messages in the queue
110122
*/
111-
public function count();
123+
public function countFailed(): int;
112124

113125
/**
114126
* Removes all messages from this queue
@@ -119,4 +131,4 @@ public function count();
119131
* @return void
120132
*/
121133
public function flush();
122-
}
134+
}

Tests/Functional/AbstractQueueTest.php

Lines changed: 54 additions & 8 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
/**
@@ -167,6 +167,8 @@ public function releaseIncreasesNumberOfReleases()
167167
$this->queue->release($messageId);
168168
$message = $this->queue->waitAndReserve(1);
169169
$this->assertSame(2, $message->getNumberOfReleases());
170+
171+
$this->queue->abort($messageId);
170172
}
171173

172174
/**
@@ -179,26 +181,70 @@ public function abortRemovesMessageFromActiveQueue()
179181
$this->queue->waitAndReserve(1);
180182

181183
$this->queue->abort($messageId);
182-
$this->assertSame(0, $this->queue->count());
184+
$this->assertSame(0, $this->queue->countReady());
183185
$this->assertNull($this->queue->waitAndTake(1));
184186
}
185187

186188
/**
187189
* @test
188190
*/
189-
public function countReturnsZeroByDefault()
191+
public function countReadyReturnsZeroByDefault()
190192
{
191-
$this->assertSame(0, $this->queue->count());
193+
$this->assertSame(0, $this->queue->countReady());
192194
}
193195

194196
/**
195197
* @test
196198
*/
197-
public function countReturnsNumberOfReadyJobs()
199+
public function countReadyReturnsNumberOfReadyJobs()
198200
{
199201
$this->queue->submit('First message');
200202
$this->queue->submit('Second message');
201203

202-
$this->assertSame(2, $this->queue->count());
204+
$this->assertSame(2, $this->queue->countReady());
205+
}
206+
207+
/**
208+
* @test
209+
*/
210+
public function countFailedReturnsZeroByDefault()
211+
{
212+
$this->assertSame(0, $this->queue->countFailed());
213+
}
214+
215+
/**
216+
* @test
217+
*/
218+
public function countFailedReturnsNumberOfFailedMessages()
219+
{
220+
$messageId = $this->queue->submit('A message');
221+
222+
$this->queue->waitAndReserve(1);
223+
$this->assertSame(0, $this->queue->countFailed());
224+
225+
$this->queue->abort($messageId);
226+
$this->assertSame(1, $this->queue->countFailed());
227+
}
228+
229+
/**
230+
* @test
231+
*/
232+
public function countReservedReturnsZeroByDefault()
233+
{
234+
$this->assertSame(0, $this->queue->countReserved());
235+
}
236+
237+
/**
238+
* @test
239+
*/
240+
public function countReservedReturnsNumberOfReservedMessages()
241+
{
242+
$messageId = $this->queue->submit('A message');
243+
244+
$this->queue->waitAndReserve(1);
245+
$this->assertSame(1, $this->queue->countReserved());
246+
247+
$this->queue->abort($messageId);
248+
$this->assertSame(0, $this->queue->countReserved());
203249
}
204-
}
250+
}

Tests/Unit/Fixtures/TestQueue.php

Lines changed: 23 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,27 @@ public function peek($limit = 1)
205210
/**
206211
* @inheritdoc
207212
*/
208-
public function count()
213+
public function countReady():int
209214
{
210215
return count($this->readyMessages);
211216
}
212217

218+
/**
219+
* @inheritdoc
220+
*/
221+
public function countReserved(): int
222+
{
223+
return count($this->reservedMessages);
224+
}
225+
226+
/**
227+
* @inheritdoc
228+
*/
229+
public function countFailed(): int
230+
{
231+
return count($this->failedMessages);
232+
}
233+
213234
/**
214235
* @inheritdoc
215236
*/

0 commit comments

Comments
 (0)