Skip to content

Commit 2fa32a8

Browse files
authored
DMP-5222 Automated Tasks Should Log Dynatrace Failure on InterruptedException (#3045)
1 parent 95e10c1 commit 2fa32a8

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/main/java/uk/gov/hmcts/darts/task/runner/impl/AbstractLockableAutomatedTask.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import static uk.gov.hmcts.darts.task.status.AutomatedTaskStatus.NOT_STARTED;
4646
import static uk.gov.hmcts.darts.task.status.AutomatedTaskStatus.SKIPPED;
4747

48+
@SuppressWarnings("PMD.TooManyMethods")
4849
@Slf4j
4950
public abstract class AbstractLockableAutomatedTask<T extends AbstractAutomatedTaskConfig> implements AutomatedTask, AutoloadingAutomatedTask {
5051

@@ -128,14 +129,22 @@ public void run(boolean isManualRun) {
128129
}
129130
}
130131
} catch (Exception exception) {
131-
logApi.taskFailed(executionId.get(), getTaskName());
132-
setAutomatedTaskStatus(FAILED);
133132
log.error("Task: {} exception while attempting to start the task", getTaskName(), exception);
133+
logTaskFailed(executionId);
134134
} finally {
135135
postRunTask();
136136
}
137137
}
138138

139+
private void logTaskFailed(ThreadLocal<UUID> executionId) {
140+
UUID executionUuid = null;
141+
if (executionId != null && executionId.get() != null) {
142+
executionUuid = executionId.get();
143+
}
144+
logApi.taskFailed(executionUuid, getTaskName());
145+
setAutomatedTaskStatus(FAILED);
146+
}
147+
139148
LockedTask createLockableTask() {
140149
return new LockedTask();
141150
}
@@ -287,17 +296,17 @@ public void run() {
287296
try {
288297
future.get(getLockAtMostFor().toMillis(), TimeUnit.MILLISECONDS);
289298
} catch (TimeoutException e) {
290-
setAutomatedTaskStatus(FAILED);
291299
log.error("Task: {} timed out after {}ms", getTaskName(), getLockAtMostFor().toMillis());
300+
logTaskFailed(executionId);
292301
if (!future.cancel(true)) {
293302
log.error("Failed to cancel task: {}.", getTaskName());
294303
}
295304
} catch (ExecutionException e) {
296-
setAutomatedTaskStatus(FAILED);
297305
log.error("Task: {} execution exception", getTaskName(), e);
306+
logTaskFailed(executionId);
298307
} catch (InterruptedException e) {
299-
setAutomatedTaskStatus(FAILED);
300308
log.error("Task: {} interrupted", getTaskName(), e);
309+
logTaskFailed(executionId);
301310
Thread.currentThread().interrupt();
302311
}
303312
executor.shutdown();

src/test/java/uk/gov/hmcts/darts/task/runner/impl/AbstractLockableAutomatedTaskTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,14 @@ public class AbstractLockableAutomatedTaskTest {
5151
@Mock
5252
private LockService lockService;
5353

54-
5554
@Nested
5655
@ExtendWith(OutputCaptureExtension.class)
5756
@SuppressWarnings("PMD.SystemPrintln") // System.out.println is used to ensure runnable is executed
5857
@Isolated
5958
class LockedTaskTest {
6059

61-
6260
@Test
63-
void lockedTaskRun_shouldBeSuccessfull_whenTaskCompletesWithinLockAtMostFor(CapturedOutput output) {
61+
void lockedTaskRun_shouldBeSuccessful_whenTaskCompletesWithinLockAtMostFor(CapturedOutput output) {
6462
Runnable task = () -> {
6563
System.out.println("LockedTaskTest: Task is running");
6664
};
@@ -114,7 +112,6 @@ void lockedTaskRun_shouldFail_whenTaskDoesNotCompletesWithinLockAtMostFor(Captur
114112

115113
doNothing().when(lockedTask).assertLocked();
116114

117-
118115
lockedTask.run();
119116

120117
//Ensure task is run
@@ -125,6 +122,7 @@ void lockedTaskRun_shouldFail_whenTaskDoesNotCompletesWithinLockAtMostFor(Captur
125122
"Timeout waiting for task status to be FAILED", 10);
126123
verify(abstractLockableAutomatedTask, atLeastOnce()).setAutomatedTaskStatus(AutomatedTaskStatus.FAILED);
127124
verify(lockedTask).assertLocked();
125+
verify(logApi).taskFailed(any(), any());
128126
}
129127

130128
@Test
@@ -145,7 +143,6 @@ void lockedTaskRun_shouldFail_whenTaskHasException(CapturedOutput output) {
145143

146144
doNothing().when(lockedTask).assertLocked();
147145

148-
149146
lockedTask.run();
150147

151148
//Ensure task is run
@@ -158,6 +155,7 @@ void lockedTaskRun_shouldFail_whenTaskHasException(CapturedOutput output) {
158155
verify(abstractLockableAutomatedTask, atLeastOnce()).setAutomatedTaskStatus(AutomatedTaskStatus.FAILED);
159156
verify(abstractLockableAutomatedTask).handleException(exception);
160157
verify(lockedTask).assertLocked();
158+
verify(logApi).taskFailed(any(), any());
161159
}
162160

163161
@Test
@@ -182,7 +180,6 @@ void lockedTaskRun_shouldFail_whenTaskInterupted(CapturedOutput output) throws E
182180
lock.setAtMostFor(Duration.ofSeconds(10));
183181
when(abstractAutomatedTaskConfig.getLock()).thenReturn(lock);
184182

185-
186183
AbstractLockableAutomatedTask<AbstractAutomatedTaskConfig> abstractLockableAutomatedTask = spy(createAbstractLockableAutomatedTask(() -> {
187184
}));
188185

@@ -198,6 +195,7 @@ void lockedTaskRun_shouldFail_whenTaskInterupted(CapturedOutput output) throws E
198195
"Timeout waiting for task status to be FAILED", 10);
199196
verify(abstractLockableAutomatedTask, atLeastOnce()).setAutomatedTaskStatus(AutomatedTaskStatus.FAILED);
200197
verify(lockedTask).assertLocked();
198+
verify(logApi).taskFailed(any(), any());
201199
}
202200
}
203201

0 commit comments

Comments
 (0)