Fixed: re-introduce creator in async doc generation

This commit is contained in:
Julien Fastré 2023-03-15 13:38:19 +01:00
parent d1bdf41c4c
commit 44ecad2bca
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
10 changed files with 47 additions and 15 deletions

View File

@ -152,7 +152,7 @@ class ActivityContext implements
$options = $template->getOptions(); $options = $template->getOptions();
$data = []; $data = [];
$data = array_merge($data, $this->baseContextData->getData()); $data = array_merge($data, $this->baseContextData->getData($contextGenerationData['creator'] ?? null));
$data['activity'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Activity::class, 'groups' => 'docgen:read']); $data['activity'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Activity::class, 'groups' => 'docgen:read']);
$data['course'] = $this->normalizer->normalize($entity->getAccompanyingPeriod(), 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); $data['course'] = $this->normalizer->normalize($entity->getAccompanyingPeriod(), 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']);

View File

@ -156,7 +156,7 @@ final class CalendarContext implements CalendarContextInterface
$options = $this->getOptions($template); $options = $this->getOptions($template);
$data = array_merge( $data = array_merge(
$this->baseContextData->getData(), $this->baseContextData->getData($contextGenerationData['creator'] ?? null),
[ [
'calendar' => $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Calendar::class, 'groups' => ['docgen:read']]), 'calendar' => $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Calendar::class, 'groups' => ['docgen:read']]),
] ]

View File

@ -205,7 +205,7 @@ final class CalendarContextTest extends TestCase
?NormalizerInterface $normalizer = null ?NormalizerInterface $normalizer = null
): CalendarContext { ): CalendarContext {
$baseContext = $this->prophesize(BaseContextData::class); $baseContext = $this->prophesize(BaseContextData::class);
$baseContext->getData()->willReturn(['base_context' => 'data']); $baseContext->getData(null)->willReturn(['base_context' => 'data']);
$personRender = $this->prophesize(PersonRender::class); $personRender = $this->prophesize(PersonRender::class);
$personRender->renderString(Argument::type(Person::class), [])->willReturn('person name'); $personRender->renderString(Argument::type(Person::class), [])->willReturn('person name');

View File

@ -21,18 +21,14 @@ class BaseContextData
{ {
private NormalizerInterface $normalizer; private NormalizerInterface $normalizer;
private Security $security; public function __construct(NormalizerInterface $normalizer)
public function __construct(Security $security, NormalizerInterface $normalizer)
{ {
$this->security = $security;
$this->normalizer = $normalizer; $this->normalizer = $normalizer;
} }
public function getData(): array public function getData(?User $user = null): array
{ {
$data = []; $data = [];
$user = $this->security->getUser();
$data['creator'] = $this->normalizer->normalize( $data['creator'] = $this->normalizer->normalize(
$user instanceof User ? $user : null, $user instanceof User ? $user : null,

View File

@ -9,6 +9,7 @@ use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface;
use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException; use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\File;
@ -55,7 +56,8 @@ class Generator implements GeneratorInterface
array $contextGenerationDataNormalized, array $contextGenerationDataNormalized,
?StoredObject $destinationStoredObject = null, ?StoredObject $destinationStoredObject = null,
bool $isTest = false, bool $isTest = false,
?File $testFile = null ?File $testFile = null,
?User $creator = null
): ?string { ): ?string {
if ($destinationStoredObject instanceof StoredObject && StoredObject::STATUS_PENDING !== $destinationStoredObject->getStatus()) { if ($destinationStoredObject instanceof StoredObject && StoredObject::STATUS_PENDING !== $destinationStoredObject->getStatus()) {
$this->logger->info(self::LOG_PREFIX.'Aborting generation of an already generated document'); $this->logger->info(self::LOG_PREFIX.'Aborting generation of an already generated document');
@ -80,6 +82,7 @@ class Generator implements GeneratorInterface
$contextGenerationDataNormalized = array_merge( $contextGenerationDataNormalized = array_merge(
$contextGenerationDataNormalized, $contextGenerationDataNormalized,
['creator' => $creator],
$context instanceof DocGeneratorContextWithPublicFormInterface ? $context instanceof DocGeneratorContextWithPublicFormInterface ?
$context->contextGenerationDataDenormalize($template, $entity, $contextGenerationDataNormalized) $context->contextGenerationDataDenormalize($template, $entity, $contextGenerationDataNormalized)
: [] : []

View File

@ -4,6 +4,7 @@ namespace Chill\DocGeneratorBundle\Service\Generator;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Entity\User;
use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\File;
interface GeneratorInterface interface GeneratorInterface
@ -22,6 +23,7 @@ interface GeneratorInterface
array $contextGenerationDataNormalized, array $contextGenerationDataNormalized,
?StoredObject $destinationStoredObject = null, ?StoredObject $destinationStoredObject = null,
bool $isTest = false, bool $isTest = false,
?File $testFile = null ?File $testFile = null,
?User $creator = null
): ?string; ): ?string;
} }

View File

@ -6,7 +6,9 @@ use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\DocGeneratorBundle\Service\Generator\Generator; use Chill\DocGeneratorBundle\Service\Generator\Generator;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Repository\StoredObjectRepository; use Chill\DocStoreBundle\Repository\StoredObjectRepository;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
@ -23,18 +25,28 @@ class RequestGenerationHandler implements MessageHandlerInterface
private Generator $generator; private Generator $generator;
private LoggerInterface $logger;
private UserRepositoryInterface $userRepository;
public const AUTHORIZED_TRIALS = 5; public const AUTHORIZED_TRIALS = 5;
private const LOG_PREFIX = '[docgen message handler] ';
public function __construct( public function __construct(
DocGeneratorTemplateRepository $docGeneratorTemplateRepository, DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
EntityManagerInterface $entityManager, EntityManagerInterface $entityManager,
Generator $generator, Generator $generator,
StoredObjectRepository $storedObjectRepository LoggerInterface $logger,
StoredObjectRepository $storedObjectRepository,
UserRepositoryInterface $userRepository
) { ) {
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->generator = $generator; $this->generator = $generator;
$this->logger = $logger;
$this->storedObjectRepository = $storedObjectRepository; $this->storedObjectRepository = $storedObjectRepository;
$this->userRepository = $userRepository;
} }
public function __invoke(RequestGenerationMessage $message) public function __invoke(RequestGenerationMessage $message)
@ -51,6 +63,8 @@ class RequestGenerationHandler implements MessageHandlerInterface
throw new UnrecoverableMessageHandlingException('maximum number of retry reached'); throw new UnrecoverableMessageHandlingException('maximum number of retry reached');
} }
$creator = $this->userRepository->find($message->getCreatorId());
$destinationStoredObject->addGenerationTrial(); $destinationStoredObject->addGenerationTrial();
$this->entityManager->createQuery('UPDATE '.StoredObject::class.' s SET s.generationTrialsCounter = s.generationTrialsCounter + 1 WHERE s.id = :id') $this->entityManager->createQuery('UPDATE '.StoredObject::class.' s SET s.generationTrialsCounter = s.generationTrialsCounter + 1 WHERE s.id = :id')
->setParameter('id', $destinationStoredObject->getId()) ->setParameter('id', $destinationStoredObject->getId())
@ -60,7 +74,16 @@ class RequestGenerationHandler implements MessageHandlerInterface
$template, $template,
$message->getEntityId(), $message->getEntityId(),
$message->getContextGenerationData(), $message->getContextGenerationData(),
$destinationStoredObject $destinationStoredObject,
false,
null,
$creator
); );
$this->logger->info(self::LOG_PREFIX.'Request generation finished', [
'template_id' => $message->getTemplateId(),
'destination_stored_object' => $message->getDestinationStoredObjectId(),
'duration_int' => (new \DateTimeImmutable('now'))->getTimestamp() - $message->getCreatedAt()->getTimestamp(),
]);
} }
} }

View File

@ -18,6 +18,8 @@ class RequestGenerationMessage
private array $contextGenerationData; private array $contextGenerationData;
private \DateTimeImmutable $createdAt;
public function __construct( public function __construct(
User $creator, User $creator,
DocGeneratorTemplate $template, DocGeneratorTemplate $template,
@ -30,6 +32,7 @@ class RequestGenerationMessage
$this->entityId = $entityId; $this->entityId = $entityId;
$this->destinationStoredObjectId = $destinationStoredObject->getId(); $this->destinationStoredObjectId = $destinationStoredObject->getId();
$this->contextGenerationData = $contextGenerationData; $this->contextGenerationData = $contextGenerationData;
$this->createdAt = new \DateTimeImmutable('now');
} }
public function getCreatorId(): int public function getCreatorId(): int
@ -56,4 +59,9 @@ class RequestGenerationMessage
{ {
return $this->contextGenerationData; return $this->contextGenerationData;
} }
public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}
} }

View File

@ -204,7 +204,7 @@ class AccompanyingPeriodContext implements
$options = $template->getOptions(); $options = $template->getOptions();
$data = []; $data = [];
$data = array_merge($data, $this->baseContextData->getData()); $data = array_merge($data, $this->baseContextData->getData($contextGenerationData['creator'] ?? null));
$data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']);
foreach (['mainPerson', 'person1', 'person2'] as $k) { foreach (['mainPerson', 'person1', 'person2'] as $k) {

View File

@ -165,7 +165,7 @@ final class PersonContext implements PersonContextInterface
} }
$data = []; $data = [];
$data = array_merge($data, $this->baseContextData->getData()); $data = array_merge($data, $this->baseContextData->getData($contextGenerationData['creator'] ?? null));
$data['person'] = $this->normalizer->normalize($entity, 'docgen', [ $data['person'] = $this->normalizer->normalize($entity, 'docgen', [
'docgen:expects' => Person::class, 'docgen:expects' => Person::class,
'groups' => ['docgen:read'], 'groups' => ['docgen:read'],