From 5b30f4ac74363b3247eaaea45f6cd2d62f883037 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sat, 10 Feb 2024 10:16:52 +0000 Subject: [PATCH] #257 - enables scheduler retry --- .vscode/launch.json | 10 ++++++++++ order-service/pom.xml | 8 ++++++++ .../example/orderservice/config/Initializer.java | 13 ++++++++++++- .../repositories/OrderRepository.java | 3 +++ .../orderservice/services/OrderService.java | 15 +++++++++++++++ order-service/src/main/resources/application.yml | 13 +++++++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index d879f47c..61c2753e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -90,6 +90,16 @@ "projectName": "catalog-service", "args": "", "envFile": "${workspaceFolder}/.env" + }, + { + "type": "java", + "name": "Spring Boot-TestOrderServiceApplication", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "com.example.orderservice.TestOrderServiceApplication", + "projectName": "order-service", + "args": "", + "envFile": "${workspaceFolder}/.env" } ] } \ No newline at end of file diff --git a/order-service/pom.xml b/order-service/pom.xml index e1b6b648..551a9968 100644 --- a/order-service/pom.xml +++ b/order-service/pom.xml @@ -112,6 +112,14 @@ spring-boot-starter-data-jpa + + + org.jobrunr + jobrunr-spring-boot-3-starter + 6.3.4 + + + diff --git a/order-service/src/main/java/com/example/orderservice/config/Initializer.java b/order-service/src/main/java/com/example/orderservice/config/Initializer.java index 788851be..2757a7fc 100644 --- a/order-service/src/main/java/com/example/orderservice/config/Initializer.java +++ b/order-service/src/main/java/com/example/orderservice/config/Initializer.java @@ -1,11 +1,14 @@ /***

- Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli. + Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli.

***/ package com.example.orderservice.config; +import com.example.orderservice.services.OrderService; +import org.jobrunr.scheduling.BackgroundJob; +import org.jobrunr.scheduling.cron.Cron; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; @@ -16,8 +19,16 @@ public class Initializer implements CommandLineRunner { private final Logger log = LoggerFactory.getLogger(this.getClass()); + private final OrderService orderService; + + public Initializer(OrderService orderService) { + this.orderService = orderService; + } + @Override public void run(String... args) { log.info("Running Initializer....."); + BackgroundJob.scheduleRecurrently(Cron.minutely(), orderService::retryNewOrders); + log.info("Completed Scheduling Recurrently BackgroundJob with 2 retries"); } } diff --git a/order-service/src/main/java/com/example/orderservice/repositories/OrderRepository.java b/order-service/src/main/java/com/example/orderservice/repositories/OrderRepository.java index a4aee019..5c33faaf 100644 --- a/order-service/src/main/java/com/example/orderservice/repositories/OrderRepository.java +++ b/order-service/src/main/java/com/example/orderservice/repositories/OrderRepository.java @@ -46,4 +46,7 @@ int updateOrderStatusAndSourceById( @Query("select o.id from Order o") Page findAllOrders(Pageable pageable); + + @EntityGraph(attributePaths = {"items"}) + List findByStatusOrderByIdAsc(OrderStatus new1); } diff --git a/order-service/src/main/java/com/example/orderservice/services/OrderService.java b/order-service/src/main/java/com/example/orderservice/services/OrderService.java index 66a32455..7397bb97 100644 --- a/order-service/src/main/java/com/example/orderservice/services/OrderService.java +++ b/order-service/src/main/java/com/example/orderservice/services/OrderService.java @@ -9,6 +9,7 @@ Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli. import com.example.common.dtos.OrderDto; import com.example.orderservice.config.logging.Loggable; import com.example.orderservice.entities.Order; +import com.example.orderservice.entities.OrderStatus; import com.example.orderservice.exception.ProductNotFoundException; import com.example.orderservice.mapper.OrderMapper; import com.example.orderservice.model.request.OrderItemRequest; @@ -20,6 +21,7 @@ Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli. import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import org.jobrunr.jobs.annotations.Job; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; @@ -154,4 +156,17 @@ private PagedResult getOrderResponsePagedResult(Page page) page.hasNext(), page.hasPrevious()); } + + @Job(name = "reProcessNewOrders", retries = 2) + public void retryNewOrders() { + // fetch all orders where Status is New in Order + List byStatusOrderByIdAsc = + orderRepository.findByStatusOrderByIdAsc(OrderStatus.NEW); + byStatusOrderByIdAsc.forEach( + order -> { + OrderDto persistedOrderDto = this.orderMapper.toDto(order); + log.info("Retrying Order :{}", persistedOrderDto); + kafkaOrderProducer.sendOrder(persistedOrderDto); + }); + } } diff --git a/order-service/src/main/resources/application.yml b/order-service/src/main/resources/application.yml index e25f08d1..3f87670e 100644 --- a/order-service/src/main/resources/application.yml +++ b/order-service/src/main/resources/application.yml @@ -41,3 +41,16 @@ spring: virtual: enabled: true # state-dir: /tmp/kafka-streams/ + +### the background-job-server and dashboard are disabled by default +org: + jobrunr: + job-scheduler: + enabled: true + background-job-server: + enabled: true + dashboard: + enabled: true + port: ${server.port} + miscellaneous: + allow-anonymous-data-usage: false