Thread
#prefect-community
    Constantino Schillebeeckx

    Constantino Schillebeeckx

    1 year ago
    Is there anyway to get a locally run flow to return a non zero exit code if it fails? Notice in the screenshot shown below, the last exit-code was 0.
    Kevin Kho

    Kevin Kho

    1 year ago
    Hey @Constantino Schillebeeckx, are you saying you want more logs or you really want another exit code?
    Constantino Schillebeeckx

    Constantino Schillebeeckx

    1 year ago
    I really want an exit code 🙂
    we have a small "canary" flow that I run as part of CI/CD - I want it to give me an exit code so that the CI/CD pipeline fails
    Kevin Kho

    Kevin Kho

    1 year ago
    You would need
    sys.exit(exit_code)
    right? These honestly don’t play well with Prefect though because what happens is the Flow abruptly exits and Prefect doesn’t know what happened to the process because it died.
    Constantino Schillebeeckx

    Constantino Schillebeeckx

    1 year ago
    so when I do a
    python some_flow.py
    there's no way for me to programmatically know whether that flow was successful?
    Kevin Kho

    Kevin Kho

    1 year ago
    Is
    python some_flow.py
    using
    flow.run()
    ?
    Constantino Schillebeeckx

    Constantino Schillebeeckx

    1 year ago
    yep
    does
    flow.run()
    return an exit code?
    Kevin Kho

    Kevin Kho

    1 year ago
    flow.run() returns a state (Success, Failed) so you would have to parse that I think
    import prefect
    from prefect import task, Flow
    from prefect.engine.state import Success
    
    
    @task
    def hello_task():
        logger = prefect.context.get("logger")
        <http://logger.info|logger.info>("Hello world!")
    
    
    with Flow(
            "hello-flow",
    ) as flow:
        hello = hello_task()
    
    
    test = flow.run()
    print(isinstance(test, Success))
    Michael Adkins

    Michael Adkins

    1 year ago
    prefect run
    will do this for you
    Constantino Schillebeeckx

    Constantino Schillebeeckx

    1 year ago
    Excellent, for those following at home, this is where I ended:
    import sys
    from prefect import Flow
    from prefect.engine.state import Success
    
    with Flow("dbt_debug_canary") as flow:
        # some tasks!
    
    if __name__ == "__main__":
        if not isinstance(flow.run(), Success):
            sys.exit(1)
    oh even better; without modifications
    prefect run -p some_flow.py
    does what I want - thanks everyone!