Skip to content

Commit

Permalink
fix: workaround failed pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien committed Dec 20, 2023
1 parent 1c5a20b commit a8aba27
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public String bo(@ModelAttribute("numberOfDocumentsToProcess") ResultDTO numberO
model.addAttribute("numberOfTenantsToProcess", tenantService.countTenantsWithStatusInToProcess());
long result = 0;
if (numberOfDocumentsToProcess.getId() == null) {
result = tenantService.getTotalOfTenantsWithFailedGeneratedPdfDocument();
result = tenantService.getCountOfTenantsWithFailedGeneratedPdfDocument();
}
model.addAttribute("TenantsWithFailedGeneratedPdf", result);
model.addAttribute("isUserAdmin", is_admin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public interface DocumentRepository extends JpaRepository<Document, Long> {
@Query(value = """
SELECT d.id
FROM Document d
WHERE d.documentStatus = 'TO_PROCESS'
AND (d.lastModifiedDate IS NULL OR d.lastModifiedDate < :toDateTime)
AND d.watermarkFile IS NULL
""")
Page<Long> findToProcessWithoutPDFToDate(LocalDateTime toDateTime, Pageable pageable);
WHERE d.document_status = 'TO_PROCESS'
AND (d.last_modified_date IS NULL OR d.last_modified_date < :to)
AND d.watermark_file_id IS NULL
""", nativeQuery = true)
List<Long> findToProcessWithoutPDFToDate(@Param("to") LocalDateTime toDateTime);

@Modifying
@Query("UPDATE Document d SET d.documentDeniedReasons = :documentDeniedReasons where d.id = :documentId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,19 @@ public void initializeFieldsToProcessPdfGeneration(Document document) {
@Transactional
public void regenerateFailedPdfDocumentsUsingButtonRequest() {
synchronized (this) {
int numberOfUpdate = 1;
int lengthOfPage = 1000;
Pageable page = PageRequest.of(0, lengthOfPage, Sort.Direction.DESC, "id");
LocalDateTime thirtyMinutesAgo = LocalDateTime.now().minusMinutes(30);

Page<Long> documents;
do {
documents = documentRepository.findToProcessWithoutPDFToDate(thirtyMinutesAgo, page);
long totalElements = documents.getTotalElements();
log.info("Treat PDF Failed :" + lengthOfPage + " on " + totalElements + " elements");

documents.forEach(documentId -> producer.generatePdf(documentId));
log.info("Send number [" + numberOfUpdate++ + "] with " + documents.getNumberOfElements() + " documentId");
LocalDateTime oneHourAgo = LocalDateTime.now().minusHours(1);
List<Long> documents = documentRepository.findToProcessWithoutPDFToDate(oneHourAgo);
log.info("Regenerate [{}] Failed PDF in TO PROCESS status", documents.size());

documents.forEach(documentId -> {
try {
this.wait(30000);
producer.generatePdf(documentId);
Thread.sleep(10);
} catch (InterruptedException e) {
log.error("some exception message ", e);
Thread.currentThread().interrupt();
log.error("Something wrong on sleep !!! ");
}
} while (!documents.isEmpty());
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,12 @@ private void updatePageOfDocumentsWithNullCreationDateTime(Page<Document> docume
});
}

public long getTotalOfTenantsWithFailedGeneratedPdfDocument() {
return tenantRepository.countAllTenantsWithFailedGeneratedPdfDocument();
public long getCountOfTenantsWithFailedGeneratedPdfDocument() {
return tenantRepository.countAllTenantsWithoutPdfDocument();
}

public Page<Tenant> getAllTenantsToProcessWithFailedGeneratedPdfDocument(Pageable pageable) {
return new PageImpl<>(tenantRepository.findAllTenantsToProcessWithFailedGeneratedPdfDocument(pageable).toList());
return new PageImpl<>(tenantRepository.findAllTenantsToProcessWithoutPdfDocument(pageable).toList());
}

public long countTenantsWithStatusInToProcess() {
Expand Down
4 changes: 2 additions & 2 deletions dossierfacile-bo/src/main/resources/templates/bo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@
<div style="background: bisque; padding: 10px; font-size: 16px; margin-top: 15px;">
<a th:href="@{/bo/documentFailedList}" style="text-decoration: none;">
<span>
<span style="color: red; font-weight: bold;">PDFs en erreur :</span>
<span style="color: red; font-weight: bold;">Locataires avec erreur PDF :</span>
<span th:text="${TenantsWithFailedGeneratedPdf}"></span>
</span>
</a>
</div>
<div th:if="${TenantsWithFailedGeneratedPdf != 0}">
<form th:action="@{/bo/regeneratePdfDocument}" th:method="post">
<button style="border-radius: 5px; width: 100%" class="btn btn-danger bo-btn" name="action" type="submit">
Relancer la génération
Regénération PDF (TO_PROCESS)
</button>
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ SELECT COUNT(DISTINCT t.id)
LEFT JOIN document d ON d.tenant_id = t.id OR d.guarantor_id = t.id
WHERE d.watermark_file_id IS NULL
AND d.document_status IS NOT NULL
AND d.last_modified_date IS NOT NULL
AND d.last_modified_date < NOW() - INTERVAL '12' HOUR
AND ( d.last_modified_date IS NULL OR d.last_modified_date < NOW() - INTERVAL '1' HOUR)
""", nativeQuery = true)
long countAllTenantsWithFailedGeneratedPdfDocument();
long countAllTenantsWithoutPdfDocument();

@Query(value = """
SELECT *
Expand All @@ -122,20 +121,19 @@ WHERE t.id IN (
JOIN document d ON d.tenant_id = t2.id
WHERE d.watermark_file_id IS NULL
AND d.document_status IS NOT NULL
AND d.last_modified_date IS NOT NULL
AND d.last_modified_date < NOW() - INTERVAL '12' HOUR
AND ( d.last_modified_date IS NULL OR d.last_modified_date < NOW() - INTERVAL '1' HOUR)
UNION DISTINCT
SELECT t3.id
FROM tenant t3
JOIN guarantor g ON g.tenant_id = t3.id
JOIN document d ON d.guarantor_id = g.id
WHERE d.watermark_file_id IS NULL
AND d.document_status IS NOT NULL
AND d.lastModified < NOW() - INTERVAL '12' HOUR
AND ( d.last_modified_date IS NULL OR d.last_modified_date < NOW() - INTERVAL '1' HOUR)
)
ORDER BY t.last_update_date DESC
""", nativeQuery = true)
Page<Tenant> findAllTenantsToProcessWithFailedGeneratedPdfDocument(Pageable pageable);
Page<Tenant> findAllTenantsToProcessWithoutPdfDocument(Pageable pageable);

long countAllByStatus(TenantFileStatus tenantFileStatus);
//endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public boolean documentIsUpToDateAt(Long timestamp, Long documentId) {
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
Document document = documentRepository.findById(documentId).orElse(null);

return document != null && dateTime.isAfter(document.getLastModifiedDate());
return document != null && ( document.getLastModifiedDate() == null || dateTime.isAfter(document.getLastModifiedDate()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean documentIsUpToDateAt(Long timestamp, Long documentId) {
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
Document document = documentRepository.findById(documentId).orElse(null);

return document != null && dateTime.isAfter(document.getLastModifiedDate());
return document != null && ( document.getLastModifiedDate() == null || dateTime.isAfter(document.getLastModifiedDate()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public interface DocumentRepository extends JpaRepository<Document, Long> {
SELECT *
FROM document
WHERE document_status = 'TO_PROCESS'
AND (last_modified_date IS NULL OR last_modified_date < :to)
AND watermark_file_id IS NULL
LIMIT 100;
AND (last_modified_date IS NULL OR last_modified_date < :to)
ORDER BY
CASE WHEN last_modified_date IS NULL THEN 1 ELSE 0 END,
last_modified_date DESC
LIMIT 200;
""", nativeQuery = true)
List<Document> findToProcessWithoutPDFToDate(@Param("to") LocalDateTime toDateTime);
}

0 comments on commit a8aba27

Please sign in to comment.