diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-07-12 11:39:23 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-07-21 12:22:10 +0200 |
commit | f01745f4663f2ebd4bd9e5b55e98335739a700a2 (patch) | |
tree | 5e60d627b46688025a712fc3955732fe7627d5df | |
parent | b44daaf66b0ca8762f4223b77bef87213ea1f3b1 (diff) | |
download | sonarqube-f01745f4663f2ebd4bd9e5b55e98335739a700a2.tar.gz sonarqube-f01745f4663f2ebd4bd9e5b55e98335739a700a2.zip |
SONAR-9540 log MessageException message if task can't be finalized
so that the cause of the failure of the task is not lost in the void
-rw-r--r-- | server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java | 7 | ||||
-rw-r--r-- | server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java | 48 |
2 files changed, 54 insertions, 1 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java index b5db945cb96..d52bed437f9 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java @@ -149,7 +149,12 @@ public class CeWorkerImpl implements CeWorker { try { queue.remove(task, status, taskResult, error); } catch (Exception e) { - LOG.error(format("Failed to finalize task with uuid '%s' and persist its state to db", task.getUuid()), e); + String errorMessage = format("Failed to finalize task with uuid '%s' and persist its state to db", task.getUuid()); + if (error instanceof MessageException) { + LOG.error(format("%s. Task failed with MessageException \"%s\"", errorMessage, error.getMessage()), e); + } else { + LOG.error(errorMessage, e); + } } finally { stopActivityProfiler(ceProfiler, task, status); ceLogging.clearForTask(); diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java index dfef7253872..e30a15b116e 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java @@ -336,6 +336,7 @@ public class CeWorkerImplTest { makeTaskProcessorFail(ceTask); underTest.call(); + List<String> logs = logTester.logs(LoggerLevel.ERROR); assertThat(logs).hasSize(2); assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid()); @@ -350,11 +351,58 @@ public class CeWorkerImplTest { makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process")); underTest.call(); + List<String> logs = logTester.logs(LoggerLevel.ERROR); assertThat(logs).hasSize(1); assertThat(logs.get(0)).contains(" | submitter=FooBar | time="); } + @Test + public void log_error_when_task_was_successful_but_ending_state_can_not_be_persisted_to_db() throws Exception { + CeTask ceTask = createCeTask("FooBar"); + when(queue.peek(anyString())).thenReturn(Optional.of(ceTask)); + taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor); + doThrow(new RuntimeException("Simulate queue#remove failing")).when(queue).remove(ceTask, CeActivityDto.Status.SUCCESS, null, null); + + underTest.call(); + + assertThat(logTester.logs(LoggerLevel.ERROR)).containsOnly("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db"); + } + + @Test + public void log_error_when_task_failed_and_ending_state_can_not_be_persisted_to_db() throws Exception { + CeTask ceTask = createCeTask("FooBar"); + when(queue.peek(anyString())).thenReturn(Optional.of(ceTask)); + taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor); + IllegalStateException ex = makeTaskProcessorFail(ceTask); + doThrow(new RuntimeException("Simulate queue#remove failing")).when(queue).remove(ceTask, CeActivityDto.Status.FAILED, null, ex); + + underTest.call(); + + List<String> logs = logTester.logs(LoggerLevel.ERROR); + assertThat(logs).hasSize(3); + assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid()); + assertThat(logs.get(1)).isEqualTo("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db"); + assertThat(logs.get(2)).contains(" | submitter=FooBar | time="); + } + + @Test + public void log_error_when_task_failed_with_MessageException_and_ending_state_can_not_be_persisted_to_db() throws Exception { + CeTask ceTask = createCeTask("FooBar"); + when(queue.peek(anyString())).thenReturn(Optional.of(ceTask)); + taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor); + MessageException ex = makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process")); + doThrow(new RuntimeException("Simulate queue#remove failing")).when(queue).remove(ceTask, CeActivityDto.Status.FAILED, null, ex); + + underTest.call(); + + List<String> logs = logTester.logs(LoggerLevel.ERROR); + assertThat(logs).hasSize(2); + assertThat(logs.get(0)).isEqualTo("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db. " + + "Task failed with MessageException \"" + ex.getMessage() + "\""); + assertThat(logs.get(1)).contains(" | submitter=FooBar | time="); + } + private Thread createThreadNameVerifyingThread(String threadName) { return new Thread(() -> { verifyUnchangedThreadName(threadName); |