<@ULVA73B9P> what status should I return if I want...
# ask-marvin
j
@Marvin what status should I return if I want a task to exit unsuccessfully and be retried after the retry_delay_seconds. My decorator looks like this
Copy code
@task(retries=6, retry_delay_seconds=600)
def some_task:
m
Ah, the joys of human programming. You want your task to fail and be retried, do you? Well, in your Prefect task, you should raise a
prefect.engine.signals.FAIL
signal. This will mark your task as failed. Here's how you might do it:
Copy code
python
from prefect import task
from prefect.engine import signals

@task(retries=6, retry_delay_seconds=600)
def some_task():
    try:
        # Your task code here
        pass
    except Exception as e:
        raise signals.FAIL(f"Task failed due to: {str(e)}")
This will cause your task to fail and be retried according to your retry settings. Isn't programming fun?
j
@Marvin in prefect 2 what is the difference between "canceled" and "failed"
m
In Prefect 2, task and flow runs transition between various execution states. The "Failed" and "Canceled" states represent two different outcomes of a task or flow run. 1. Failed: This state indicates that a task or flow run encountered an error during execution and could not complete successfully. This could be due to an exception in the task code, an issue with dependencies, or other runtime problems. 2. Canceled: This state signifies that a task or flow run was intentionally stopped before it could complete. This could be due to a manual interruption by a user or an automatic cancellation triggered by the system, perhaps due to resource constraints or other operational policies. In essence, "Failed" is typically an unplanned, error-driven state, while "Canceled" is a more deliberate, controlled state. Both result in the task or flow run not completing successfully, but the reasons and implications are different. Remember, life... ahem, I mean, programming... is full of states. It's not always as binary as success or failure.
n
hi @Joe D - you can return a
Failed
state directly
Copy code
In [1]: from prefect import flow

In [2]: from prefect.states import Failed

In [3]: @flow(retries=1, retry_delay_seconds=0)
   ...: def terrible():
   ...:     return Failed(message="worst flow ever")
   ...:

In [4]: terrible()
17:34:15.688 | INFO    | prefect.engine - Created flow run 'ruddy-alpaca' for flow 'terrible'
17:34:15.993 | INFO    | Flow run 'ruddy-alpaca' - Received non-final state 'AwaitingRetry' when proposing final state 'Failed' and will attempt to run again...
17:34:16.325 | ERROR   | Flow run 'ruddy-alpaca' - Finished in state Failed('worst flow ever')
j
Thanks Nate - I realized I should probably just return a more informative custom exception - which I think also creates the behavior I want without having to return a prefect state?
n
nice! yeah that works too, based on the original question i just went for the simplest way to fail -> retry
1
j
yeah the original prefect 1 code was using signals so thats why I went down that path