Skip to content

Commit 19abaa2

Browse files
committed
tidyup
1 parent 52b9503 commit 19abaa2

File tree

4 files changed

+26
-52
lines changed

4 files changed

+26
-52
lines changed

task-manager-service/src/main/java/uk/gov/hmcts/cp/taskmanager/domain/executor/TaskExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private void executeTask(final ExecutableTask task, final ExecutionInfo executio
240240
*/
241241
private boolean canRetry(final ExecutableTask task, final ExecutionInfo taskResponse) {
242242
final boolean shouldRetryTask = taskResponse.isShouldRetry();
243-
final Integer retryAttemptsRemaining = job.getRetryAttemptsRemaining();
243+
final int retryAttemptsRemaining = job.getRetryAttemptsRemaining();
244244
final boolean taskHasRetryDurationsConfigured = task.getRetryDurationsInSecs().isPresent();
245245

246246
logger.info("Checking if task is retryable, jobID:{}, executionInfo.shouldRetry:{}, retryAttemptsRemaining:{}, has task configured with retryDurationsInSecs:{}",

task-manager-service/src/main/java/uk/gov/hmcts/cp/taskmanager/persistence/repository/JobsRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public interface JobsRepository extends JpaRepository<Job, UUID> {
8181

8282
@Modifying
8383
@Query(value = """
84-
WITH cte AS (
84+
WITH uaj AS (
8585
SELECT job_id
8686
FROM jobs
8787
WHERE worker_id IS NULL
@@ -94,8 +94,8 @@ WITH cte AS (
9494
UPDATE jobs j
9595
SET worker_id = :workerId,
9696
worker_lock_time = :lockTime
97-
FROM cte
98-
WHERE j.job_id = cte.job_id
97+
FROM uaj
98+
WHERE j.job_id = uaj.job_id
9999
RETURNING j.*
100100
""", nativeQuery = true)
101101
List<Job> assignJobsToWorkerBatch(@Param("workerId") UUID workerId,

task-manager-service/src/main/java/uk/gov/hmcts/cp/taskmanager/persistence/service/JobService.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,11 @@ public JobService(JobsRepository jobsRepository, JsonObjectConverter jsonObjectC
7070
this.jsonObjectConverter = jsonObjectConverter;
7171
}
7272

73-
/**
74-
* Retrieves all unassigned jobs that are ready to be executed.
75-
*
76-
* <p>This method finds jobs that:
77-
* <ul>
78-
* <li>Have no worker assigned ({@code workerId} is null)</li>
79-
* <li>Have reached their scheduled start time</li>
80-
* </ul>
81-
*
82-
* <p>Jobs are ordered by priority (ascending, 1 is highest) and then by start time (ascending).
83-
* Uses pessimistic write locking to prevent concurrent access.
84-
*
85-
* @return a list of unassigned jobs ready for execution, ordered by priority and start time
86-
*/
87-
@Transactional
88-
public List<Job> getUnassignedJobs() {
89-
return jobsRepository.findUnassignedJobs(ZonedDateTime.now());
90-
}
91-
9273
/**
9374
* Retrieves unassigned jobs with a limit on the number of results.
9475
*
95-
* <p>This method is similar to {@link #getUnassignedJobs()} but limits the number
96-
* of results returned. This is useful for batch processing to avoid loading too
97-
* many jobs at once and improve performance.
76+
* <p>This method limits the number of results returned. This is useful for batch processing to
77+
* avoid loading too many jobs at once and improve performance.
9878
*
9979
* <p>Jobs are ordered by priority (ascending) and then by start time (ascending).
10080
* Uses pessimistic write locking to prevent concurrent access.
@@ -137,8 +117,7 @@ public void decrementRetryAttempts(UUID jobId) {
137117
/**
138118
* Inserts a new job into the database.
139119
*
140-
* <p>This method persists a new job entity. The jobId will be automatically
141-
* generated if not set (via {@link Job#onCreate()}).
120+
* <p>This method persists a new job entity.
142121
*
143122
* @param job the job to insert, must not be null
144123
*/
@@ -158,7 +137,7 @@ public void insertJob(Job job) {
158137
*/
159138
@Transactional
160139
public void updateJobTaskData(final UUID jobId, final JsonObject data) {
161-
String jobDataString = jsonObjectConverter.convertToDatabaseColumn(data);
140+
final String jobDataString = jsonObjectConverter.convertToDatabaseColumn(data);
162141
jobsRepository.updateJobData(jobDataString, jobId);
163142
}
164143

task-manager-service/src/test/java/uk/gov/hmcts/cp/taskmanager/persistence/service/JobServiceTest.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
package uk.gov.hmcts.cp.taskmanager.persistence.service;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.ArgumentMatchers.eq;
8+
import static org.mockito.Mockito.never;
9+
import static org.mockito.Mockito.verify;
10+
import static org.mockito.Mockito.when;
11+
312
import uk.gov.hmcts.cp.taskmanager.domain.converter.JsonObjectConverter;
413
import uk.gov.hmcts.cp.taskmanager.persistence.entity.Job;
514
import uk.gov.hmcts.cp.taskmanager.persistence.repository.JobsRepository;
15+
16+
import java.time.ZonedDateTime;
17+
import java.util.Arrays;
18+
import java.util.List;
19+
import java.util.Optional;
20+
import java.util.UUID;
21+
622
import jakarta.json.Json;
723
import jakarta.json.JsonObject;
824
import org.junit.jupiter.api.BeforeEach;
@@ -14,13 +30,6 @@
1430
import org.springframework.data.domain.PageRequest;
1531
import org.springframework.data.domain.Pageable;
1632

17-
import java.time.ZonedDateTime;
18-
import java.util.*;
19-
20-
import static org.junit.jupiter.api.Assertions.*;
21-
import static org.mockito.ArgumentMatchers.*;
22-
import static org.mockito.Mockito.*;
23-
2433
@ExtendWith(MockitoExtension.class)
2534
class JobServiceTest {
2635

@@ -46,24 +55,10 @@ void setUp() {
4655
.add("key", "value")
4756
.build();
4857

49-
testJob = new Job(testJobId, testJobData, "TEST_TASK",
58+
testJob = new Job(testJobId, testJobData, "TEST_TASK",
5059
ZonedDateTime.now(), null, null, 5, 10);
5160
}
5261

53-
@Test
54-
void testGetUnassignedJobs() {
55-
List<Job> expectedJobs = Arrays.asList(testJob);
56-
when(jobsRepository.findUnassignedJobs(any(ZonedDateTime.class)))
57-
.thenReturn(expectedJobs);
58-
59-
List<Job> result = jobService.getUnassignedJobs();
60-
61-
assertNotNull(result);
62-
assertEquals(1, result.size());
63-
assertEquals(testJob, result.get(0));
64-
verify(jobsRepository).findUnassignedJobs(any(ZonedDateTime.class));
65-
}
66-
6762
@Test
6863
void testGetUnassignedJobsWithBatchSize() {
6964
int batchSize = 10;
@@ -108,7 +103,7 @@ void testDecrementRetryAttemptsWhenZero() {
108103
void testDecrementRetryAttemptsJobNotFound() {
109104
when(jobsRepository.findByJobId(testJobId)).thenReturn(Optional.empty());
110105

111-
assertThrows(RuntimeException.class,
106+
assertThrows(RuntimeException.class,
112107
() -> jobService.decrementRetryAttempts(testJobId));
113108
}
114109

0 commit comments

Comments
 (0)