Skip to content

Commit 41d70c6

Browse files
authored
Merge pull request #343 from FlowCI/fix/1425
fix step failure but goes to next
2 parents 234f336 + 16ee33d commit 41d70c6

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

core/src/main/java/com/flowci/core/job/service/JobActionServiceImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ private void dispatch(Job job, Agent agent) {
662662
/**
663663
* Dispatch next step to agent
664664
*
665-
* @return true if next step dispatched, false if no more steps
665+
* @return true if next step dispatched, false if no more steps or failure
666666
*/
667667
private boolean toNextStep(JobSmContext context) {
668668
Job job = context.job;
@@ -683,7 +683,7 @@ private boolean toNextStep(JobSmContext context) {
683683
NodeTree tree = ymlManager.getTree(job);
684684
StepNode node = tree.get(currentPath);
685685
updateJobTime(job, step, tree, node);
686-
updateJobStatusAndContext(job, node, step);
686+
updateJobContextAndLatestStatus(job, node, step);
687687

688688
// to next step
689689
Optional<StepNode> next = findNext(tree, node, step.isSuccess());
@@ -739,7 +739,7 @@ private void updateJobTime(Job job, ExecutedCmd execCmd, NodeTree tree, StepNode
739739
job.setFinishAt(execCmd.getFinishAt());
740740
}
741741

742-
private void updateJobStatusAndContext(Job job, StepNode node, ExecutedCmd cmd) {
742+
private void updateJobContextAndLatestStatus(Job job, StepNode node, ExecutedCmd cmd) {
743743
// merge output to job context
744744
Vars<String> context = job.getContext();
745745
context.merge(cmd.getOutput());
@@ -748,15 +748,15 @@ private void updateJobStatusAndContext(Job job, StepNode node, ExecutedCmd cmd)
748748
context.put(Variables.Job.FinishAt, job.finishAtInStr());
749749
context.put(Variables.Job.Steps, stepService.toVarString(job, node));
750750

751-
// after status not apart of job status
751+
// latest status saved in context apart from job status property
752752
job.setStatusToContext(StatusHelper.convert(cmd));
753753
job.setErrorToContext(cmd.getError());
754754
}
755755

756756
private Optional<StepNode> findNext(NodeTree tree, Node current, boolean isSuccess) {
757757
StepNode next = tree.next(current.getPath());
758758

759-
if (Objects.isNull(next)) {
759+
if (Objects.isNull(next) || !isSuccess) {
760760
return Optional.empty();
761761
}
762762

core/src/test/java/com/flowci/core/test/job/JobServiceTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,28 @@ public void should_handle_cmd_callback_for_success_status() {
360360
Assert.assertEquals(Status.SUCCESS, job.getStatus());
361361
}
362362

363+
@Test
364+
public void should_handle_cmd_callback_for_failure_status() throws IOException {
365+
// init: agent and job
366+
yml = ymlService.saveYml(flow, StringHelper.toString(load("flow-with-failure.yml")));
367+
Agent agent = agentService.create("hello.agent", null, Optional.empty());
368+
Job job = prepareJobForRunningStatus(agent);
369+
370+
NodeTree tree = ymlManager.getTree(job);
371+
StepNode firstNode = tree.next(tree.getRoot().getPath());
372+
ExecutedCmd firstStep = stepService.get(job.getId(), firstNode.getPathAsString());
373+
374+
// when: cmd of first node with failure
375+
firstStep.setStatus(ExecutedCmd.Status.EXCEPTION);
376+
executedCmdDao.save(firstStep);
377+
jobEventService.handleCallback(firstStep);
378+
379+
// then: job should be failure
380+
job = jobDao.findById(job.getId()).get();
381+
Assert.assertEquals(Status.FAILURE, job.getStatus());
382+
Assert.assertEquals("hello/step-1", job.getCurrentPath());
383+
}
384+
363385
@Test
364386
public void should_handle_cmd_callback_for_failure_status_but_allow_failure() throws IOException {
365387
// init: agent and job

core/src/test/resources/flow-failure-with-after.yml renamed to core/src/test/resources/flow-with-failure.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ steps:
77
FLOW_WORKSPACE: "echo step"
88
FLOW_VERSION: "echo step version"
99
script: |
10-
echo hello
10+
echo shoulde failure
11+
12+
- script: |
13+
echo cannot run

0 commit comments

Comments
 (0)