Skip to content

Commit b1f459c

Browse files
cancan101clauderobertSt7
authored
Fix TypeError in EnqueueRelatedIdsHandler when element was deleted (#431)
EnqueueRelatedIdsMessage carries only an element ID. If the element is deleted between the dispatch of the message and its consumption by a messenger worker, ElementService::getElementByType() returns null and passing it to IndexQueueService::updateIndexQueue() throws a TypeError, sending the message to the failed transport where retries can never succeed. Skip processing in that case, mirroring the null handling of the other ID-based message handlers. Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: robertSt7 <robert.steinkellner@pimcore.com>
1 parent 9393bb5 commit b1f459c

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

src/MessageHandler/EnqueueRelatedIdsHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function __invoke(EnqueueRelatedIdsMessage $message): void
4040
$elementType = $message->getElementType();
4141
$element = $this->elementService->getElementByType($message->getElementId(), $elementType->value);
4242

43+
if ($element === null) {
44+
return;
45+
}
46+
4347
if ($elementType === ElementType::DATA_OBJECT) {
4448
$inheritanceBackup = AbstractObject::getGetInheritedValues();
4549
AbstractObject::setGetInheritedValues(true);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* This source file is available under the terms of the
6+
* Pimcore Open Core License (POCL)
7+
* Full copyright and license information is available in
8+
* LICENSE.md which is distributed with this source code.
9+
*
10+
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
11+
* @license Pimcore Open Core License (POCL)
12+
*/
13+
14+
namespace Pimcore\Bundle\GenericDataIndexBundle\Tests\Unit\MessageHandler;
15+
16+
use Codeception\Stub\Expected;
17+
use Codeception\Test\Unit;
18+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
19+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation;
20+
use Pimcore\Bundle\GenericDataIndexBundle\Message\EnqueueRelatedIdsMessage;
21+
use Pimcore\Bundle\GenericDataIndexBundle\MessageHandler\EnqueueRelatedIdsHandler;
22+
use Pimcore\Bundle\GenericDataIndexBundle\Service\ElementServiceInterface;
23+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\QueueMessagesDispatcher;
24+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueueServiceInterface;
25+
use ReflectionClass;
26+
27+
/**
28+
* @internal
29+
*/
30+
final class EnqueueRelatedIdsHandlerTest extends Unit
31+
{
32+
public function testDeletedElementSkipsProcessing(): void
33+
{
34+
// QueueMessagesDispatcher is final and cannot be doubled; the guard
35+
// returns before it is used, so an uninitialized instance is safe here.
36+
$queueMessagesDispatcher = (new ReflectionClass(QueueMessagesDispatcher::class))
37+
->newInstanceWithoutConstructor();
38+
39+
$handler = new EnqueueRelatedIdsHandler(
40+
$this->makeEmpty(IndexQueueServiceInterface::class, [
41+
'updateIndexQueue' => Expected::never(),
42+
]),
43+
$queueMessagesDispatcher,
44+
$this->makeEmpty(ElementServiceInterface::class, [
45+
'getElementByType' => null,
46+
])
47+
);
48+
49+
$handler(new EnqueueRelatedIdsMessage(
50+
999,
51+
ElementType::ASSET,
52+
IndexQueueOperation::UPDATE->value,
53+
false
54+
));
55+
}
56+
}

0 commit comments

Comments
 (0)