Skip to content

Commit ee88486

Browse files
authored
Merge pull request #62 from cakephp/fix-60
Fix queue parameter being required on non-default queues
2 parents afbeaaa + b4cd2b3 commit ee88486

File tree

2 files changed

+70
-37
lines changed

2 files changed

+70
-37
lines changed

src/Command/WorkerCommand.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public function getOptionParser(): ConsoleOptionParser
4848
'short' => 'c',
4949
]);
5050
$parser->addOption('queue', [
51-
'default' => 'default',
52-
'help' => 'Name of queue to bind to',
51+
'help' => 'Name of queue to bind to. Defaults to the queue config (--config).',
5352
'short' => 'Q',
5453
]);
5554
$parser->addOption('processor', [
@@ -140,10 +139,14 @@ public function execute(Arguments $args, ConsoleIo $io)
140139
$processor->getEventManager()->on($listener);
141140
$extension->getEventManager()->on($listener);
142141
}
143-
$url = Configure::read(sprintf('Queue.%s.url', $config));
142+
$url = Configure::read("Queue.{$config}.url");
144143
$client = new SimpleClient($url, $logger);
145-
/** @psalm-suppress InvalidArgument */
146-
$client->bindTopic((string)$args->getOption('queue'), $processor, $args->getOption('processor'));
144+
$queue = $args->getOption('queue')
145+
? (string)$args->getOption('queue')
146+
: Configure::read("Queue.{$config}.queue", 'default');
147+
$processorName = $args->getOption('processor') ? (string)$args->getOption('processor') : null;
148+
149+
$client->bindTopic($queue, $processor, $processorName);
147150
$client->consume($extension);
148151
}
149152
}

tests/TestCase/Command/WorkerCommandTest.php

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ public function testQueueProcessesWithLogger()
146146
]);
147147

148148
$this->exec('worker --max-runtime=1 --logger=debug --verbose');
149-
$log = Log::engine('debug');
150-
$this->assertIsArray($log->read());
151-
$this->assertNotEmpty($log->read());
152-
$this->assertEquals($log->read()[0], 'debug Max Iterations: 0');
149+
$this->assertDebugLogContains('debug Max Iterations: 0');
153150
}
154151

155152
/**
@@ -159,36 +156,30 @@ public function testQueueProcessesWithLogger()
159156
*/
160157
public function testQueueProcessesJob()
161158
{
159+
$config = [
160+
'queue' => 'default',
161+
'url' => 'file:///' . TMP . DS . 'queue',
162+
];
162163
Configure::write([
163-
'Queue' => [
164-
'default' => [
165-
'queue' => 'default',
166-
'url' => 'file:///' . TMP . DS . 'queue',
167-
],
168-
],
164+
'Queue' => ['default' => $config],
169165
]);
170166

171167
Log::setConfig('debug', [
172168
'className' => 'Array',
173169
'levels' => ['notice', 'info', 'debug'],
174170
]);
175171

176-
$this->exec('worker --max-runtime=3 --logger=debug --verbose');
177-
178172
$callable = [WelcomeMailer::class, 'welcome'];
179173
$arguments = [];
180174
$options = ['config' => 'default'];
181175

176+
QueueManager::setConfig('default', $config);
182177
QueueManager::push($callable, $arguments, $options);
178+
QueueManager::drop('default');
183179

184-
$log = Log::engine('debug');
185-
$this->assertIsArray($log->read());
186-
$this->assertNotEmpty($log->read());
187-
foreach ($log->read() as $line) {
188-
if (stripos($line, 'Welcome mail sent') !== false) {
189-
$this->assertTrue(true);
190-
}
191-
}
180+
$this->exec('worker --max-runtime=3 --logger=debug --verbose');
181+
182+
$this->assertDebugLogContains('Welcome mail sent');
192183
}
193184

194185
/**
@@ -198,35 +189,74 @@ public function testQueueProcessesJob()
198189
*/
199190
public function testQueueProcessesJobWithProcessor()
200191
{
192+
$config = [
193+
'queue' => 'default',
194+
'url' => 'file:///' . TMP . DS . 'queue',
195+
];
201196
Configure::write([
202-
'Queue' => [
203-
'default' => [
204-
'queue' => 'default',
205-
'url' => 'file:///' . TMP . DS . 'queue',
206-
],
207-
],
197+
'Queue' => ['default' => $config],
208198
]);
209-
210199
Log::setConfig('debug', [
211200
'className' => 'Array',
212201
'levels' => ['notice', 'info', 'debug'],
213202
]);
214203

204+
$callable = [WelcomeMailer::class, 'welcome'];
205+
$arguments = [];
206+
$options = ['config' => 'default'];
207+
208+
QueueManager::setConfig('default', $config);
209+
QueueManager::push($callable, $arguments, $options);
210+
QueueManager::drop('default');
211+
215212
$this->exec('worker --max-runtime=3 --processor=processor-name --logger=debug --verbose');
216213

214+
$this->assertDebugLogContains('Welcome mail sent');
215+
}
216+
217+
/**
218+
* Test non-default queue name
219+
*
220+
* @runInSeparateProcess
221+
*/
222+
public function testQueueProcessesJobWithOtherQueue()
223+
{
224+
$config = [
225+
'queue' => 'other',
226+
'url' => 'file:///' . TMP . DS . 'queue',
227+
];
228+
Configure::write([
229+
'Queue' => ['other' => $config],
230+
]);
231+
232+
Log::setConfig('debug', [
233+
'className' => 'Array',
234+
'levels' => ['notice', 'info', 'debug'],
235+
]);
236+
217237
$callable = [WelcomeMailer::class, 'welcome'];
218238
$arguments = [];
219-
$options = ['config' => 'default'];
239+
$options = ['config' => 'other'];
220240

241+
QueueManager::setConfig('other', $config);
221242
QueueManager::push($callable, $arguments, $options);
243+
QueueManager::drop('other');
244+
245+
$this->exec('worker --config=other --max-runtime=3 --processor=processor-name --logger=debug --verbose');
222246

247+
$this->assertDebugLogContains('Welcome mail sent');
248+
}
249+
250+
protected function assertDebugLogContains($expected): void
251+
{
223252
$log = Log::engine('debug');
224-
$this->assertIsArray($log->read());
225-
$this->assertNotEmpty($log->read());
253+
$found = false;
226254
foreach ($log->read() as $line) {
227-
if (stripos($line, 'Welcome mail sent') !== false) {
228-
$this->assertTrue(true);
255+
if (strpos($line, $expected) !== false) {
256+
$found = true;
257+
break;
229258
}
230259
}
260+
$this->assertTrue($found, "Did not find `{$expected}` in logs.");
231261
}
232262
}

0 commit comments

Comments
 (0)