stage.getId(), stage.getExecution().getId(), canaryPipelineExecutionId, stage.getContext()); } catch (Exception e) { log.error("Failed to cancel stage (stageId: {}, executionId: {}), e: {}", stage.getId(), stage.getExecution().getId(), e.getMessage(), e); stage.getId(), stage.getExecution().getId(), stage.getContext());
public Result(Stage stage, Map details) { this.stageId = stage.getId(); this.details = details; }
@Nonnull public Stage stageById(String stageId) { return stages .stream() .filter(it -> it.getId().equals(stageId)) .findFirst() .orElseThrow(() -> new IllegalArgumentException(String.format("No stage with id %s exists", stageId))); }
/** * Returns the top-most stage. */ @JsonIgnore public Stage getTopLevelStage() { Stage topLevelStage = this; while (topLevelStage.parentStageId != null) { String sid = topLevelStage.parentStageId; Optional<Stage> stage = execution.getStages().stream().filter(s -> s.id.equals(sid)).findFirst(); if (stage.isPresent()) { topLevelStage = stage.get(); } else { throw new IllegalStateException("Could not find stage by parentStageId (stage: " + topLevelStage.getId() + ", parentStageId:" + sid + ")"); } } return topLevelStage; }
@Deprecated static @Nonnull Stage newStage( @Nonnull Execution execution, @Nonnull String type, @Nullable String name, @Nonnull Map<String, Object> context, @Nullable Stage parent, @Nullable SyntheticStageOwner stageOwner ) { Stage stage = new Stage(execution, type, name, context); if (parent != null) { stage.setParentStageId(parent.getId()); } stage.setSyntheticStageOwner(stageOwner); return stage; }
default Map<String, List<String>> manifestNamesByNamespace(Stage stage) { Map<String, List<String>> result = (Map<String, List<String>>) stage.getContext().get("outputs.manifestNamesByNamespace"); if (result != null) { return result; } result = new HashMap<>(); String name = (String) stage.getContext().get("manifest.name"); String location = (String) stage.getContext().get("manifest.location"); if (name != null && location != null) { result.put(location, Collections.singletonList(name)); } else { Logger.getLogger(this.getClass().getName()).warning("No manifests found in stage " + stage.getId()); } return result; } }
private void onStageStarted(StageStarted event) { Execution execution = retrieve(event); List<Stage> stages = execution.getStages(); stages .stream() .filter(it -> it.getId().equals(event.getStageId())) .findFirst() .ifPresent(stage -> delegate.beforeStage(persister, stage)); }
private void onStageComplete(StageComplete event) { Execution execution = retrieve(event); List<Stage> stages = execution.getStages(); stages .stream() .filter(it -> it.getId().equals(event.getStageId())) .findFirst() .ifPresent(stage -> delegate.afterStage(persister, stage)); }
/** * Checks existence of a Stage by id * @param obj #root.execution * @param id the name or id of the stage to check existence * @return W */ static boolean stageExists(Object obj, String id) { if (obj instanceof Execution) { Execution execution = (Execution) obj; return execution.getStages() .stream() .anyMatch(i -> id != null && (id.equals(i.getName()) || id.equals(i.getId()))); } throw new SpelHelperFunctionException(String.format("Invalid first param to #stage(%s). must be an execution", id)); }
@Override public void taskGraph( @Nonnull Stage stage, @Nonnull TaskNode.Builder builder) { if (!isTopLevelStage(stage)) { String preconditionType = stage.getContext().get("preconditionType").toString(); if (preconditionType == null) { throw new IllegalStateException(format("no preconditionType specified for stage %s", stage.getId())); } Task preconditionTask = preconditionTasks .stream() .filter(it -> it.getPreconditionType().equals(preconditionType)) .findFirst() .orElseThrow(() -> new IllegalStateException(format("no Precondition implementation for type %s", preconditionType)) ); builder.withTask("checkPrecondition", preconditionTask.getClass()); } }
private static Predicate<Stage> matchesDeployedStage(String ...id) { List<String> idsOrNames = Arrays.asList(id); if (!idsOrNames.isEmpty()){ return stage -> DEPLOY_STAGE_NAMES.contains(stage.getType()) && stage.getContext().containsKey("deploy.server.groups") && stage.getStatus() == ExecutionStatus.SUCCEEDED && (idsOrNames.contains(stage.getName()) || idsOrNames.contains(stage.getId())); } else { return stage -> DEPLOY_STAGE_NAMES.contains(stage.getType()) && stage.getContext().containsKey("deploy.server.groups") && stage.getStatus() == ExecutionStatus.SUCCEEDED; } } }
private void onTaskStarted(TaskStarted event) { Execution execution = retrieve(event); List<Stage> stages = execution.getStages(); stages .stream() .filter(it -> it.getId().equals(event.getStageId())) .findFirst() .ifPresent(stage -> delegate.beforeTask( persister, stage, stage.getTasks().stream().filter(it -> it.getId().equals(event.getTaskId())).findFirst().get() ) ); }
private String generateRefId() { long offset = parent .getExecution() .getStages() .stream() .filter(i -> parent.getId().equals(i.getParentStageId()) && type == i.getSyntheticStageOwner()) .count(); return format( "%s%s%d", parent.getRefId(), type == STAGE_BEFORE ? "<" : ">", offset + graph.nodes().size() ); }
private void storeStageInternal(RedisClientDelegate delegate, Stage stage, Boolean updateIndex) { String key = executionKey(stage); String indexKey = format("%s:stageIndex", key); Map<String, String> serializedStage = serializeStage(stage); List<String> keysToRemove = serializedStage.entrySet().stream() .filter(e -> e.getValue() == null) .map(Map.Entry::getKey) .collect(Collectors.toList()); serializedStage.values().removeIf(Objects::isNull); delegate.withTransaction(tx -> { tx.hmset(key, serializedStage); if (!keysToRemove.isEmpty()) { tx.hdel(key, keysToRemove.toArray(new String[0])); } if (updateIndex) { BinaryClient.LIST_POSITION pos = stage.getSyntheticStageOwner() == STAGE_BEFORE ? BEFORE : AFTER; tx.linsert(indexKey, pos, stage.getParentStageId(), stage.getId()); } tx.exec(); }); }
private void onTaskComplete(TaskComplete event) { Execution execution = retrieve(event); List<Stage> stages = execution.getStages(); ExecutionStatus status = event.getStatus(); stages .stream() .filter(it -> it.getId().equals(event.getStageId())) .findFirst() .ifPresent(stage -> delegate.afterTask(persister, stage, stage.getTasks().stream().filter(it -> it.getId().equals(event.getTaskId())).findFirst().get(), status, // TODO: not sure if status.isSuccessful covers all the weird cases here status.isSuccessful() ) ); }
@Override public void updateStageContext(@Nonnull Stage stage) { RedisClientDelegate delegate = getRedisDelegate(stage); String key = executionKey(stage); String contextKey = format("stage.%s.context", stage.getId()); delegate.withCommandsClient(c -> { try { c.hset(key, contextKey, mapper.writeValueAsString(stage.getContext())); } catch (JsonProcessingException e) { throw new StageSerializationException( format("Failed serializing stage, executionId: %s, stageId: %s", stage.getExecution().getId(), stage.getId()), e ); } }); }
public StageStarted( @Nonnull Object source, @Nonnull Stage stage ) { this(source, stage.getExecution().getType(), stage.getExecution().getId(), stage.getId(), stage.getType(), stage.getName()); }
public StageComplete( @Nonnull Object source, @Nonnull Stage stage ) { this(source, stage.getExecution().getType(), stage.getExecution().getId(), stage.getId(), stage.getType(), stage.getName(), stage.getStatus()); }
public TaskStarted( @Nonnull Object source, @Nonnull Stage stage, @Nonnull Task task ) { this( source, stage.getExecution().getType(), stage.getExecution().getId(), stage.getId(), stage.getType(), stage.getName(), task.getId(), task.getImplementingClass(), task.getName() ); }
/** * Adds a new stage to the graph. By default the new stage is not dependent on any * others. Use {@link #connect(Stage, Stage)} to make it depend on other stages or * have other stages depend on it. */ public void add(@Nonnull Stage stage) { stage.setExecution(parent.getExecution()); stage.setParentStageId(parent.getId()); stage.setSyntheticStageOwner(type); if (graph.addNode(stage)) { stage.setRefId(generateRefId()); } lastAdded = stage; }