def test_create_cron_clock_with_invalid_cron_string_raises_error(self): with pytest.raises(Exception): clocks.CronClock("*") with pytest.raises(Exception): clocks.CronClock("hello world") with pytest.raises(Exception): clocks.CronClock(1) with pytest.raises(Exception): clocks.CronClock("* * 32 1 1")
def test_cron_clock_when_start_date_and_after_doesnt_align_with_schedule( self): every_day = "0 0 * * *" start_date = pendulum.datetime(2050, 1, 1, 6, 0, 0) c = clocks.CronClock(every_day, start_date=start_date) assert islice(c.events(after=pendulum.datetime(2050, 1, 1, 7, 0, 0)), 1) == [pendulum.datetime(2050, 1, 2, 0, 0, 0)]
def test_cron_clock_events(self): every_day = "0 0 * * *" c = clocks.CronClock(every_day) assert islice(c.events(), 3) == [ pendulum.today("UTC").add(days=1), pendulum.today("UTC").add(days=2), pendulum.today("UTC").add(days=3), ]
def test_cron_clock_end_date(self): every_day = "0 0 * * *" start_date = pendulum.datetime(2050, 1, 1) end_date = pendulum.datetime(2050, 1, 2) c = clocks.CronClock(every_day, start_date=start_date, end_date=end_date) assert islice(c.events(), 3) == [start_date, start_date.add(days=1)]
def test_cron_clock_end_daylight_savings_time(self): """ 11/4/2018, at 2am, America/New_York switched clocks back an hour. """ dt = pendulum.datetime(2018, 11, 4, tz="America/New_York") every_hour = "0 * * * *" c = clocks.CronClock(every_hour) next_4 = islice(c.events(after=dt), 4) assert [t.start_time.in_tz("UTC").hour for t in next_4] == [5, 6, 7, 8]
def test_cron_clock_start_daylight_savings_time(self): """ On 3/11/2018, at 2am, America/New_York switched clocks forward an hour. """ dt = pendulum.datetime(2018, 3, 11, tz="America/New_York") every_hour = "0 * * * *" c = clocks.CronClock(every_hour) next_4 = islice(c.events(after=dt), 4) assert [t.in_tz("UTC").hour for t in next_4] == [6, 7, 8, 9]
def test_cron_clock_events_with_after_argument(self): every_day = "0 0 * * *" c = clocks.CronClock(every_day) start_date = pendulum.datetime(2018, 1, 1) assert islice(c.events(after=start_date), 3) == [ start_date.add(days=1), start_date.add(days=2), start_date.add(days=3), ]
def test_cron_clock_events(self, labels): every_day = "0 0 * * *" c = clocks.CronClock(every_day, labels=labels) output = islice(c.events(), 3) assert all(isinstance(e, clocks.ClockEvent) for e in output) assert all(e.labels == labels for e in output) assert output == [ pendulum.today("UTC").add(days=1), pendulum.today("UTC").add(days=2), pendulum.today("UTC").add(days=3), ]
def test_cron_clock_hourly_daylight_savings_time_forward(self, serialize): """ On 3/11/2018, at 2am, America/New_York switched clocks forward an hour. """ dt = pendulum.datetime(2018, 3, 10, 23, tz="America/New_York") c = clocks.CronClock("0 * * * *", dt) if serialize: c = ClockSchema().load(ClockSchema().dump(c)) next_4 = islice(c.events(after=dt), 4) # skip 2am assert [t.in_tz("America/New_York").hour for t in next_4] == [0, 1, 3, 4] # constant hourly schedule in utc time assert [t.in_tz("UTC").hour for t in next_4] == [5, 6, 7, 8]
def test_with_clocks_with_different_timezones(): east = clocks.CronClock( "0 9 * * 1-5", start_date=pendulum.parse("2019-03-14", tz="US/Eastern") ) west = clocks.CronClock( "30 6 * * 1-5", start_date=pendulum.parse("2019-03-14", tz="US/Pacific") ) s = schedules.Schedule(clocks=[east, west]) after = pendulum.datetime(2019, 5, 1) next_east = list(itertools.islice(east.events(after=after), 3)) next_west = list(itertools.islice(west.events(after=after), 3)) expected = [ next_east[0], next_west[0], next_east[1], next_west[1], next_east[2], next_west[2], ] assert s.next(6, after) == expected
def test_cron_clock_events_with_day_or(self): """At first tuesday of each month. Check if cron work as expected using day_or parameter. """ every_day = "0 0 1-7 * 2" c = clocks.CronClock(every_day, day_or=False) output = islice(c.events(), 3) first_event, next_events = output[0], output[1:] # one event every month assert [event.start_time.month for event in next_events] == [ first_event.start_time.add(months=1).month, first_event.start_time.add(months=2).month, ]
def test_cron_clock_events(self, params): every_day = "0 0 * * *" c = clocks.CronClock(every_day, parameter_defaults=params) output = islice(c.events(), 3) assert all([isinstance(e, clocks.ClockEvent) for e in output]) assert all([e.parameter_defaults == params for e in output]) assert output == [ pendulum.today("UTC").add(days=1), pendulum.today("UTC").add(days=2), pendulum.today("UTC").add(days=3), ]
def test_cron_clock_hourly_daylight_savings_time_backward(self, serialize): """ 11/4/2018, at 2am, America/New_York switched clocks back an hour. """ dt = pendulum.datetime(2018, 11, 3, 23, tz="America/New_York") c = clocks.CronClock("0 * * * *", dt) if serialize: c = ClockSchema().load(ClockSchema().dump(c)) next_4 = islice(c.events(after=dt), 4) # repeat the 1am run in local time assert [t.in_tz("America/New_York").hour for t in next_4] == [0, 1, 2, 3] # runs every hour UTC assert [t.in_tz("UTC").hour for t in next_4] == [4, 6, 7, 8]
def test_cron_clock_daily_start_daylight_savings_time_backward(self, serialize): """ On 11/4/2018, at 2am, America/New_York switched clocks back an hour. Confirm that a schedule for 9am America/New_York stays 9am through the switch. """ dt = pendulum.datetime(2018, 11, 1, 9, tz="America/New_York") c = clocks.CronClock("0 9 * * *", dt) if serialize: c = ClockSchema().load(ClockSchema().dump(c)) next_4 = islice(c.events(after=dt), 4) # constant 9am start assert [t.in_tz("America/New_York").hour for t in next_4] == [9, 9, 9, 9] assert [t.in_tz("UTC").hour for t in next_4] == [13, 13, 14, 14]
def test_serialize_schedule_with_labels(): dt = pendulum.datetime(2099, 1, 1) s = schedules.Schedule(clocks=[ clocks.IntervalClock(timedelta(hours=1), labels=["dev"]), clocks.CronClock("0 8 * * *", labels=["prod"]), ]) s2 = serialize_and_deserialize(s) assert s2.clocks[0].labels == ["dev"] assert s2.clocks[1].labels == ["prod"] output = s2.next(3, after=dt, return_events=True) assert all(isinstance(e, clocks.ClockEvent) for e in output)
def test_serialize_schedule_with_parameters(): dt = pendulum.datetime(2099, 1, 1) s = schedules.Schedule( clocks=[ clocks.IntervalClock(timedelta(hours=1), parameter_defaults=dict(x=42)), clocks.CronClock("0 8 * * *", parameter_defaults=dict(y=99)), ] ) s2 = serialize_and_deserialize(s) assert s2.clocks[0].parameter_defaults == dict(x=42) assert s2.clocks[1].parameter_defaults == dict(y=99) output = s2.next(3, after=dt, return_events=True)
def test_create_cron_clock_with_labels(self): c = clocks.CronClock("* * * * *", labels=["dev", "foo"]) assert c.labels == ["dev", "foo"]
def test_cron_clock_respects_microseconds(self): every_day = "0 0 * * *" start_date = pendulum.datetime(2050, 1, 1, 0, 0, 0, 1) c = clocks.CronClock(every_day, start_date=start_date) assert islice(c.events(), 1) == [pendulum.datetime(2050, 1, 2)]
from prefect import task, Flow, Task, Parameter from prefect.schedules import clocks, Schedule diurnal = ['rooster', 'dog'] nocturnal = ['owl', 'hampster'] # Clocks diurnal_clock = clocks.CronClock("51 * * * *", parameter_defaults={"animals": diurnal}) nocturnal_clock = clocks.CronClock("53 * * * *", parameter_defaults={"animals": nocturnal}) # the full schedule schedule = Schedule(clocks=[diurnal_clock, nocturnal_clock]) @task def wakeup(animals): for item in animals: print("Waking up animal %s" % item) # Flow is common to both types, though with different schedules. with Flow(name="wakuptime", schedule=schedule) as this_flow: animals = Parameter("animals", default=[]) wakeup(animals) # will run on the schedule with varying parameter values this_flow.register("Teste")
def test_create_cron_clock(self): assert clocks.CronClock("* * * * *")
def test_create_cron_clock(self): c = clocks.CronClock("* * * * *") assert c.parameter_defaults == dict() assert c.labels is None
def test_create_cron_clock_with_parameters(self): c = clocks.CronClock("* * * * *", parameter_defaults=dict(x=42)) assert c.parameter_defaults == dict(x=42)
import prefect from prefect import task, Flow, Parameter from prefect.schedules import clocks, Schedule from prefect.environments.storage import GitHub @task def hello_world(): print("Hello, World!") clock = clocks.CronClock("0 0 * * *") schedule = Schedule(clocks=[clock]) with Flow("Star GitHub Repositories", schedule=schedule) as flow: hello_world() flow.storage = GitHub( repo="znicholasbrown/star-repos", path="app.py", secrets=["GITHUB_AUTH_TOKEN" ], # Change this to your own GitHub auth token secret ) flow.register(project_name="SOME PROJECT") # flow.run()
def test_create_cron_clock_with_day_or(self, input_day_or, expected_day_or): c = clocks.CronClock("* * * * *", day_or=input_day_or) assert c.day_or is expected_day_or