https://prefect.io logo
d

DiffyBron

12/11/2019, 9:30 AM
Hi, I'm trying to make the app say "hello world" when : 1. it is a weekday 2. at 8.15am, 9.30am, 3.50pm, 4pm 3. not during Christmas everyday. 4. In New York timezone Is the following code the right approach?
Copy code
#!/usr/bin/env python

import pendulum

from datetime import timedelta
from prefect import task, Flow, schedules
from prefect.schedules import filters, Schedule

@task
def say_hello():
    print("hello world")

if __name__ == '__main__':
    curr_schedule = Schedule(
        # Fire every min
        clocks=[schedules.clocks.IntervalClock(interval=timedelta(minutes=1), start_date=pendulum.datetime(2019, 1, 1, tz='America/New_York'))],
        # Only on weekdays
        filters=[filters.is_weekday],
        # and only at 8.15am, 9.30am, 3.50pm, 4pm
        or_filters=[
            filters.between_times(pendulum.time(hour=8, minute=15), pendulum.time(hour=8, minute=15)),
            filters.between_times(pendulum.time(hour=9, minute=30), pendulum.time(hour=9,minute=30)),
            filters.between_times(pendulum.time(hour=15, minute=50), pendulum.time(hour=15,minute=50)),
            filters.between_times(pendulum.time(hour=16), pendulum.time(hour=16)),
        ],
        # do not run on Christmas
        not_filters=[
            filters.between_dates(12, 8, 12, 25)
        ]
    )

    with Flow('Sounds alerts', curr_schedule) as flow:
        say_hello()

    flow.run()
j

josh

12/11/2019, 3:30 PM
From first glance I think you need to update the not filter to:
Copy code
not_filters=[
            filters.between_dates(12, 25, 12, 25)
        ]
๐Ÿ‘ 1
So itโ€™s only Christmas
d

DiffyBron

12/11/2019, 3:31 PM
oh yea .it's a typo ..
let me change that...the thing is when i run that code it runs only once even with the dates set correctly. I'm guessing it's probably the logic of my code that's incorrect and requires a second pair of eyes to look at it
i ran the code with the updated date for "X'mas" . It runs now. but ...it seems that it's not looking at the timezone which I've set ..
Copy code
import pendulum

from datetime import timedelta
from prefect import task, Flow, schedules
from prefect.schedules import filters, Schedule

@task
def say_hello():
    print("hello world")


if __name__ == '__main__':
    curr_schedule = Schedule(
        # Fire every min
        clocks=[schedules.clocks.IntervalClock(interval=timedelta(minutes=1), start_date=pendulum.datetime(2019, 1, 1, tz='America/New_York'))],
        # Only on weekdays
        filters=[filters.is_weekday],
        # and only at 8.15am, 9.30am, 3.50pm, 4pm
        or_filters=[
            filters.between_times(pendulum.time(hour=8, minute=15), pendulum.time(hour=8, minute=15)),
            filters.between_times(pendulum.time(hour=9, minute=30), pendulum.time(hour=9,minute=30)),
            filters.between_times(pendulum.time(hour=15, minute=50), pendulum.time(hour=15,minute=50)),
            filters.between_times(pendulum.time(hour=16), pendulum.time(hour=16)),
        ],
        # do not run on Christmas
        not_filters=[
            filters.between_dates(12, 25, 12, 25)
        ]
    )

    with Flow('Sounds alerts', curr_schedule) as flow:
        say_hello()

    flow.run()
It's just looking at my current timezone:
Copy code
[2019-12-11 15:32:39,689] INFO - prefect.Flow: Sounds alerts | Waiting for next scheduled run at 2019-12-11T15:50:00-05:00
oh okay..nevermind ..i'm looking at the time now..
gosh ...
j

josh

12/11/2019, 3:37 PM
Yeah haha I just ran it and it worked for me!
d

DiffyBron

12/11/2019, 3:37 PM
Thank you... i think you corrected the problem . I've also added
curr_schedule
earlier which could have fixed the issue.
Thank you Josh !
j

josh

12/11/2019, 3:37 PM
Anytime @DiffyBron!
๐Ÿ‘ 1
To follow up on this: hereโ€™s a PR which will simplify all of those between_times ๐Ÿ™‚ https://github.com/PrefectHQ/prefect/pull/1837
๐Ÿ‘ 1
d

DiffyBron

12/12/2019, 8:46 AM
looks good ..will wait for that merge
@josh,oh yea..i was testing it last night..it seems that the timezone change didn't work. .the code was still picking up my machine's localtime. Did you try to change your PC's timezone so it differs from the code?
j

josh

12/12/2019, 2:24 PM
Timezones work for me:
Copy code
start_date=pendulum.datetime(2019, 1, 1, tz='America/New_York')
Will give me
Copy code
Waiting for next scheduled run at 2019-12-12T09:30:00-05:00
and
Copy code
start_date=pendulum.datetime(2019, 1, 1, tz='Europe/Paris')
gives me
Copy code
Waiting for next scheduled run at 2019-12-12T16:00:00+01:00
d

DiffyBron

12/12/2019, 2:32 PM
i've just given it a test.. finally !! it worked..
it was probably me not testing it properly yesterday .. Thanks Josh !
๐Ÿ‘ 1
j

josh

12/12/2019, 2:33 PM
Anytime!
๐Ÿค 1