docGeneratorTemplateRepository->find($message->getTemplateId())) { throw new \RuntimeException('template not found: '.$message->getTemplateId()); } if (null === $destinationStoredObject = $this->storedObjectRepository->find($message->getDestinationStoredObjectId())) { throw new \RuntimeException('destination stored object not found : '.$message->getDestinationStoredObjectId()); } if ($destinationStoredObject->getGenerationTrialsCounter() >= self::AUTHORIZED_TRIALS) { $this->logger->error(self::LOG_PREFIX.'Request generation abandoned: maximum number of retry reached', [ 'template_id' => $message->getTemplateId(), 'destination_stored_object' => $message->getDestinationStoredObjectId(), 'trial' => $destinationStoredObject->getGenerationTrialsCounter(), ]); throw new UnrecoverableMessageHandlingException('maximum number of retry reached'); } $creator = $this->userRepository->find($message->getCreatorId()); // we increase the number of generation trial in the object, and, in the same time, update the counter // on the database side. This ensure that, if the script fails for any reason (memory limit reached), the // counter is inscreased $destinationStoredObject->addGenerationTrial(); $this->entityManager->createQuery('UPDATE '.StoredObject::class.' s SET s.generationTrialsCounter = s.generationTrialsCounter + 1 WHERE s.id = :id') ->setParameter('id', $destinationStoredObject->getId()) ->execute(); try { if ($message->isDumpOnly()) { $destinationStoredObject = $this->generator->generateDataDump( $template, $message->getEntityId(), $message->getContextGenerationData(), $destinationStoredObject, $creator ); $this->sendDataDump($destinationStoredObject, $message); } else { $this->generator->generateDocFromTemplate( $template, $message->getEntityId(), $message->getContextGenerationData(), $destinationStoredObject, $creator ); } } catch (StoredObjectManagerException|GeneratorException $e) { $this->entityManager->flush(); $this->logger->error(self::LOG_PREFIX.'Request generation failed', [ 'template_id' => $message->getTemplateId(), 'destination_stored_object' => $message->getDestinationStoredObjectId(), 'trial' => $destinationStoredObject->getGenerationTrialsCounter(), 'error' => $e->getTraceAsString(), ]); throw $e; } $this->entityManager->flush(); $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(), ]); } private function sendDataDump(StoredObject $destinationStoredObject, RequestGenerationMessage $message): void { // Get the content of the document $content = $this->storedObjectManager->read($destinationStoredObject); $filename = $destinationStoredObject->getFilename(); $contentType = $destinationStoredObject->getType(); // Create the email with the document as an attachment $email = (new TemplatedEmail()) ->to($message->getSendResultToEmail()) ->textTemplate('@ChillDocGenerator/Email/send_data_dump_to_admin.txt.twig') ->context([ 'filename' => $filename, ]) ->subject($this->translator->trans('docgen.data_dump_email.subject')) ->attach($content, $filename, $contentType); $this->mailer->send($email); } }