diff --git a/dossierfacile-bo/src/main/java/fr/gouv/bo/controller/BOController.java b/dossierfacile-bo/src/main/java/fr/gouv/bo/controller/BOController.java index 692c91971..5ab7a1666 100644 --- a/dossierfacile-bo/src/main/java/fr/gouv/bo/controller/BOController.java +++ b/dossierfacile-bo/src/main/java/fr/gouv/bo/controller/BOController.java @@ -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); diff --git a/dossierfacile-bo/src/main/java/fr/gouv/bo/repository/DocumentRepository.java b/dossierfacile-bo/src/main/java/fr/gouv/bo/repository/DocumentRepository.java index a26e90c6e..0c2e8d0df 100644 --- a/dossierfacile-bo/src/main/java/fr/gouv/bo/repository/DocumentRepository.java +++ b/dossierfacile-bo/src/main/java/fr/gouv/bo/repository/DocumentRepository.java @@ -21,11 +21,11 @@ public interface DocumentRepository extends JpaRepository { @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 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 findToProcessWithoutPDFToDate(@Param("to") LocalDateTime toDateTime); @Modifying @Query("UPDATE Document d SET d.documentDeniedReasons = :documentDeniedReasons where d.id = :documentId") diff --git a/dossierfacile-bo/src/main/java/fr/gouv/bo/service/DocumentService.java b/dossierfacile-bo/src/main/java/fr/gouv/bo/service/DocumentService.java index 194a4784a..3fa4edac7 100644 --- a/dossierfacile-bo/src/main/java/fr/gouv/bo/service/DocumentService.java +++ b/dossierfacile-bo/src/main/java/fr/gouv/bo/service/DocumentService.java @@ -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 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 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()); + }); } } diff --git a/dossierfacile-bo/src/main/java/fr/gouv/bo/service/TenantService.java b/dossierfacile-bo/src/main/java/fr/gouv/bo/service/TenantService.java index 37cdf9f0a..323e2c920 100644 --- a/dossierfacile-bo/src/main/java/fr/gouv/bo/service/TenantService.java +++ b/dossierfacile-bo/src/main/java/fr/gouv/bo/service/TenantService.java @@ -763,12 +763,12 @@ private void updatePageOfDocumentsWithNullCreationDateTime(Page docume }); } - public long getTotalOfTenantsWithFailedGeneratedPdfDocument() { - return tenantRepository.countAllTenantsWithFailedGeneratedPdfDocument(); + public long getCountOfTenantsWithFailedGeneratedPdfDocument() { + return tenantRepository.countAllTenantsWithoutPdfDocument(); } public Page getAllTenantsToProcessWithFailedGeneratedPdfDocument(Pageable pageable) { - return new PageImpl<>(tenantRepository.findAllTenantsToProcessWithFailedGeneratedPdfDocument(pageable).toList()); + return new PageImpl<>(tenantRepository.findAllTenantsToProcessWithoutPdfDocument(pageable).toList()); } public long countTenantsWithStatusInToProcess() { diff --git a/dossierfacile-bo/src/main/resources/templates/bo/index.html b/dossierfacile-bo/src/main/resources/templates/bo/index.html index eb97560d6..5b1e04698 100644 --- a/dossierfacile-bo/src/main/resources/templates/bo/index.html +++ b/dossierfacile-bo/src/main/resources/templates/bo/index.html @@ -72,7 +72,7 @@
- PDFs en erreur : + Locataires avec erreur PDF : @@ -80,7 +80,7 @@
diff --git a/dossierfacile-common-library/src/main/java/fr/dossierfacile/common/repository/TenantCommonRepository.java b/dossierfacile-common-library/src/main/java/fr/dossierfacile/common/repository/TenantCommonRepository.java index 1a8b87741..8a40d6035 100644 --- a/dossierfacile-common-library/src/main/java/fr/dossierfacile/common/repository/TenantCommonRepository.java +++ b/dossierfacile-common-library/src/main/java/fr/dossierfacile/common/repository/TenantCommonRepository.java @@ -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 * @@ -122,8 +121,7 @@ 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 @@ -131,11 +129,11 @@ AND d.last_modified_date < NOW() - INTERVAL '12' HOUR 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 findAllTenantsToProcessWithFailedGeneratedPdfDocument(Pageable pageable); + Page findAllTenantsToProcessWithoutPdfDocument(Pageable pageable); long countAllByStatus(TenantFileStatus tenantFileStatus); //endregion diff --git a/dossierfacile-pdf-generator/src/main/java/fr/dossierfacile/api/pdfgenerator/service/DocumentServiceImpl.java b/dossierfacile-pdf-generator/src/main/java/fr/dossierfacile/api/pdfgenerator/service/DocumentServiceImpl.java index d560fbc53..ea349b5b7 100644 --- a/dossierfacile-pdf-generator/src/main/java/fr/dossierfacile/api/pdfgenerator/service/DocumentServiceImpl.java +++ b/dossierfacile-pdf-generator/src/main/java/fr/dossierfacile/api/pdfgenerator/service/DocumentServiceImpl.java @@ -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 diff --git a/dossierfacile-process-file/src/main/java/fr/dossierfacile/process/file/service/DocumentServiceImpl.java b/dossierfacile-process-file/src/main/java/fr/dossierfacile/process/file/service/DocumentServiceImpl.java index 9cc54409d..067eeb580 100644 --- a/dossierfacile-process-file/src/main/java/fr/dossierfacile/process/file/service/DocumentServiceImpl.java +++ b/dossierfacile-process-file/src/main/java/fr/dossierfacile/process/file/service/DocumentServiceImpl.java @@ -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 diff --git a/dossierfacile-task-scheduler/src/main/java/fr/dossierfacile/scheduler/tasks/documenttoprocess/DocumentRepository.java b/dossierfacile-task-scheduler/src/main/java/fr/dossierfacile/scheduler/tasks/documenttoprocess/DocumentRepository.java index 20ae354f7..76e24ca63 100644 --- a/dossierfacile-task-scheduler/src/main/java/fr/dossierfacile/scheduler/tasks/documenttoprocess/DocumentRepository.java +++ b/dossierfacile-task-scheduler/src/main/java/fr/dossierfacile/scheduler/tasks/documenttoprocess/DocumentRepository.java @@ -15,9 +15,12 @@ public interface DocumentRepository extends JpaRepository { 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 findToProcessWithoutPDFToDate(@Param("to") LocalDateTime toDateTime); }