https://prefect.io logo
Title
j

Jeremy Savage

05/11/2022, 12:26 PM
Hi guys, just a quick question. I see that tasks have a field called max retries - I have a task which I wish to add a certain number of max retries to. Can I add max_retries as below:
@task(max_retries=200)
def some_task():
    if helpers.test_if_job_done() is False:
        raise RETRY(
            "Work not done yet, retrying in 20 seconds.",
            start_time=pendulum.now().add(seconds=20),
        )
TIA
a

Anna Geller

05/11/2022, 12:35 PM
Yup, that's a totally valid use case. In fact, we have a very similar logic as a recommendation for polling logic here. The only issue I see with this code is which part of your code is supposed to cause a retry behavior - the retry set on a task decorator or the one triggered by raising a RETRY signal. When you already poll for some condition and manually call a RETRY signal, then you shouldn't need to rely on retries set on the task decorator (which fire when a task run fails, rather than based on your condition), so I'd rewrite it to:
@task
def some_task():
    if (
        helpers.test_if_job_done() is False
        and prefect.context.get("task_run_count") < 200
    ):
        raise RETRY(
            "Work not done yet, retrying in 20 seconds.",
            start_time=pendulum.now().add(seconds=20),
        )
🙌 1
j

Jeremy Savage

05/11/2022, 12:46 PM
Ahh, awesome that makes perfect sense, cheers Anna. I'm guessing it is possible for me to do something like the below to ensure the task fails if the max retries is hit without completion of the work?
@task
def some_task():
    if helpers.test_if_job_done() is False:
        if prefect.context.get("task_run_count") < 200:
            raise RETRY(
                "Work not done yet, retrying in 20 seconds.",
                start_time=pendulum.now().add(seconds=20),
            )
        else:
            raise FAIL()
@Anna Geller
a

Anna Geller

05/11/2022, 1:09 PM
yes, 100% - this will ensure that you poll for some condition and if this wasn't satisfied after 200 times, then the task run will be marked as failed
❤️ 1