Skip to content

Commit

Permalink
ECS: return failed status when exit code is empty (#1724)
Browse files Browse the repository at this point in the history
  • Loading branch information
builtinnya authored Feb 9, 2023
1 parent a270498 commit 10843b3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,13 @@ CommandStatus createNextCommandStatus(
// To fetch log until all logs is written in CloudWatch,
// finish this poll once and wait finish marker in head of this method in next poll, considering risk of crushing in this poll.
nextStatus.put("task_finished_at", Instant.now().getEpochSecond());
// Set exit code of container finished to nextStatus
nextStatus.put("status_code", task.getContainers().get(0).getExitCode());
// Set exit code of container finished to nextStatus.
// If exit code doesn't exist, something's wrong with execution, so set status code to 1 to make workflow fail
Integer exitCode = task.getContainers().get(0).getExitCode();
if (exitCode == null) {
logger.debug("Container has no exit code. The status code will be set as error (1)");
}
nextStatus.put("status_code", exitCode != null ? exitCode : 1);
}

// always return false to check if all logs are fetched. (return in head of this method after checking finish marker.)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,35 @@ public void testGetErrorMessageFromTask()
}
}
}

@Test
public void testErrorOnEmptyExitCodeFromContainer()
throws Exception
{
final EcsClient ecsClient = mock(EcsClient.class);
final CommandContext commandContext = mock(CommandContext.class);
final EcsCommandExecutor executor = spy(new EcsCommandExecutor(
systemConfig, ecsClientFactory, dockerCommandExecutor,
storageManager, projectArchiveLoader, commandLogger));
final Task task = mock(Task.class);
final Container container = mock(Container.class);

final ObjectNode previousStatusJson = om.createObjectNode();
previousStatusJson.put("cluster_name", "my_cluster");
previousStatusJson.put("task_arn", "my_task_arn");
previousStatusJson.put("executor_state", om.createObjectNode());
previousStatusJson.put("awslogs", om.createObjectNode().nullNode());

doReturn(mock(EcsClientConfig.class)).when(executor).createEcsClientConfig(any(Optional.class), any(Config.class), any(Config.class));
doReturn(ecsClient).when(ecsClientFactory).createClient(any(EcsClientConfig.class));
doReturn(mock(TaskRequest.class)).when(commandContext).getTaskRequest();
doReturn("stopped").when(task).getLastStatus();
doReturn(null).when(container).getExitCode();
doReturn(Arrays.asList(container)).when(task).getContainers();
doReturn(task).when(ecsClient).getTask(previousStatusJson.get("cluster_name").asText(), previousStatusJson.get("task_arn").asText());

CommandStatus commandStatus = executor.poll(commandContext, previousStatusJson);

assertThat(commandStatus.getStatusCode(), is(1));
}
}

0 comments on commit 10843b3

Please sign in to comment.