https://prefect.io logo
v

Varun Joshi

04/14/2021, 8:33 AM
Hey Prefecters, I don't want my flows to run between 2 AM to 10 AM from Mondays to Thursdays and 2 AM to 8 AM from Fridays to Sundays. I'm using the filters module provided by prefect for this. Assuming that I can do this,
Copy code
from prefect.schedules import IntervalSchedule, filters
I made the below change in my code
Copy code
ist = pendulum.timezone('Asia/Calcutta')
schedule = Schedule(
    clocks=[IntervalClock(interval=datetime.timedelta(minutes=1))],
    filters = filters.between_times(pendulum.time(6),pendulum.time(10))
However, this doesn't seem to be working. Could someone please guide me on how to import filters and apply them to my schedule? Thank you, Varun
k

Kevin Kho

04/14/2021, 1:52 PM
Hey @Varun Joshi! Schedule takes a list of clocks. Maybe it’s easier to pass a list of clocks.
Copy code
from prefect.schedules.clocks import CronClock

week = CronClock(cron="0 6-18 * * 1-5")
weekend = CronClock(cron="0 6-18/4 * * 0,6")

clocks = [week, weekend]
schedule = Schedule(clocks=clocks)
v

Varun Joshi

04/14/2021, 3:12 PM
Great @Kevin Kho! Thanks a lot!
Hey @Kevin Kho, where do I assign the start_date in this?
k

Kevin Kho

04/14/2021, 4:05 PM
Copy code
clock1   = clocks.IntervalClock(start_date=now, 
                                interval=datetime.timedelta(minutes=1), 
                                parameter_defaults={"p": "CLOCK 1"})
Make two IntervalClocks and combine them to a schedule.
🙌 1
v

Varun Joshi

04/14/2021, 4:11 PM
So now if I have to run flows between 10 AM to 2 AM on weekdays and 8 AM to 2 AM on weekends at a frequency of 3 mins, this will work right?
Copy code
from prefect.schedules.clocks import CronClock
week = CronClock(cron="3 6-18 * * 1-5",start_date=datetime.datetime.now())
weekend = CronClock(cron="3 6-18/4 * * 0,6")

clocks = [week, weekend]
schedule = Schedule(clocks=clocks)
k

Kevin Kho

04/14/2021, 4:17 PM
I think you can’t do 10AM to 2AM with cron. You need to split that out to 10AM to midnight and then midnight to 2AM.
Have you seen this tool: https://crontab.guru/ . It’s very helpful
v

Varun Joshi

04/14/2021, 4:18 PM
Yes, I was working based on that itself.
Thanks for the quick help. Really appreciate it 🙂
👍 1
7 Views