Sagar Dhande
09/25/2023, 2:19 AMSagar Dhande
09/25/2023, 4:24 PMSerina
09/25/2023, 5:25 PMPREFECT_TASK_DEFAULT_RETRIES
setting to non-zero to automatically retry failed tasks or else pass it in to the specific task in question as an argument like @task(retries=10)
.
Here is a full working example:
from prefect import flow, task
from prefect.logging import get_run_logger
@task
def always_succeed():
# task that should run once
logger = get_run_logger()
<http://logger.info|logger.info>("This is fine.")
@task(retries=10) # by configuring retries on a task level, it means that we won't have to run the entire flow again if this task fails
def often_fail():
# task that should run many times
import random
if random.random() < 0.9:
raise ValueError("This should happen often")
return "This is fine."
@flow(retries=2)
def sometimes_failing_flow():
always_succeed()
often_fail()
if __name__ == "__main__":
sometimes_failing_flow()
Even if you remove flow-level retries from this example, the task itself will be retried up to 10 times. Adding retries=2
on the flow here means that if the 10 task retries still leads to a failed state, it will then try to run the flow again (and I think it would still skip the always_succeed()
task).Sagar Dhande
09/26/2023, 1:13 AMSagar Dhande
09/26/2023, 1:15 AMsometimes_failing_flow
via a worker?