Skip to content

Commit

Permalink
Adds example of returning a state
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilRex committed Nov 22, 2024
1 parent ed43cbd commit dbbd697
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions basics/retry-after.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Demonstrates directly returning a state. In this case, an AwaitingRetry state
which causes a task to be retried after a delay.
"""

from datetime import datetime, timedelta, UTC

from prefect import task, get_run_logger
from prefect.context import get_run_context
from prefect.states import AwaitingRetry


@task
def main():
get_run_logger().info("Running main task")

# In practice we might fetch a URL that is temporarily unavailable
# and retry after a number of seconds specified by a Retry-After header
retry_after = 10

# Break out of the retry loop after the first run
ctx = get_run_context()
if ctx.task_run.run_count > 1:
return

# Schedule the task to run again in {retry_after} seconds
return AwaitingRetry(
scheduled_time=datetime.now(UTC) + timedelta(seconds=retry_after)
)


if __name__ == "__main__":
main()

0 comments on commit dbbd697

Please sign in to comment.