From ef165b1d0924ba52b23cffd9e489e4c5e4790d9f Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Thu, 29 Aug 2024 13:44:40 +0200 Subject: [PATCH] wip --- src/Blob/BlobSubscriber.php | 24 +++++++++++++++---- src/Blob/TypesenseTask.php | 19 +++++++++++++++ .../DbpRelayCabinetExtension.php | 3 +++ src/TypesenseSync/TypesenseSync.php | 2 +- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/Blob/TypesenseTask.php diff --git a/src/Blob/BlobSubscriber.php b/src/Blob/BlobSubscriber.php index 47ce09f..1ec98dd 100644 --- a/src/Blob/BlobSubscriber.php +++ b/src/Blob/BlobSubscriber.php @@ -12,18 +12,22 @@ use Dbp\Relay\CabinetBundle\TypesenseSync\DocumentTranslator; use Dbp\Relay\CabinetBundle\TypesenseSync\TypesenseSync; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Messenger\Attribute\AsMessageHandler; +use Symfony\Component\Messenger\MessageBusInterface; class BlobSubscriber implements EventSubscriberInterface { private ConfigurationService $config; private DocumentTranslator $translator; private TypesenseSync $typesenseSync; + private MessageBusInterface $messageBus; - public function __construct(ConfigurationService $config, DocumentTranslator $translator, TypesenseSync $typesenseSync) + public function __construct(ConfigurationService $config, DocumentTranslator $translator, TypesenseSync $typesenseSync, MessageBusInterface $messageBus) { $this->config = $config; $this->translator = $translator; $this->typesenseSync = $typesenseSync; + $this->messageBus = $messageBus; } private function isForCabinet(FileData $fileData): bool @@ -70,7 +74,7 @@ public function onFileAdded(AddFileDataByPostSuccessEvent $event) } $translated = $this->translateMetadata($fileData); - $this->typesenseSync->upsertPartialFile($translated); + $this->messageBus->dispatch(new TypesenseTask('upsert', $translated)); } public function onFileChanged(ChangeFileDataByPatchSuccessEvent $event) @@ -81,7 +85,7 @@ public function onFileChanged(ChangeFileDataByPatchSuccessEvent $event) } $translated = $this->translateMetadata($fileData); - $this->typesenseSync->upsertPartialFile($translated); + $this->messageBus->dispatch(new TypesenseTask('upsert', $translated)); } public function onFileRemoved(DeleteFileDataByDeleteSuccessEvent $event) @@ -92,6 +96,18 @@ public function onFileRemoved(DeleteFileDataByDeleteSuccessEvent $event) } $translated = $this->translateMetadata($fileData); - $this->typesenseSync->removePartialFile($translated); + $this->messageBus->dispatch(new TypesenseTask('delete', $translated)); + } + + #[AsMessageHandler] + public function handleTypesenseTask(TypesenseTask $task): void + { + if ($task->getAction() === 'upsert') { + $this->typesenseSync->upsertPartialFile($task->getDocument()); + } elseif ($task->getAction() === 'delete') { + $this->typesenseSync->deletePartialFile($task->getDocument()); + } else { + throw new \RuntimeException('unsupported task: ' . $task->getAction()); + } } } diff --git a/src/Blob/TypesenseTask.php b/src/Blob/TypesenseTask.php new file mode 100644 index 0000000..58eab0d --- /dev/null +++ b/src/Blob/TypesenseTask.php @@ -0,0 +1,19 @@ +action; + } + + public function getDocument(): array + { + return $this->document; + } +} \ No newline at end of file diff --git a/src/DependencyInjection/DbpRelayCabinetExtension.php b/src/DependencyInjection/DbpRelayCabinetExtension.php index 859c168..e7dcd1b 100644 --- a/src/DependencyInjection/DbpRelayCabinetExtension.php +++ b/src/DependencyInjection/DbpRelayCabinetExtension.php @@ -5,6 +5,7 @@ namespace Dbp\Relay\CabinetBundle\DependencyInjection; use Dbp\Relay\CabinetBundle\Authorization\AuthorizationService; +use Dbp\Relay\CabinetBundle\Blob\TypesenseTask; use Dbp\Relay\CabinetBundle\Service\ConfigurationService; use Dbp\Relay\CoreBundle\Extension\ExtensionTrait; use Symfony\Component\Config\FileLocator; @@ -88,5 +89,7 @@ public function prepend(ContainerBuilder $container): void 'Dbp\Relay\CabinetBundle\Migrations' => __DIR__.'/../Migrations', ], ]); + + $this->addQueueMessageClass($container, TypesenseTask::class); } } diff --git a/src/TypesenseSync/TypesenseSync.php b/src/TypesenseSync/TypesenseSync.php index 4b93478..f54163e 100644 --- a/src/TypesenseSync/TypesenseSync.php +++ b/src/TypesenseSync/TypesenseSync.php @@ -168,7 +168,7 @@ public function upsertPartialFile(array $partialFileDocument): void $this->searchIndex->addDocumentsToCollection($collectionName, [$partialFileDocument]); } - public function removePartialFile(array $partialFileDocument): void + public function deletePartialFile(array $partialFileDocument): void { $collectionName = $this->searchIndex->getAliasName(); $id = $partialFileDocument['id'];