Skip to content

Commit

Permalink
feat: Moved all document processes from event-subscribers to async me…
Browse files Browse the repository at this point in the history
…ssenger, read AV media size and duration
  • Loading branch information
ambroisemaupate committed Sep 6, 2022
1 parent fcb9e9d commit 251b9b5
Show file tree
Hide file tree
Showing 22 changed files with 698 additions and 56 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"doctrine/orm": "^2.12.2",
"gedmo/doctrine-extensions": "^2.4 || ^3.0.0",
"inlinestyle/inlinestyle": "~1.2.7",
"james-heinrich/getid3": "^1.9",
"jms/serializer": "^3.1.1",
"jms/serializer-bundle": "^3.10.0",
"lexik/jwt-authentication-bundle": "^2.13",
Expand Down
66 changes: 62 additions & 4 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,68 @@ services:
autowire: true
autoconfigure: false

RZ\Roadiz\Document\EventSubscriber\:
resource: '%kernel.project_dir%/vendor/roadiz/documents/src/Roadiz/Document/EventSubscriber/'
autowire: true
autoconfigure: true
# Do not register roadiz/document packages event-subscribers
# They've been replaced with MessageHandlers

RZ\Roadiz\CoreBundle\Document\MessageHandler\AbstractDocumentMessageHandler:
autoconfigure: false
abstract: true

RZ\Roadiz\CoreBundle\Document\MessageHandler\AbstractLockingDocumentMessageHandler:
autoconfigure: false
abstract: true

RZ\Roadiz\CoreBundle\Document\MessageHandler\DocumentAverageColorMessageHandler:
autoconfigure: false
tags:
- name: messenger.message_handler
handles: RZ\Roadiz\CoreBundle\Document\Message\DocumentAverageColorMessage
- { name: monolog.logger, channel: messenger }

RZ\Roadiz\CoreBundle\Document\MessageHandler\DocumentExifMessageHandler:
autoconfigure: false
tags:
- name: messenger.message_handler
handles: RZ\Roadiz\CoreBundle\Document\Message\DocumentExifMessage
- { name: monolog.logger, channel: messenger }

RZ\Roadiz\CoreBundle\Document\MessageHandler\DocumentFilesizeMessageHandler:
autoconfigure: false
tags:
- name: messenger.message_handler
handles: RZ\Roadiz\CoreBundle\Document\Message\DocumentFilesizeMessage
- { name: monolog.logger, channel: messenger }

RZ\Roadiz\CoreBundle\Document\MessageHandler\DocumentRawMessageHandler:
autoconfigure: false
tags:
- name: messenger.message_handler
handles: RZ\Roadiz\CoreBundle\Document\Message\DocumentRawMessage
priority: -100
- { name: monolog.logger, channel: messenger }

RZ\Roadiz\CoreBundle\Document\MessageHandler\DocumentSizeMessageHandler:
autoconfigure: false
tags:
- name: messenger.message_handler
handles: RZ\Roadiz\CoreBundle\Document\Message\DocumentSizeMessage
- { name: monolog.logger, channel: messenger }

RZ\Roadiz\CoreBundle\Document\MessageHandler\DocumentSvgMessageHandler:
autoconfigure: false
tags:
- name: messenger.message_handler
handles: RZ\Roadiz\CoreBundle\Document\Message\DocumentSvgMessage
priority: -100
- { name: monolog.logger, channel: messenger }

RZ\Roadiz\CoreBundle\Document\MessageHandler\DocumentAudioVideoMessageHandler:
autoconfigure: false
tags:
- name: messenger.message_handler
handles: RZ\Roadiz\CoreBundle\Document\Message\DocumentAudioVideoMessage
priority: -100
- { name: monolog.logger, channel: messenger }

RZ\Roadiz\CoreBundle\SearchEngine\Indexer\:
resource: '../src/SearchEngine/Indexer/'
Expand Down
63 changes: 63 additions & 0 deletions src/Document/EventSubscriber/DocumentMessageDispatchSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\EventSubscriber;

use RZ\Roadiz\Core\Events\DocumentCreatedEvent;
use RZ\Roadiz\Core\Events\DocumentUpdatedEvent;
use RZ\Roadiz\Core\Events\FilterDocumentEvent;
use RZ\Roadiz\CoreBundle\Document\Message\DocumentAudioVideoMessage;
use RZ\Roadiz\CoreBundle\Document\Message\DocumentAverageColorMessage;
use RZ\Roadiz\CoreBundle\Document\Message\DocumentExifMessage;
use RZ\Roadiz\CoreBundle\Document\Message\DocumentFilesizeMessage;
use RZ\Roadiz\CoreBundle\Document\Message\DocumentRawMessage;
use RZ\Roadiz\CoreBundle\Document\Message\DocumentSizeMessage;
use RZ\Roadiz\CoreBundle\Document\Message\DocumentSvgMessage;
use RZ\Roadiz\CoreBundle\Entity\Document;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class DocumentMessageDispatchSubscriber implements EventSubscriberInterface
{
private MessageBusInterface $bus;

/**
* @param MessageBusInterface $bus
*/
public function __construct(MessageBusInterface $bus)
{
$this->bus = $bus;
}

/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
DocumentCreatedEvent::class => ['onFilterDocumentEvent', 0],
DocumentUpdatedEvent::class => ['onFilterDocumentEvent', 0],
];
}

public function onFilterDocumentEvent(FilterDocumentEvent $event)
{
$document = $event->getDocument();
if (
$document instanceof Document &&
null !== $document->getId() &&
$document->isLocal() &&
null !== $document->getRelativePath()
) {
$this->bus->dispatch(new Envelope(new DocumentRawMessage($document->getId())));
$this->bus->dispatch(new Envelope(new DocumentFilesizeMessage($document->getId())));
$this->bus->dispatch(new Envelope(new DocumentSizeMessage($document->getId())));
$this->bus->dispatch(new Envelope(new DocumentAverageColorMessage($document->getId())));
$this->bus->dispatch(new Envelope(new DocumentExifMessage($document->getId())));
$this->bus->dispatch(new Envelope(new DocumentSvgMessage($document->getId())));
$this->bus->dispatch(new Envelope(new DocumentAudioVideoMessage($document->getId())));
}
}
}
51 changes: 0 additions & 51 deletions src/Document/EventSubscriber/ExifDocumentSubscriber.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Document/Message/AbstractDocumentMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\Message;

use RZ\Roadiz\CoreBundle\Message\AsyncMessage;

abstract class AbstractDocumentMessage implements AsyncMessage
{
private int $documentId;

/**
* @param int $documentId
*/
public function __construct(int $documentId)
{
$this->documentId = $documentId;
}

/**
* @return int
*/
public function getDocumentId(): int
{
return $this->documentId;
}
}
10 changes: 10 additions & 0 deletions src/Document/Message/DocumentAudioVideoMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\Message;

class DocumentAudioVideoMessage extends AbstractDocumentMessage
{

}
10 changes: 10 additions & 0 deletions src/Document/Message/DocumentAverageColorMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\Message;

class DocumentAverageColorMessage extends AbstractDocumentMessage
{

}
10 changes: 10 additions & 0 deletions src/Document/Message/DocumentExifMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\Message;

class DocumentExifMessage extends AbstractDocumentMessage
{

}
10 changes: 10 additions & 0 deletions src/Document/Message/DocumentFilesizeMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\Message;

class DocumentFilesizeMessage extends AbstractDocumentMessage
{

}
10 changes: 10 additions & 0 deletions src/Document/Message/DocumentRawMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\Message;

class DocumentRawMessage extends AbstractDocumentMessage
{

}
10 changes: 10 additions & 0 deletions src/Document/Message/DocumentSizeMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\Message;

class DocumentSizeMessage extends AbstractDocumentMessage
{

}
10 changes: 10 additions & 0 deletions src/Document/Message/DocumentSvgMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\Message;

class DocumentSvgMessage extends AbstractDocumentMessage
{

}
46 changes: 46 additions & 0 deletions src/Document/MessageHandler/AbstractDocumentMessageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\MessageHandler;

use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use RZ\Roadiz\Core\Models\DocumentInterface;
use RZ\Roadiz\CoreBundle\Document\Message\AbstractDocumentMessage;
use RZ\Roadiz\CoreBundle\Entity\Document;
use RZ\Roadiz\Utils\Asset\Packages;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

abstract class AbstractDocumentMessageHandler implements MessageHandlerInterface
{
protected ManagerRegistry $managerRegistry;
protected LoggerInterface $logger;
protected Packages $packages;

/**
* @param ManagerRegistry $managerRegistry
* @param LoggerInterface $messengerLogger
* @param Packages $packages
*/
public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $messengerLogger, Packages $packages)
{
$this->managerRegistry = $managerRegistry;
$this->logger = $messengerLogger;
$this->packages = $packages;
}

abstract protected function supports(DocumentInterface $document): bool;

abstract protected function processMessage(AbstractDocumentMessage $message, Document $document): void;

public function __invoke(AbstractDocumentMessage $message): void
{
$document = $this->managerRegistry->getRepository(Document::class)->find($message->getDocumentId());

if ($document instanceof Document && $this->supports($document)) {
$this->processMessage($message, $document);
$this->managerRegistry->getManager()->flush();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Document\MessageHandler;

use RZ\Roadiz\CoreBundle\Document\Message\AbstractDocumentMessage;
use RZ\Roadiz\CoreBundle\Entity\Document;
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;

/**
* Use file locking system to ensure only one async operation is done on each document file.
*/
abstract class AbstractLockingDocumentMessageHandler extends AbstractDocumentMessageHandler
{
public function __invoke(AbstractDocumentMessage $message): void
{
$document = $this->managerRegistry->getRepository(Document::class)->find($message->getDocumentId());

if ($document instanceof Document && $this->supports($document)) {
$documentPath = $this->packages->getDocumentFilePath($document);
$resource = \fopen($documentPath, "r+");

if (false === $resource) {
throw new RecoverableMessageHandlingException(sprintf(
'%s file does not exist',
$documentPath
));
}

if (\flock($resource, \LOCK_EX)) {
$this->processMessage($message, $document);
$this->managerRegistry->getManager()->flush();
\flock($resource, \LOCK_UN);
} else {
throw new RecoverableMessageHandlingException(sprintf(
'%s file is currently locked',
$documentPath
));
}
}
}
}
Loading

0 comments on commit 251b9b5

Please sign in to comment.