Skip to content

Commit

Permalink
fix(core): reset synthetic stages when restarting a pipeline (#1292)
Browse files Browse the repository at this point in the history
* fix(core): reset synthetic stages when restarting a pipeline
  • Loading branch information
robfletcher committed Apr 24, 2017
1 parent d77d72c commit 01762bc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.netflix.spinnaker.orca.pipeline;

import java.util.*;
import java.util.stream.Stream;
import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.pipeline.model.Execution;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
Expand Down Expand Up @@ -111,24 +112,32 @@ public static <T extends Execution<T>> Stage<T> prepareStageForRestart(
})
.collect(toList());

childStages.forEach((Stage<T> childStage) -> {
StageDefinitionBuilder stageBuilder = allStageBuilders
.stream()
.filter(it -> it.getType().equals(childStage.getType()))
.findFirst()
.orElse(self);
stageBuilder.prepareStageForRestart(executionRepository, childStage, allStageBuilders);

// the default `prepareStageForRestart` behavior sets a stage back to RUNNING, that's not appropriate for child stages
childStage.setStatus(ExecutionStatus.NOT_STARTED);
List<Task> childStageTasks = childStage.getTasks();
childStageTasks.forEach(it -> {
it.setStartTime(null);
it.setEndTime(null);
it.setStatus(ExecutionStatus.NOT_STARTED);
List<String> restartingStageAndChildren = Stream.concat(Stream.of(stage), childStages.stream()).map(Stage::getId).collect(toList());
List<Stage<T>> syntheticStages = stages
.stream()
.filter(it -> it.getStatus().isComplete() && restartingStageAndChildren.contains(it.getParentStageId()))
.collect(toList());

Stream
.concat(childStages.stream(), syntheticStages.stream())
.forEach(childStage -> {
StageDefinitionBuilder stageBuilder = allStageBuilders
.stream()
.filter(it -> it.getType().equals(childStage.getType()))
.findFirst()
.orElse(self);
stageBuilder.prepareStageForRestart(executionRepository, childStage, allStageBuilders);

// the default `prepareStageForRestart` behavior sets a stage back to RUNNING, that's not appropriate for child stages
childStage.setStatus(ExecutionStatus.NOT_STARTED);
List<Task> childStageTasks = childStage.getTasks();
childStageTasks.forEach(it -> {
it.setStartTime(null);
it.setEndTime(null);
it.setStatus(ExecutionStatus.NOT_STARTED);
});
executionRepository.storeStage(childStage);
});
executionRepository.storeStage(childStage);
});

List<Task> tasks = stage.getTasks();
tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,22 @@ public abstract class Execution<T extends Execution<T>> implements Serializable
AuthenticationDetails authentication;
PausedDetails paused;

public Stage namedStage(String type) {
public Stage<T> namedStage(String type) {
return stages
.stream()
.filter(it -> it.getType().equals(type))
.findFirst()
.orElse(null);
}

public Stage<T> stageById(String stageId) {
return stages
.stream()
.filter(it -> it.getId().equals(stageId))
.findFirst()
.orElse(null);
}

@Data
public static class AuthenticationDetails implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Stage(T execution, String type) {
/**
* Gets the last stage preceding this stage that has the specified type.
*/
public Stage preceding(String type) {
public Stage<T> preceding(String type) {
int i = getExecution()
.getStages()
.indexOf(this);
Expand All @@ -164,6 +164,14 @@ public Stage preceding(String type) {
.orElse(null);
}

public Collection<Stage<T>> children() {
return getExecution()
.getStages()
.stream()
.filter(it -> getId().equals(it.getParentStageId()))
.collect(toList());
}

@JsonIgnore
@Setter
private StageNavigator stageNavigator = null;
Expand Down

0 comments on commit 01762bc

Please sign in to comment.