Zohaa Qamar

    Zohaa Qamar

    9 months ago
    Hi all, I have a Python task in my flow that has sys.exit() in it meaning I want that task to break if some condition has met and do not proceed further. But, my task keeps on running in this case and does nothing. Any help?
    Sylvain Hazard

    Sylvain Hazard

    9 months ago
    You should be able to use something like
    from prefect.engine.signals import FAIL
    from prefect import task
    
    @task
    def my_task():
       if condition:
            raise FAIL("Fail Message")
    This will have the task enter a failed state.
    There are some others signals that might be useful in some cases, see here.
    Zohaa Qamar

    Zohaa Qamar

    9 months ago
    would it retry in case of fail?
    Sylvain Hazard

    Sylvain Hazard

    9 months ago
    I believe it would if the task's parameters allow it to.
    e.g.
    max_retries=1
    Zohaa Qamar

    Zohaa Qamar

    9 months ago
    right, thanks.. will try this
    even for Success, I can do raise SUCCESS("Message")?
    Sylvain Hazard

    Sylvain Hazard

    9 months ago
    Theoretically you can do that, yeah. It think it is not recommended though and that just having your task return something is a more "prefectic way". Actually, you could also have your task fail by raising any error that you would not catch e.g.
    @task
    def my_task:
       if condition:
           raise AttributeError("something went wrong")
    Anna Geller

    Anna Geller

    9 months ago
    @Zohaa Qamar As @Sylvain Hazard mentioned, the best way to solve it, would be to raise a signal that would end the task run execution. Raising FAILED signal respects retries. If you don’t want retries to be triggered, use ENDRUN:
    from prefect.engine.signals import ENDRUN
    from prefect import task
    
    @task
    def my_task():
       if condition:
            raise ENDRUN("Exiting the task execution...")
    Zohaa Qamar

    Zohaa Qamar

    9 months ago
    ENDRUN would stop the whole flow execution or would just stop that task and move to next ones?
    Anna Geller

    Anna Geller

    9 months ago
    it would stop the task. If you want to stop the entire flow, the easiest way to do it would be adding a conditional logic:
    from prefect import task, Flow, case
    
    @task(log_stdout=True)
    def end_flow():
        print("Ending the flow run due to the XXX")
    
    with Flow("conditional-logic") as flow:
        cond = check_condition()
    
        with case(cond, True):
            val = t1()
            val2 = t2()
            t3(val, val2)
    
        with case(cond, False):
            end_flow()
    Zohaa Qamar

    Zohaa Qamar

    9 months ago
    thanks!