Skip to content

Choice states cannot be chained #31

Closed
@piotrdrazikowski

Description

@piotrdrazikowski

I'm currently adopting this package in place of own code to generate a state machine definition and one of our main patterns is to have a choice state that checks for a certain variable and kicks off previous step again if the condition was met, or move to the next step if not.
Example (failing) code:

import stepfunctions

first_job = stepfunctions.steps.Pass('First job')
second_job = stepfunctions.steps.Pass('Second job')

check = stepfunctions.steps.Choice('Check first job')
check.add_choice(
    rule=stepfunctions.steps.ChoiceRule.BooleanEquals(
        variable='$run_me_again',
        value=True
    ),
    next_step=first_job
)
check.default_choice(second_job) # This could be set automatically

chain = stepfunctions.steps.Chain([first_job, check, second_job])

This code currently gives State type `Choice` does not support method `next`.

I understand that the choice state was designed to branch the workflow and that terminates the chain. Please correct me if i'm wrong but in my opinion the presence of the next step can be treated as the default_choice for the choice state.

Here is a quick workaround that solves our issue:

class ChainableChoice(stepfunctions.steps.Choice):
    def next(self, next_step):
        self.default_choice(next_step)
        return next_step

I'd be more than happy to submit a proper PR, please let me know what do you think about it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions