Skip to content

Commit 837af4c

Browse files
committed
Use unique names for queue unique cache configurations
1 parent 8a0b4c7 commit 837af4c

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public static function setConfig($key, $config = null): void
127127

128128
$cacheConfig = array_merge($cacheDefaults, $config['uniqueCache']);
129129

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

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

229231
/** @psalm-suppress InvalidPropertyFetch */
230232
if (!empty($class::$shouldBeUnique)) {
231-
if (!Cache::getConfig('Cake/Queue.queueUnique')) {
233+
if (empty($config['uniqueCache'])) {
232234
throw new InvalidArgumentException(
233235
"$class::\$shouldBeUnique is set to `true` but `uniqueCache` configuration is missing."
234236
);
235237
}
236238

237239
$uniqueId = static::getUniqueId($class, $method, $data);
238240

239-
if (Cache::read($uniqueId, 'Cake/Queue.queueUnique')) {
241+
if (Cache::read($uniqueId, $config['uniqueCacheKey'])) {
240242
if ($logger) {
241243
$logger->debug(
242244
"An identical instance of $class already exists on the queue. This push will be ignored."
@@ -279,7 +281,7 @@ public static function push($className, array $data = [], array $options = []):
279281
if (!empty($class::$shouldBeUnique)) {
280282
$uniqueId = static::getUniqueId($class, $method, $data);
281283

282-
Cache::add($uniqueId, true, 'Cake/Queue.queueUnique');
284+
Cache::add($uniqueId, true, $config['uniqueCacheKey']);
283285
}
284286
}
285287

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: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,56 @@ 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 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+
7097
public function testSetConfigInvalidValue()
7198
{
7299
$this->expectException(LogicException::class);

0 commit comments

Comments
 (0)