Is there anyway to get a locally run flow to retur...
# ask-community
c
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.
k
Hey @Constantino Schillebeeckx, are you saying you want more logs or you really want another exit code?
c
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
k
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.
c
so when I do a
python some_flow.py
there's no way for me to programmatically know whether that flow was successful?
k
Is
python some_flow.py
using
flow.run()
?
c
yep
does
flow.run()
return an exit code?
k
flow.run() returns a state (Success, Failed) so you would have to parse that I think
Copy code
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))
🙌 1
z
prefect run
will do this for you
c
Excellent, for those following at home, this is where I ended:
Copy code
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!