Skip to content

Commit

Permalink
#257 - enables scheduler retry
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Feb 10, 2024
1 parent 612ecf2 commit 5b30f4a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@
"projectName": "catalog-service",
"args": "",
"envFile": "${workspaceFolder}/.env"
},
{
"type": "java",
"name": "Spring Boot-TestOrderServiceApplication<order-service>",
"request": "launch",
"cwd": "${workspaceFolder}",
"mainClass": "com.example.orderservice.TestOrderServiceApplication",
"projectName": "order-service",
"args": "",
"envFile": "${workspaceFolder}/.env"
}
]
}
8 changes: 8 additions & 0 deletions order-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- Scheduling -->
<dependency>
<groupId>org.jobrunr</groupId>
<artifactId>jobrunr-spring-boot-3-starter</artifactId>
<version>6.3.4</version>
</dependency>


<!-- Observability -->
<!-- Metrics -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/***
<p>
Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli.
Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli.
</p>
***/

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;
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ int updateOrderStatusAndSourceById(

@Query("select o.id from Order o")
Page<Long> findAllOrders(Pageable pageable);

@EntityGraph(attributePaths = {"items"})
List<Order> findByStatusOrderByIdAsc(OrderStatus new1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -154,4 +156,17 @@ private PagedResult<OrderResponse> getOrderResponsePagedResult(Page<Long> page)
page.hasNext(),
page.hasPrevious());
}

@Job(name = "reProcessNewOrders", retries = 2)
public void retryNewOrders() {
// fetch all orders where Status is New in Order
List<Order> byStatusOrderByIdAsc =
orderRepository.findByStatusOrderByIdAsc(OrderStatus.NEW);
byStatusOrderByIdAsc.forEach(
order -> {
OrderDto persistedOrderDto = this.orderMapper.toDto(order);
log.info("Retrying Order :{}", persistedOrderDto);
kafkaOrderProducer.sendOrder(persistedOrderDto);
});
}
}
13 changes: 13 additions & 0 deletions order-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5b30f4a

Please sign in to comment.