-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use Symfony Messenger to handle event-triggered updates and prevent o…
- Loading branch information
Showing
19 changed files
with
269 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Valantic\ElasticaBridgeBundle\Messenger\Handler; | ||
|
||
use Pimcore\Model\DataObject\Concrete; | ||
use Pimcore\Model\Element\AbstractElement; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
use Valantic\ElasticaBridgeBundle\Exception\EventListener\PimcoreElementNotFoundException; | ||
use Valantic\ElasticaBridgeBundle\Messenger\Message\AbstractRefresh; | ||
|
||
/** @template TModel of AbstractElement */ | ||
#[AsMessageHandler] | ||
abstract class AbstractRefreshHandler | ||
{ | ||
protected function resolveElement(AbstractRefresh $message): AbstractElement | ||
{ | ||
/** @var class-string<TModel> $className */ | ||
$className = $message->className; | ||
|
||
try { | ||
$element = $className::getById($message->id); | ||
} catch (\Throwable) { | ||
throw new PimcoreElementNotFoundException($message->id, $message->className); | ||
} | ||
|
||
if (!$element instanceof AbstractElement) { | ||
// The element in question was deleted so we need a skeleton. | ||
/** @var TModel $element */ | ||
$element = new ($className)(); | ||
$element->setId($message->id); | ||
|
||
if ($element instanceof Concrete) { | ||
$element->setPublished(false); | ||
} | ||
} | ||
|
||
if ($element === null) { | ||
throw new PimcoreElementNotFoundException($message->id, $message->className); | ||
} | ||
|
||
return $element; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Valantic\ElasticaBridgeBundle\Messenger\Handler; | ||
|
||
use Pimcore\Model\Element\AbstractElement; | ||
use Valantic\ElasticaBridgeBundle\Messenger\Message\RefreshElement; | ||
use Valantic\ElasticaBridgeBundle\Service\PropagateChanges; | ||
|
||
/** | ||
* @template TModel of AbstractElement | ||
* | ||
* @extends AbstractRefreshHandler<TModel> | ||
*/ | ||
class RefreshElementHandler extends AbstractRefreshHandler | ||
{ | ||
public function __construct( | ||
private readonly PropagateChanges $propagateChanges, | ||
) {} | ||
|
||
public function __invoke(RefreshElement $message): void | ||
{ | ||
$element = $this->resolveElement($message); | ||
|
||
$this->propagateChanges->handle($element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Valantic\ElasticaBridgeBundle\Messenger\Handler; | ||
|
||
use Pimcore\Model\Element\AbstractElement; | ||
use Valantic\ElasticaBridgeBundle\Messenger\Message\RefreshElementInIndex; | ||
use Valantic\ElasticaBridgeBundle\Repository\IndexRepository; | ||
use Valantic\ElasticaBridgeBundle\Service\LockService; | ||
use Valantic\ElasticaBridgeBundle\Service\PropagateChanges; | ||
|
||
/** | ||
* @template TModel of AbstractElement | ||
* | ||
* @extends AbstractRefreshHandler<TModel> | ||
*/ | ||
class RefreshElementInIndexHandler extends AbstractRefreshHandler | ||
{ | ||
public function __construct( | ||
private readonly PropagateChanges $propagateChanges, | ||
private readonly LockService $lockService, | ||
private readonly IndexRepository $indexRepository, | ||
) {} | ||
|
||
public function __invoke(RefreshElementInIndex $message): void | ||
{ | ||
$index = $this->indexRepository->flattenedGet($message->index); | ||
$element = $this->resolveElement($message); | ||
|
||
if ($index->usesBlueGreenIndices() && !$this->lockService->getIndexingLock($index)->acquire()) { | ||
$this->propagateChanges->handleIndex($element, $index, $index->getBlueGreenInactiveElasticaIndex()); | ||
} | ||
|
||
$this->propagateChanges->handleIndex($element, $index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Valantic\ElasticaBridgeBundle\Messenger\Message; | ||
|
||
use Pimcore\Model\Element\ElementInterface; | ||
|
||
abstract class AbstractRefresh | ||
{ | ||
/** @var class-string<ElementInterface> */ | ||
public string $className; | ||
public int $id; | ||
|
||
protected function setElement(ElementInterface $element): void | ||
{ | ||
$this->className = $element::class; | ||
$this->id = $element->getId() ?? throw new \InvalidArgumentException('Pimcore ID is null.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Valantic\ElasticaBridgeBundle\Messenger\Message; | ||
|
||
use Pimcore\Model\Element\ElementInterface; | ||
|
||
class RefreshElement extends AbstractRefresh | ||
{ | ||
public function __construct(ElementInterface $element) | ||
{ | ||
$this->setElement($element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Valantic\ElasticaBridgeBundle\Messenger\Message; | ||
|
||
use Pimcore\Model\Element\ElementInterface; | ||
|
||
class RefreshElementInIndex extends AbstractRefresh | ||
{ | ||
public function __construct( | ||
ElementInterface $element, | ||
public readonly string $index, | ||
) { | ||
$this->setElement($element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
imports: | ||
- { resource: messenger.yaml } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
framework: | ||
messenger: | ||
enabled: true | ||
transports: | ||
elastica_bridge_index: 'doctrine://default?queue_name=elastica_bridge_index' | ||
routing: | ||
Valantic\ElasticaBridgeBundle\Messenger\Message\RefreshElement: elastica_bridge_index | ||
Valantic\ElasticaBridgeBundle\Messenger\Message\RefreshElementInIndex: elastica_bridge_index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Valantic\ElasticaBridgeBundle\Service; | ||
|
||
use Symfony\Component\Lock\LockFactory; | ||
use Symfony\Component\Lock\LockInterface; | ||
use Valantic\ElasticaBridgeBundle\Index\IndexInterface; | ||
use Valantic\ElasticaBridgeBundle\Repository\ConfigurationRepository; | ||
|
||
class LockService | ||
{ | ||
private const LOCK_PREFIX = 'pimcore-elastica-bridge'; | ||
|
||
public function __construct( | ||
private readonly LockFactory $lockFactory, | ||
private readonly ConfigurationRepository $configurationRepository, | ||
) {} | ||
|
||
public function getIndexingLock(IndexInterface $indexConfig): LockInterface | ||
{ | ||
return $this->lockFactory | ||
->createLock( | ||
sprintf('%s:indexing:%s', self::LOCK_PREFIX, $indexConfig->getName()), | ||
ttl: $this->configurationRepository->getIndexingLockTimeout() | ||
); | ||
} | ||
} |
Oops, something went wrong.