-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Moved all document processes from event-subscribers to async me…
…ssenger, read AV media size and duration
- Loading branch information
1 parent
fcb9e9d
commit 251b9b5
Showing
22 changed files
with
698 additions
and
56 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
63 changes: 63 additions & 0 deletions
63
src/Document/EventSubscriber/DocumentMessageDispatchSubscriber.php
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,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()))); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 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; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Document\Message; | ||
|
||
class DocumentAudioVideoMessage extends AbstractDocumentMessage | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Document\Message; | ||
|
||
class DocumentAverageColorMessage extends AbstractDocumentMessage | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Document\Message; | ||
|
||
class DocumentExifMessage extends AbstractDocumentMessage | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Document\Message; | ||
|
||
class DocumentFilesizeMessage extends AbstractDocumentMessage | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Document\Message; | ||
|
||
class DocumentRawMessage extends AbstractDocumentMessage | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Document\Message; | ||
|
||
class DocumentSizeMessage extends AbstractDocumentMessage | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Document\Message; | ||
|
||
class DocumentSvgMessage extends AbstractDocumentMessage | ||
{ | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/Document/MessageHandler/AbstractDocumentMessageHandler.php
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,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(); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Document/MessageHandler/AbstractLockingDocumentMessageHandler.php
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,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 | ||
)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.