Skip to content

Commit 75db394

Browse files
authored
Merge pull request #120 from sheldonreiff/use-unique-name-for-cache-configurations
Use unique names for queue unique cache configurations
2 parents 297d528 + e87fe35 commit 75db394

File tree

5 files changed

+95
-24
lines changed

5 files changed

+95
-24
lines changed

src/Command/WorkerCommand.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,12 @@ protected function getQueueExtension(Arguments $args, LoggerInterface $logger):
130130

131131
$limitAttempsExtension->getEventManager()->on(new FailedJobsListener());
132132

133+
$configKey = (string)$args->getOption('config');
134+
$config = QueueManager::getConfig($configKey);
135+
133136
$extensions = [
134137
new LoggerExtension($logger),
135138
$limitAttempsExtension,
136-
new RemoveUniqueJobIdFromCacheExtension('Cake/Queue.queueUnique'),
137139
];
138140

139141
if (!is_null($args->getOption('max-jobs'))) {
@@ -146,6 +148,10 @@ protected function getQueueExtension(Arguments $args, LoggerInterface $logger):
146148
$extensions[] = new LimitConsumptionTimeExtension($endTime);
147149
}
148150

151+
if (isset($config['uniqueCacheKey'])) {
152+
$extensions[] = new RemoveUniqueJobIdFromCacheExtension($config['uniqueCacheKey']);
153+
}
154+
149155
return new ChainExtension($extensions);
150156
}
151157

@@ -172,16 +178,16 @@ protected function getLogger(Arguments $args): LoggerInterface
172178
*/
173179
public function execute(Arguments $args, ConsoleIo $io)
174180
{
175-
$logger = $this->getLogger($args);
176-
$processor = new Processor($logger, $this->container);
177-
$extension = $this->getQueueExtension($args, $logger);
178-
179181
$config = (string)$args->getOption('config');
180182
if (!Configure::check(sprintf('Queue.%s', $config))) {
181183
$io->error(sprintf('Configuration key "%s" was not found', $config));
182184
$this->abort();
183185
}
184186

187+
$logger = $this->getLogger($args);
188+
$processor = new Processor($logger, $this->container);
189+
$extension = $this->getQueueExtension($args, $logger);
190+
185191
$hasListener = Configure::check(sprintf('Queue.%s.listener', $config));
186192
if ($hasListener) {
187193
$listenerClassName = Configure::read(sprintf('Queue.%s.listener', $config));

src/QueueManager.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public static function setConfig($key, $config = null): void
8989
}
9090

9191
return;
92+
} elseif (is_array($key)) {
93+
throw new LogicException('If config is not null, key must be a string.');
9294
}
9395

9496
if (isset(static::$_config[$key])) {
@@ -127,7 +129,9 @@ public static function setConfig($key, $config = null): void
127129

128130
$cacheConfig = array_merge($cacheDefaults, $config['uniqueCache']);
129131

130-
Cache::setConfig('Cake/Queue.queueUnique', $cacheConfig);
132+
$config['uniqueCacheKey'] = "Cake/Queue.queueUnique.{$key}";
133+
134+
Cache::setConfig($config['uniqueCacheKey'], $cacheConfig);
131135
}
132136

133137
/** @psalm-suppress InvalidPropertyAssignmentValue */
@@ -228,15 +232,15 @@ public static function push($className, array $data = [], array $options = []):
228232

229233
/** @psalm-suppress InvalidPropertyFetch */
230234
if (!empty($class::$shouldBeUnique)) {
231-
if (!Cache::getConfig('Cake/Queue.queueUnique')) {
235+
if (empty($config['uniqueCache'])) {
232236
throw new InvalidArgumentException(
233237
"$class::\$shouldBeUnique is set to `true` but `uniqueCache` configuration is missing."
234238
);
235239
}
236240

237241
$uniqueId = static::getUniqueId($class, $method, $data);
238242

239-
if (Cache::read($uniqueId, 'Cake/Queue.queueUnique')) {
243+
if (Cache::read($uniqueId, $config['uniqueCacheKey'])) {
240244
if ($logger) {
241245
$logger->debug(
242246
"An identical instance of $class already exists on the queue. This push will be ignored."
@@ -279,7 +283,7 @@ public static function push($className, array $data = [], array $options = []):
279283
if (!empty($class::$shouldBeUnique)) {
280284
$uniqueId = static::getUniqueId($class, $method, $data);
281285

282-
Cache::add($uniqueId, true, 'Cake/Queue.queueUnique');
286+
Cache::add($uniqueId, true, $config['uniqueCacheKey']);
283287
}
284288
}
285289

tests/TestCase/Command/WorkerCommandTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,31 @@ public function testQueueProcessesJobWithDIService()
299299

300300
$this->assertDebugLogContains('Debug job was run with service infotext');
301301
}
302+
303+
/**
304+
* Test that queue will process when a unique cache is configured.
305+
*
306+
* @runInSeparateProcess
307+
*/
308+
public function testQueueProcessesWithUniqueCacheConfigured()
309+
{
310+
$config = [
311+
'queue' => 'default',
312+
'url' => 'file:///' . TMP . DS . 'queue',
313+
'receiveTimeout' => 100,
314+
'uniqueCache' => [
315+
'engine' => 'File',
316+
],
317+
];
318+
Configure::write('Queue', ['default' => $config]);
319+
320+
Log::setConfig('debug', [
321+
'className' => 'Array',
322+
'levels' => ['notice', 'info', 'debug'],
323+
]);
324+
325+
$this->exec('queue worker --max-jobs=1 --logger=debug --verbose');
326+
327+
$this->assertDebugLogContains('Consumption has started');
328+
}
302329
}

tests/TestCase/Consumption/RemoveUniqueJobIdFromCacheExtensionTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public static function dropConfigs()
2626

2727
QueueManager::drop('default');
2828

29-
if (Cache::getConfig('Cake/Queue.queueUnique')) {
30-
Cache::clear('Cake/Queue.queueUnique');
31-
Cache::drop('Cake/Queue.queueUnique');
29+
$cacheKey = QueueManager::getConfig('default')['uniqueCacheKey'] ?? null;
30+
if ($cacheKey) {
31+
Cache::clear($cacheKey);
32+
Cache::drop($cacheKey);
3233
}
3334
}
3435

@@ -39,14 +40,14 @@ public function testJobIsRemovedFromCacheAfterProcessing()
3940
QueueManager::push(UniqueJob::class, []);
4041

4142
$uniqueId = QueueManager::getUniqueId(UniqueJob::class, 'execute', []);
42-
$this->assertTrue(Cache::read($uniqueId, 'Cake/Queue.queueUnique'));
43+
$this->assertTrue(Cache::read($uniqueId, 'Cake/Queue.queueUnique.default'));
4344

4445
$consume();
4546

46-
$this->assertNull(Cache::read($uniqueId, 'Cake/Queue.queueUnique'));
47+
$this->assertNull(Cache::read($uniqueId, 'Cake/Queue.queueUnique.default'));
4748
}
4849

49-
protected function setupQueue($extensionArgs = [])
50+
protected function setupQueue()
5051
{
5152
Log::setConfig('debug', [
5253
'className' => 'Array',
@@ -68,7 +69,7 @@ protected function setupQueue($extensionArgs = [])
6869

6970
$extension = new ChainExtension([
7071
new LimitConsumedMessagesExtension(1),
71-
new RemoveUniqueJobIdFromCacheExtension('Cake/Queue.queueUnique'),
72+
new RemoveUniqueJobIdFromCacheExtension('Cake/Queue.queueUnique.default'),
7273
]);
7374

7475
return function () use ($client, $extension) {

tests/TestCase/QueueManagerTest.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,68 @@ public function tearDown(): void
4444
{
4545
parent::tearDown();
4646

47+
$cacheKey = QueueManager::getConfig('test')['uniqueCacheKey'] ?? null;
48+
if ($cacheKey) {
49+
Cache::clear($cacheKey);
50+
Cache::drop($cacheKey);
51+
}
52+
4753
QueueManager::drop('test');
4854
Log::drop('test');
4955

5056
// delete file based queues
5157
array_map('unlink', glob($this->fsQueuePath . DS . '*'));
52-
53-
if (Cache::getConfig('Cake/Queue.queueUnique')) {
54-
Cache::clear('Cake/Queue.queueUnique');
55-
Cache::drop('Cake/Queue.queueUnique');
56-
}
5758
}
5859

5960
public function testSetConfig()
6061
{
61-
$result = QueueManager::setConfig('test', [
62+
QueueManager::setConfig('test', [
6263
'url' => 'null:',
6364
]);
64-
$this->assertNull($result);
6565

6666
$config = QueueManager::getConfig('test');
6767
$this->assertSame('null:', $config['url']);
6868
}
6969

70-
public function testSetConfigInvalidValue()
70+
public function testSetMultipleConfigs()
71+
{
72+
QueueManager::setConfig('test', [
73+
'url' => 'null:',
74+
'uniqueCache' => [
75+
'engine' => 'File',
76+
],
77+
'logger' => 'debug',
78+
]);
79+
80+
QueueManager::setConfig('other', [
81+
'url' => 'null:',
82+
'uniqueCache' => [
83+
'engine' => 'File',
84+
],
85+
'logger' => 'debug',
86+
]);
87+
88+
$testConfig = QueueManager::getConfig('test');
89+
$this->assertSame('null:', $testConfig['url']);
90+
91+
$otherConfig = QueueManager::getConfig('other');
92+
$this->assertSame('null:', $otherConfig['url']);
93+
94+
QueueManager::drop('other');
95+
}
96+
97+
public function testSetConfigWithInvalidConfigValue()
7198
{
7299
$this->expectException(LogicException::class);
73100
QueueManager::setConfig('test', null);
74101
}
75102

103+
public function testSetConfigInvalidKeyValue()
104+
{
105+
$this->expectException(LogicException::class);
106+
QueueManager::setConfig(['test' => []], 'default');
107+
}
108+
76109
public function testSetConfigNoUrl()
77110
{
78111
$this->expectException(BadMethodCallException::class);

0 commit comments

Comments
 (0)