How to reset schedules affected by Daylight Savings time

The Problem

Some users may experience issues with schedules when shifting to and from Daylight Savings time, as a result users may end up with duplicate runs for the same day and all around unpleasantness, this mostly occurs with cron schedules with a solid explanation post here on this stack overflow page dst - Daylight Savings and Cron - Stack Overflow

If you end up in the is situation with prefect the easiest way to address it is by resetting your schedules you can do this manually or if you wanted to do so for all of your currently active schedule you can run the following.

Prefect 1:

from prefect.client import Client
import time
​
client = Client(api_key="CLOUD1_API_KEY")
​
flows = client.graphql(query="""{
  flow (
    where: {
      is_schedule_active: {_eq: true}}
  ) {
    name
    id
  }
}""")
​
flows_list = flows['data']['flow']
​
print(flows_list)
​
for flow in flows_list:
​
    flow_id = flow['id']
​
    disable_schedule = client.graphql(
        query=f"""mutation {{
  set_schedule_inactive(input: {{flow_id: "{flow_id}"}})
  {{
    success
    __typename
  }}
}}"""
        )
​
    print(disable_schedule)
​
    enable_schedule = client.graphql(query=f"""mutation {{
  set_schedule_active(input: {{flow_id: "{flow_id}"}})
  {{
    success
    __typename
  }}
}}""")
​
    print(enable_schedule)

Prefect 2:

import asyncio
from prefect import get_client
from prefect.orion.schemas.filters import DeploymentFilter, DeploymentFilterIsScheduleActive

async def reset_depoloyment_schedules():
    filter = DeploymentFilter(is_schedule_active=DeploymentFilterIsScheduleActive(eq_ = True))

    client = get_client()
    active_deployments = await client.read_deployments(deployment_filter=filter)
    # deployments_dict = active_deployments.dict()

    for d in active_deployments:
        deployment_dict = d.dict()
        if deployment_dict['schedule'] == None:
            print("No Existing Schedule")
        else:
            disable_schedule = await client.update_deployment(deployment=d, is_schedule_active=False)

            print(disable_schedule)

            enable_schedule = await client.update_deployment(deployment=d, is_schedule_active=True)

            print(enable_schedule)

asyncio.run(reset_depoloyment_schedules())
1 Like