]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9540 log MessageException message if task can't be finalized 2240/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 12 Jul 2017 09:39:23 +0000 (11:39 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 21 Jul 2017 10:22:10 +0000 (12:22 +0200)
so that the cause of the failure of the task is not lost in the void

server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java
server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java

index b5db945cb9690a7adfc0468cd1e766d0b822dfa0..d52bed437f9a9489340b18ac440a0d88ec5df656 100644 (file)
@@ -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();
index dfef725387289b82d7c5176f66f48edf74ad3309..e30a15b116e61fcaf768c2d01e426c87cdbe063d 100644 (file)
@@ -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);