Skip to content

Commit 016d2f3

Browse files
committed
Async adding items for update/add operation
1 parent ae1b417 commit 016d2f3

File tree

10 files changed

+193
-40
lines changed

10 files changed

+193
-40
lines changed

src/EventSubscriber/AssetIndexUpdateSubscriber.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public function updateAsset(AssetEvent $event): void
5454
->updateIndexQueue(
5555
element: $event->getAsset(),
5656
operation: IndexQueueOperation::UPDATE->value,
57-
processSynchronously: $this->synchronousProcessing->isEnabled()
57+
processSynchronously: $this->synchronousProcessing->isEnabled(),
58+
enqueueRelatedItemsAsync:true,
5859
)
5960
->commit();
6061

src/EventSubscriber/DataObjectIndexUpdateSubscriber.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
2222
use Pimcore\Event\DataObjectEvents;
2323
use Pimcore\Event\Model\DataObjectEvent;
24-
use Pimcore\Model\DataObject\AbstractObject;
24+
use Pimcore\Model\DataObject\Service;
2525
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2626

2727
/**
@@ -62,19 +62,20 @@ public function updateDataObject(DataObjectEvent $event): void
6262
return;
6363
}
6464

65-
$inheritanceBackup = AbstractObject::getGetInheritedValues();
66-
AbstractObject::setGetInheritedValues(true);
67-
68-
$this->indexQueueService
69-
->updateIndexQueue(
70-
element: $event->getObject(),
71-
operation: IndexQueueOperation::UPDATE->value,
72-
processSynchronously: $this->synchronousProcessing->isEnabled()
73-
)
74-
->commit();
75-
$this->queueMessagesDispatcher->dispatchQueueMessages();
76-
77-
AbstractObject::setGetInheritedValues($inheritanceBackup);
65+
$dataObject = $event->getObject();
66+
Service::useInheritedValues(true, fn () =>
67+
$this->indexQueueService
68+
->updateIndexQueue(
69+
$dataObject,
70+
IndexQueueOperation::UPDATE->value,
71+
$this->synchronousProcessing->isEnabled(),
72+
$dataObject->hasChildren(includingUnpublished: true),
73+
true
74+
)
75+
->commit()
76+
);
77+
78+
$this->queueMessagesDispatcher->dispatchQueueMessages();
7879
}
7980

8081
public function deleteDataObject(DataObjectEvent $event): void
@@ -83,11 +84,13 @@ public function deleteDataObject(DataObjectEvent $event): void
8384
return;
8485
}
8586

87+
$dataObject = $event->getObject();
8688
$this->indexQueueService
8789
->updateIndexQueue(
88-
element: $event->getObject(),
89-
operation: IndexQueueOperation::DELETE->value,
90-
processSynchronously: $this->synchronousProcessing->isEnabled()
90+
$dataObject,
91+
IndexQueueOperation::DELETE->value,
92+
$this->synchronousProcessing->isEnabled(),
93+
$dataObject->hasChildren(includingUnpublished: true)
9194
)
9295
->commit();
9396
$this->queueMessagesDispatcher->dispatchQueueMessages();

src/EventSubscriber/DocumentIndexUpdateSubscriber.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public function updateDocument(DocumentEvent $event): void
5454
->updateIndexQueue(
5555
element: $event->getDocument(),
5656
operation: IndexQueueOperation::UPDATE->value,
57-
processSynchronously: $this->synchronousProcessing->isEnabled()
57+
processSynchronously: $this->synchronousProcessing->isEnabled(),
58+
enqueueRelatedItemsAsync: true
5859
)
5960
->commit();
6061

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Message;
15+
16+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
17+
18+
/**
19+
* @internal
20+
*/
21+
final readonly class EnqueueRelatedIdsMessage
22+
{
23+
public function __construct(
24+
private int $elementId,
25+
private ElementType $elementType,
26+
private string $operation,
27+
private bool $addParentElement
28+
)
29+
{
30+
}
31+
32+
public function getElementId(): int
33+
{
34+
return $this->elementId;
35+
}
36+
37+
public function getElementType(): ElementType
38+
{
39+
return $this->elementType;
40+
}
41+
42+
public function getOperation(): string
43+
{
44+
return $this->operation;
45+
}
46+
47+
public function getAddParentElement(): bool
48+
{
49+
return $this->addParentElement;
50+
}
51+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\MessageHandler;
15+
16+
use Exception;
17+
use Pimcore\Model\Element\Service;
18+
use Pimcore\Model\Element\ElementInterface;
19+
use Pimcore\Model\DataObject\AbstractObject;
20+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
22+
use Pimcore\Bundle\GenericDataIndexBundle\Service\ElementServiceInterface;
23+
use Pimcore\Bundle\GenericDataIndexBundle\Message\EnqueueRelatedIdsMessage;
24+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueueServiceInterface;
25+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\QueueMessagesDispatcher;
26+
27+
/**
28+
* @internal
29+
*/
30+
#[AsMessageHandler]
31+
final readonly class EnqueueRelatedIdsHandler
32+
{
33+
public function __construct(
34+
private IndexQueueServiceInterface $indexQueueService,
35+
private QueueMessagesDispatcher $queueMessagesDispatcher,
36+
private ElementServiceInterface $elementService
37+
) {
38+
}
39+
40+
public function __invoke(EnqueueRelatedIdsMessage $message): void
41+
{
42+
$inheritanceBackup = null;
43+
$elementType = $message->getElementType();
44+
45+
$element = $this->elementService->getElementByType($message->getElementId(), $elementType->value);
46+
47+
if($element === null) {
48+
$element = new ElementInterface();
49+
}
50+
if($elementType === ElementType::DATA_OBJECT) {
51+
$inheritanceBackup = AbstractObject::getGetInheritedValues();
52+
AbstractObject::setGetInheritedValues(true);
53+
}
54+
55+
$this->indexQueueService->updateIndexQueue(
56+
$element,
57+
$message->getOperation(),
58+
!$message->getAddParentElement(),
59+
true,
60+
false
61+
)->commit();
62+
63+
if($inheritanceBackup !== null) {
64+
AbstractObject::setGetInheritedValues($inheritanceBackup);
65+
}
66+
67+
$this->queueMessagesDispatcher->dispatchQueueMessages();
68+
}
69+
}

src/Service/SearchIndex/IndexQueueService.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex;
1515

1616
use Exception;
17+
use Pimcore\Model\Asset;
18+
use Pimcore\Model\Element\ElementInterface;
19+
use Symfony\Component\Messenger\MessageBusInterface;
1720
use Pimcore\Bundle\GenericDataIndexBundle\Entity\IndexQueue;
18-
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
1922
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
20-
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation;
21-
use Pimcore\Bundle\GenericDataIndexBundle\Exception\HandleIndexQueueEntriesException;
23+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
2224
use Pimcore\Bundle\GenericDataIndexBundle\Exception\IndexDataException;
23-
use Pimcore\Bundle\GenericDataIndexBundle\Exception\InvalidArgumentException;
2425
use Pimcore\Bundle\GenericDataIndexBundle\Repository\IndexQueueRepository;
25-
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\BulkOperationServiceInterface;
26-
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\PathServiceInterface;
2726
use Pimcore\Bundle\GenericDataIndexBundle\Service\ElementServiceInterface;
27+
use Pimcore\Bundle\GenericDataIndexBundle\Message\EnqueueRelatedIdsMessage;
28+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\InvalidArgumentException;
29+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation;
30+
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\PathServiceInterface;
31+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\HandleIndexQueueEntriesException;
32+
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\BulkOperationServiceInterface;
2833
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
2934
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexService\IndexServiceInterface;
30-
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
31-
use Pimcore\Model\Asset;
32-
use Pimcore\Model\Element\ElementInterface;
3335

3436
/**
3537
* @internal
@@ -45,14 +47,17 @@ public function __construct(
4547
private readonly IndexQueueRepository $indexQueueRepository,
4648
private readonly EnqueueServiceInterface $enqueueService,
4749
private readonly ElementServiceInterface $elementService,
48-
private readonly SearchIndexConfigServiceInterface $searchIndexConfigService
50+
private readonly SearchIndexConfigServiceInterface $searchIndexConfigService,
51+
private readonly MessageBusInterface $messageBus
4952
) {
5053
}
5154

5255
public function updateIndexQueue(
5356
ElementInterface $element,
5457
string $operation,
55-
bool $processSynchronously = false
58+
bool $processSynchronously = false,
59+
bool $enqueueRelatedItems = true,
60+
bool $enqueueRelatedItemsAsync = false
5661
): IndexQueueService {
5762
try {
5863
$this->checkOperationValid($operation);
@@ -61,8 +66,14 @@ public function updateIndexQueue(
6166
$this->doHandleIndexData($element, $operation);
6267
}
6368

64-
$this->handleQueueByOperation($element, $operation, $processSynchronously);
65-
69+
if($enqueueRelatedItems || $processSynchronously === false) {
70+
if ($enqueueRelatedItemsAsync) {
71+
$this->dispatchEnqueueRelatedIdsMessage($element, $operation, !$processSynchronously);
72+
} else {
73+
$this->handleQueueByOperation($element, $operation, $processSynchronously);
74+
}
75+
}
76+
6677
$this->pathService->rewriteChildrenIndexPaths($element);
6778
} catch (Exception $e) {
6879
$this->logger->error(
@@ -197,4 +208,19 @@ private function checkOperationValid(string $operation): void
197208
throw new IndexDataException(sprintf('Operation %s not valid', $operation));
198209
}
199210
}
211+
212+
private function dispatchEnqueueRelatedIdsMessage(
213+
ElementInterface $element,
214+
string $operation,
215+
bool $addParentElement
216+
): void {
217+
$this->messageBus->dispatch(
218+
new EnqueueRelatedIdsMessage(
219+
$element->getId(),
220+
$this->elementService->getElementType($element),
221+
$operation,
222+
$addParentElement
223+
)
224+
);
225+
}
200226
}

src/Service/SearchIndex/IndexQueueServiceInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ interface IndexQueueServiceInterface
2424
public function updateIndexQueue(
2525
ElementInterface $element,
2626
string $operation,
27-
bool $processSynchronously = false
27+
bool $processSynchronously = false,
28+
bool $enqueueRelatedItems = true,
29+
bool $enqueueRelatedItemsAsync = false
2830
): IndexQueueService;
2931

3032
/**

src/Service/SearchIndex/IndexService/ElementTypeAdapter/AssetTypeAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getRelatedItemsOnUpdateQuery(
8585
): ?QueryBuilder {
8686

8787
$selects = [
88-
(string)$element->getId(),
88+
(string)$element->getId() . " as " . $this->dbConnection->quoteIdentifier('id'),
8989
"'" . ElementType::ASSET->value . "' as " . $this->dbConnection->quoteIdentifier('elementType'),
9090
"'" . IndexName::ASSET->value . "' as " . $this->dbConnection->quoteIdentifier('className'),
9191
"'$operation' as " . $this->dbConnection->quoteIdentifier('operation'),

src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ private function getSelectParameters(
188188
int $operationTime
189189
): array {
190190
return [
191-
(string)$element->getId(),
192-
"'" . ElementType::DATA_OBJECT->value . "'",
193-
$this->getIndexName($element, $operation),
194-
"'$operation'",
195-
"'$operationTime'",
191+
(string)$element->getId() . " as " . $this->dbConnection->quoteIdentifier('id'),
192+
"'" . ElementType::DATA_OBJECT->value . "' as " . $this->dbConnection->quoteIdentifier('elementType'),
193+
$this->getIndexName($element, $operation) . " as " . $this->dbConnection->quoteIdentifier('className'),
194+
"'$operation' as " . $this->dbConnection->quoteIdentifier('operation'),
195+
"'$operationTime' as " . $this->dbConnection->quoteIdentifier('operationTime'),
196196
'0',
197197
];
198198
}

src/Service/SearchIndex/IndexService/ElementTypeAdapter/DocumentTypeAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getRelatedItemsOnUpdateQuery(
8484
bool $includeElement = false
8585
): ?QueryBuilder {
8686
$selects = [
87-
(string)$element->getId(),
87+
(string)$element->getId() . " as " . $this->dbConnection->quoteIdentifier('id'),
8888
"'" . ElementType::DOCUMENT->value . "' as " . $this->dbConnection->quoteIdentifier('elementType'),
8989
"'" . IndexName::DOCUMENT->value . "' as " . $this->dbConnection->quoteIdentifier('className'),
9090
"'$operation' as " . $this->dbConnection->quoteIdentifier('operation'),

0 commit comments

Comments
 (0)