Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC(StepFunctions): A way to have States inside a Chain more easily exposed #7256

Closed
andrestone opened this issue Apr 8, 2020 · 1 comment · Fixed by #7324, bifravst/cloudformation-cleaner#11 or bifravst/cloudformation-cleaner#13
Assignees
Labels
@aws-cdk/aws-stepfunctions Related to AWS StepFunctions feature-request A feature should be added or improved. in-progress This issue is being actively worked on. needs-triage This issue or PR still needs to be triaged.

Comments

@andrestone
Copy link
Contributor

Maybe I'm missing something, but I couldn't find any friendly path to play with definitions programatically. All properties / methods that could be used to this purpose are inaccessible from outside the class definition scope.

I'd like to have a simple public method similar to findReachableEndStates, that could be defined like this:

  /**
   * Find the set of states reachable through transitions from the given start state
   */
  public static findReachableStates(start: State, options: FindStateOptions = {}) {
    const visited = new Set<State>();
    const ret = new Set<State>();
    const queue = [start];
    while (queue.length > 0) {
      const state = queue.splice(0, 1)[0]!;
      if (visited.has(state)) { continue; }
      visited.add(state);
      const outgoing = state.outgoingTransitions(options);
      queue.push(...outgoing);
      ret.add(state);
    }
    return Array.from(ret);
  }
@andrestone andrestone added the needs-triage This issue or PR still needs to be triaged. label Apr 8, 2020
@andrestone andrestone changed the title RFC: A way to have States inside a Chain more easily exposed. RFC: A way to have States inside a Chain more easily exposed. (StepFunctions) Apr 8, 2020
@andrestone andrestone changed the title RFC: A way to have States inside a Chain more easily exposed. (StepFunctions) RFC(StepFunctions): A way to have States inside a Chain more easily exposed Apr 8, 2020
@SomayaB SomayaB added the @aws-cdk/aws-stepfunctions Related to AWS StepFunctions label Apr 9, 2020
@SomayaB SomayaB added needs-discussion This issue/PR requires more discussion with community. feature-request A feature should be added or improved. and removed needs-discussion This issue/PR requires more discussion with community. labels Apr 9, 2020
@SomayaB SomayaB added the in-progress This issue is being actively worked on. label Apr 14, 2020
@nija-at
Copy link
Contributor

nija-at commented Apr 16, 2020

Hi @andrestone - thanks for updating. I'm not sure I quite follow what you've said. Can you expand or provide a concrete example?

Sure.

This method takes an original definition and adds a ResumeTo Choice state to the top of the chain, programatically adding each existing state id as a Choice condition to be matched.

private static transformToResumable(definition: sf.IChainable): sf.IChainable {
    // Building a set of States
    const states = new Set<sf.State>();

    // All states starting from a giving state (using the method of this PR)
    sf.State.findReachableStates(definition.startState).forEach(state => {
        states.add(state);
    });

    // Here we add our ResumeTo logic to the top of the chain
    const choices = new sf.Choice(scope, "ResumeTo");

    // We used Set to ensure we have only a single state, although it's redundant with
    // the method implementation
    let statesArray = Array.from(states);

    // For each pre-existing state, we add a Choice condition
    for (let i = 0; i < statesArray.length; i++) {
        choices.when(sf.Condition.stringEquals('$.resumeTo', statesArray[i].id), statesArray[i]);
    }

    // The default is the original definition
    choices.otherwise(definition);

    // This is our new State Machine definition ready to accept `resumeTo` as part of the input
    // to resume the execution starting from the referred state
    return choices
}

Should I add this to the commit message?

Thanks.

Originally posted by @andrestone in #7324 (comment)

@mergify mergify bot closed this as completed in #7324 Apr 22, 2020
mergify bot pushed a commit that referenced this issue Apr 22, 2020
… in a state machine definition (#7324)

A new method - `findReachableStates` - to retrieve all reachable states subsequent to a given initial state, including itself.

Motivation
With this method, developers can programatically modify their state definitions, given an initial state.
For example, walk through the set of states and attach some of them to a 'ResumeTo' Choice state, so that state machine executions can jump directly to one of these states. 

closes #7256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment