<@ULVA73B9P> can we have custom _retry_delay_sec...
# ask-marvin
m
@Marvin can we have custom _retry_delay_seconds_ passing function details - i want to decide _retry_delay_seconds_ based on time of the day can it be possible
m
In Prefect workflows, you generally have control over how retries are managed for tasks that may fail. You can specify a retry delay as well as a custom backoff function that determines the delay between retries based on the number of previous attempts or other logic, like the time of day. As of my knowledge cutoff in April 2023, you can define a custom function and use it for the
retry_delay
parameter when setting up your tasks. However, since Prefect 2.x might have differences compared to earlier versions, I'd recommend checking the latest Prefect documentation or GitHub issues for any updates or examples specific to the version you're working with. Here's a simplified example of how you could define a function to control retry delay based on the time of day:
Copy code
python
from datetime import datetime, timedelta
import random

def custom_retry_delay(attempt: int):
    # Get the current hour
    current_hour = datetime.now().hour
    
    # Define different delay strategies for different times of the day
    if 0 <= current_hour < 6:
        # Between 00:00 and 06:00, retry after a fixed delay
        delay_seconds = 300  # e.g., 5 minutes
    elif 6 <= current_hour < 12:
        # Between 06:00 and 12:00, increase the delay with each attempt
        delay_seconds = attempt * 60
    else:
        # Otherwise, use a random delay for example
        delay_seconds = random.randint(60, 180)
    
    return timedelta(seconds=delay_seconds)

# Then, use this function when defining your task
@task(retry_delay=custom_retry_delay, max_retries=3)
def my_task():
    # Your task implementation here
    pass
Remember to refer to the latest Prefect documentation or community resources to ensure that this approach is valid and that any recent changes in the Prefect framework are taken into account. If you're implementing something especially complex or unconventional, you might run into edge cases or limitations that aren't immediately obvious, which is just life, I suppose.
m
Thanks seems to be helpful, let me try