Skip to content

Commit

Permalink
Po 210 process pending jobs (#434)
Browse files Browse the repository at this point in the history
* add methods for printing all pending

* pending jobs code without pagination

* pending jobs code with pagination

* process pending with save via sftp

* fix tests

* async changes

* added postman json

* choose tranaction point

* async processing changes

* unit tests and retry fix

* use saxon for xlst 2 compatibility

* define default transaction manager with primary

* define default transaction manager with primary

---------

Co-authored-by: Sabah u Din Irfan <sabah.irfan@gmail.com>
Co-authored-by: Cade Faulkner <147704481+CadeFaulkner@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 1, 2024
1 parent 37bcbbd commit e5aa7e8
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 21 deletions.
103 changes: 103 additions & 0 deletions postman/Opal Print.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"info": {
"_postman_id": "47e7c899-66b9-485a-bf81-13002887dbc6",
"name": "Opal Print Service",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "1068308"
},
"item": [
{
"name": "generate-pdf",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>John Doe</name><accountNumber>123456789</accountNumber><balance>501.55</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n}\n"
},
"url": {
"raw": "http://localhost:4550/api/print/generate-pdf",
"protocol": "http",
"host": [
"localhost"
],
"port": "4550",
"path": [
"api",
"print",
"generate-pdf"
]
}
},
"response": []
},
{
"name": "enqueue-print-jobs",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "[\n {\n \"xmlData\": \"This one should fail\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Bob Brown</name><accountNumber>112233445</accountNumber><balance>250.00</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Alice Green</name><accountNumber>556677889</accountNumber><balance>799.99</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Charlie Black</name><accountNumber>223344556</accountNumber><balance>1240.50</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Emma White</name><accountNumber>667788990</accountNumber><balance>645.25</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Liam Grey</name><accountNumber>334455667</accountNumber><balance>320.75</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Olivia Brown</name><accountNumber>445566778</accountNumber><balance>510.65</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Noah Blue</name><accountNumber>223344556</accountNumber><balance>985.20</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Ava Red</name><accountNumber>778899001</accountNumber><balance>450.10</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>William Green</name><accountNumber>112233445</accountNumber><balance>702.95</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Sophia Violet</name><accountNumber>667788990</accountNumber><balance>250.50</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>James Yellow</name><accountNumber>334455667</accountNumber><balance>890.30</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Isabella Orange</name><accountNumber>445566778</accountNumber><balance>395.75</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Benjamin Pink</name><accountNumber>223344556</accountNumber><balance>980.00</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Mia Purple</name><accountNumber>778899001</accountNumber><balance>215.80</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Lucas Grey</name><accountNumber>112233445</accountNumber><balance>467.35</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Amelia White</name><accountNumber>667788990</accountNumber><balance>730.15</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Henry Black</name><accountNumber>334455667</accountNumber><balance>1005.25</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n },\n {\n \"xmlData\": \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?><fine><name>Emily Brown</name><accountNumber>445566778</accountNumber><balance>589.45</balance></fine>\",\n \"docType\": \"TEST_PDF_definition_id\",\n \"docVersion\": \"test_version_1\"\n }\n]\n\n\n"
},
"url": {
"raw": "http://localhost:4550/api/print/enqueue-print-jobs",
"protocol": "http",
"host": [
"localhost"
],
"port": "4550",
"path": [
"api",
"print",
"enqueue-print-jobs"
]
}
},
"response": []
},
{
"name": "process-pending-jobs",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"info\": {\n \"general\": {\n \"version\": \"00_1\",\n \"docref\": \"AAA\"\n }\n },\n \"data\": {\n \"job\": {\n \"division\": \"MinimalDivision\",\n \"accountnumber\": \"000001\",\n \"casenumber\": \"CASE-00001\",\n \"dob\": \"1990-01-01\",\n \"defendantname\": \"John Doe\",\n \"sex\": \"Male\",\n \"amountoutstanding\": \"£100.00\",\n \"defendantindefault\": \"No\",\n \"dateproduced\": \"2024-04-09\",\n \"dateoforder\": \"2024-03-01\",\n \"defendantaddress\": {\n \"street\": \"123 Minimal St\",\n \"city\": \"Minimal City\",\n \"postalCode\": \"M1234\"\n },\n \"jobcentreaddress\": {\n \"name\": \"Minimal Job Centre\",\n \"address\": {\n \"street\": \"456 Minimal St\",\n \"city\": \"Job Centre City\",\n \"postalCode\": \"JC123\"\n }\n }\n }\n }\n}\n"
},
"url": {
"raw": "http://localhost:4550/api/print/process-pending-jobs",
"protocol": "http",
"host": [
"localhost"
],
"port": "4550",
"path": [
"api",
"print",
"process-pending-jobs"
]
}
},
"response": []
}
]
}
10 changes: 10 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/config/AsyncConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.hmcts.opal.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class AsyncConfig {

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public TransactionAwareDataSourceProxy transactionAwareDataSourceProxy(
}

@Bean
@Primary
public PlatformTransactionManager transactionManager(
TransactionAwareDataSourceProxy transactionAwareDataSourceProxy
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.gov.hmcts.opal.config;

import jakarta.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.JpaTransactionManager;

@Configuration
public class PrintTransactionManagerConfig {

@Bean(name = "printTransactionManager")
public JpaTransactionManager printTransactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.opal.entity.print.PrintJob;
import uk.gov.hmcts.opal.service.print.AsyncPrintJobProcessor;
import uk.gov.hmcts.opal.service.print.PrintService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ContentDisposition;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

Expand All @@ -28,6 +30,9 @@ public class PrintRequestController {

private final PrintService printService;

private final AsyncPrintJobProcessor asyncPrintJobProcessor;


@PostMapping(value = "/enqueue-print-jobs", consumes = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Enqueues print jobs for a batch of documents")
public ResponseEntity<String> enqueuePrintJobs(@RequestBody List<PrintJob> printJobs) {
Expand All @@ -52,5 +57,15 @@ public ResponseEntity<byte[]> generatePdf(@RequestBody PrintJob printJob) {
return ResponseEntity.ok().headers(headers).body(response);
}

@PostMapping(value = "/process-pending-jobs")
@Operation(summary = "Processes pending print jobs")
public ResponseEntity<String> processPendingJobs() {
log.info(":POST:processPendingJobs: processing pending print jobs");

asyncPrintJobProcessor.processPendingJobsAsync(LocalDateTime.now());

return ResponseEntity.ok().body("OK");
}

}

3 changes: 1 addition & 2 deletions src/main/java/uk/gov/hmcts/opal/entity/print/PrintJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -41,7 +40,7 @@ public class PrintJob {
@Column(name = "job_uuid", nullable = false)
private UUID jobId;

@Lob

@Column(name = "xml_data", nullable = false)
private String xmlData;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package uk.gov.hmcts.opal.repository.print;

import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import uk.gov.hmcts.opal.entity.print.PrintDefinition;


@Repository
public interface PrintDefinitionRepository extends JpaRepository<PrintDefinition, Long> {

@Transactional
PrintDefinition findByDocTypeAndTemplateId(String docType, String templateId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
package uk.gov.hmcts.opal.repository.print;

import jakarta.persistence.LockModeType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import uk.gov.hmcts.opal.entity.print.PrintJob;
import uk.gov.hmcts.opal.entity.print.PrintStatus;

import java.time.LocalDateTime;

@Repository
public interface PrintJobRepository extends JpaRepository<PrintJob, Long> {

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT p FROM PrintJob p WHERE p.status = :status AND p.createdAt <= :cutoffDate")
public Page<PrintJob> findPendingJobsForUpdate(@Param("status") PrintStatus status,
@Param("cutoffDate") LocalDateTime cutoffDate,
Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.gov.hmcts.opal.service.print;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AsyncPrintJobProcessor {

private final PrintService printService;

@Autowired
public AsyncPrintJobProcessor(PrintService printService) {
this.printService = printService;
}

@Async
public void processPendingJobsAsync(LocalDateTime cutoffDate) {
printService.processPendingJobs(cutoffDate);
}

}
Loading

0 comments on commit e5aa7e8

Please sign in to comment.