Hi, we're trying to switch from a lot of small job...
# ask-community
e
Hi, we're trying to switch from a lot of small job (60~100 launch per minute) to long jobs that run a lot of sub tasks to reduce the load on postgress (almost 300 deployments) The code under is working fine but we'd like to improve it (launching for 4 hours every 4 hours) Is there any way to get the schedule from inside a flow ? This way if the schedule is changed in the UI the flow will adapt it's run time (using prefect V3)
Copy code
def get_next_cron_run_time(current: datetime, interval_hours: int) -> datetime:
    """
    Calculates the next scheduled run time rounded to an exact multiple of the interval.
    Input:
      current: datetime - The current time.
      interval_hours: int - The interval in hours between runs.
    Output:
      A datetime object representing the next run time.
    """
    floor_hour = current.hour - (current.hour % interval_hours)
    base_time = current.replace(hour=floor_hour, minute=0, second=0, microsecond=0)
    next_run = base_time + timedelta(hours=interval_hours)
    if next_run <= current:
        next_run += timedelta(hours=interval_hours)
    return next_run  

@flow
def example():
    run_start = datetime.now()
    interval_hours = 4
    next_run = get_next_cron_run_time(run_start, interval_hours)
    run_end = next_run - timedelta(minutes=1)
    print(f"[{datetime.now()}] Flow will run until: {run_end}")
    current_time = datetime.now()
    while current_time < run_end:
      # some code