Skip to content

Possible to give a task access to how many times it's been retried? #5193

Answered by anna-geller
itsayellow asked this question in Q&A
Discussion options

You must be logged in to vote

@itsayellow you can retrieve the number of retries by using the task_run_count from the context and subtracting 1 (the initial run which was not a retry). You could then use it e.g. in a state handler.

Here is a sample flow you could use to test that:

from datetime import timedelta
import prefect
from prefect import Flow, task


@task(max_retries=2, retry_delay=timedelta(seconds=2))
def retry_test():
    logger = prefect.context.get("logger")
    run_count = prefect.context.get("task_run_count")
    logger.info("%s. TaskRun", run_count)
    raise Exception("Failing to test retries...")


with Flow("retry-tester") as flow:
    retry_test()

if __name__ == "__main__":
    flow.run()

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@itsayellow
Comment options

@itsayellow
Comment options

Answer selected by itsayellow
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants