mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
Feature: [docgen] generate documents in an async queue
The documents are now generated in a queue, using symfony messenger. This queue should be configured: ```yaml # app/config/messenger.yaml framework: messenger: # reset services after consuming messages # reset_on_message: true failure_transport: failed transports: # https://symfony.com/doc/current/messenger.html#transport-configuration async: '%env(MESSENGER_TRANSPORT_DSN)%' priority: dsn: '%env(MESSENGER_TRANSPORT_DSN)%' failed: 'doctrine://default?queue_name=failed' routing: # ... other messages 'Chill\DocGeneratorBundle\Service\Messenger\RequestGenerationMessage': priority ``` `StoredObject`s now have additionnal properties: * status (pending, failure, ready (by default) ), which explain if the document is generated; * a generationTrialCounter, which is incremented on each generation trial, which prevent each generation more than 5 times; The generator computation is moved from the `DocGenTemplateController` to a `Generator` (implementing `GeneratorInterface`. There are new methods to `Context` which allow to normalize/denormalize context data to/from a messenger's `Message`.
This commit is contained in:
@@ -16,67 +16,57 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
|
||||
use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException;
|
||||
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
||||
use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface;
|
||||
use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException;
|
||||
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
|
||||
use Chill\DocGeneratorBundle\Service\Generator\GeneratorInterface;
|
||||
use Chill\DocGeneratorBundle\Service\Messenger\RequestGenerationMessage;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
// TODO à mettre dans services
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Throwable;
|
||||
use function strlen;
|
||||
|
||||
final class DocGeneratorTemplateController extends AbstractController
|
||||
{
|
||||
private HttpClientInterface $client;
|
||||
|
||||
private ContextManager $contextManager;
|
||||
|
||||
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
|
||||
|
||||
private DriverInterface $driver;
|
||||
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
private LoggerInterface $logger;
|
||||
private GeneratorInterface $generator;
|
||||
|
||||
private MessageBusInterface $messageBus;
|
||||
|
||||
private PaginatorFactory $paginatorFactory;
|
||||
|
||||
private StoredObjectManagerInterface $storedObjectManager;
|
||||
|
||||
public function __construct(
|
||||
ContextManager $contextManager,
|
||||
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
|
||||
DriverInterface $driver,
|
||||
LoggerInterface $logger,
|
||||
GeneratorInterface $generator,
|
||||
MessageBusInterface $messageBus,
|
||||
PaginatorFactory $paginatorFactory,
|
||||
HttpClientInterface $client,
|
||||
StoredObjectManagerInterface $storedObjectManager,
|
||||
EntityManagerInterface $entityManager
|
||||
) {
|
||||
$this->contextManager = $contextManager;
|
||||
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
|
||||
$this->driver = $driver;
|
||||
$this->logger = $logger;
|
||||
$this->generator = $generator;
|
||||
$this->messageBus = $messageBus;
|
||||
$this->paginatorFactory = $paginatorFactory;
|
||||
$this->client = $client;
|
||||
$this->storedObjectManager = $storedObjectManager;
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
@@ -94,7 +84,6 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
): Response {
|
||||
return $this->generateDocFromTemplate(
|
||||
$template,
|
||||
$entityClassName,
|
||||
$entityId,
|
||||
$request,
|
||||
true
|
||||
@@ -115,7 +104,6 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
): Response {
|
||||
return $this->generateDocFromTemplate(
|
||||
$template,
|
||||
$entityClassName,
|
||||
$entityId,
|
||||
$request,
|
||||
false
|
||||
@@ -185,7 +173,6 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
|
||||
private function generateDocFromTemplate(
|
||||
DocGeneratorTemplate $template,
|
||||
string $entityClassName,
|
||||
int $entityId,
|
||||
Request $request,
|
||||
bool $isTest
|
||||
@@ -206,7 +193,7 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
|
||||
if (null === $entity) {
|
||||
throw new NotFoundHttpException(
|
||||
sprintf('Entity with classname %s and id %s is not found', $entityClassName, $entityId)
|
||||
sprintf('Entity with classname %s and id %s is not found', $context->getEntityClass(), $entityId)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -259,99 +246,68 @@ final class DocGeneratorTemplateController extends AbstractController
|
||||
}
|
||||
}
|
||||
|
||||
$document = $template->getFile();
|
||||
|
||||
if ($isTest && ($contextGenerationData['test_file'] instanceof File)) {
|
||||
$dataDecrypted = file_get_contents($contextGenerationData['test_file']->getPathname());
|
||||
} else {
|
||||
try {
|
||||
$dataDecrypted = $this->storedObjectManager->read($document);
|
||||
} catch (Throwable $exception) {
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
// transform context generation data
|
||||
$contextGenerationDataSanitized =
|
||||
$context instanceof DocGeneratorContextWithPublicFormInterface ?
|
||||
$context->contextGenerationDataNormalize($template, $entity, $contextGenerationData)
|
||||
: [];
|
||||
|
||||
// if is test, render the data or generate the doc
|
||||
if ($isTest && isset($form) && $form['show_data']->getData()) {
|
||||
return $this->render('@ChillDocGenerator/Generator/debug_value.html.twig', [
|
||||
'datas' => json_encode($context->getData($template, $entity, $contextGenerationData), JSON_PRETTY_PRINT)
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$generatedResource = $this
|
||||
->driver
|
||||
->generateFromString(
|
||||
$dataDecrypted,
|
||||
$template->getFile()->getType(),
|
||||
$context->getData($template, $entity, $contextGenerationData),
|
||||
$template->getFile()->getFilename()
|
||||
);
|
||||
} catch (TemplateException $e) {
|
||||
return new Response(
|
||||
implode("\n", $e->getErrors()),
|
||||
400,
|
||||
[
|
||||
'Content-Type' => 'text/plain',
|
||||
]
|
||||
} elseif ($isTest) {
|
||||
$generated = $this->generator->generateDocFromTemplate(
|
||||
$template,
|
||||
$entityId,
|
||||
$contextGenerationDataSanitized,
|
||||
null,
|
||||
true,
|
||||
isset($form) ? $form['test_file']->getData() : null
|
||||
);
|
||||
}
|
||||
|
||||
if ($isTest) {
|
||||
return new Response(
|
||||
$generatedResource,
|
||||
$generated,
|
||||
Response::HTTP_OK,
|
||||
[
|
||||
'Content-Transfer-Encoding', 'binary',
|
||||
'Content-Type' => 'application/vnd.oasis.opendocument.text',
|
||||
'Content-Disposition' => 'attachment; filename="generated.odt"',
|
||||
'Content-Length' => strlen($generatedResource),
|
||||
'Content-Length' => strlen($generated),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/** @var StoredObject $storedObject */
|
||||
$storedObject = (new ObjectNormalizer())
|
||||
->denormalize(
|
||||
[
|
||||
'type' => $template->getFile()->getType(),
|
||||
'filename' => sprintf('%s_odt', uniqid('doc_', true)),
|
||||
],
|
||||
StoredObject::class
|
||||
);
|
||||
|
||||
try {
|
||||
$this->storedObjectManager->write($storedObject, $generatedResource);
|
||||
} catch (Throwable $exception) {
|
||||
throw $exception;
|
||||
}
|
||||
// this is not a test
|
||||
// we prepare the object to store the document
|
||||
$storedObject = (new StoredObject())
|
||||
->setStatus(StoredObject::STATUS_PENDING)
|
||||
;
|
||||
|
||||
$this->entityManager->persist($storedObject);
|
||||
|
||||
try {
|
||||
$context
|
||||
->storeGenerated(
|
||||
$template,
|
||||
$storedObject,
|
||||
$entity,
|
||||
$contextGenerationData
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
$this
|
||||
->logger
|
||||
->error(
|
||||
'Unable to store the associated document to entity',
|
||||
[
|
||||
'entityClassName' => $entityClassName,
|
||||
'entityId' => $entityId,
|
||||
'contextKey' => $context->getName(),
|
||||
]
|
||||
);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
// we store the generated document
|
||||
$context
|
||||
->storeGenerated(
|
||||
$template,
|
||||
$storedObject,
|
||||
$entity,
|
||||
$contextGenerationData
|
||||
);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$this->messageBus->dispatch(
|
||||
new RequestGenerationMessage(
|
||||
$this->getUser(),
|
||||
$template,
|
||||
$entityId,
|
||||
$storedObject,
|
||||
$contextGenerationDataSanitized,
|
||||
)
|
||||
);
|
||||
|
||||
return $this
|
||||
->redirectToRoute(
|
||||
'chill_wopi_file_edit',
|
||||
|
Reference in New Issue
Block a user