def test_end_date(self, timezone):
        end_date = timezone.localize(datetime(2014, 4, 13, 3))
        trigger = CronTrigger(year=2014, hour=4, end_date=end_date)

        start_date = timezone.localize(datetime(2014, 4, 13, 2, 30))
        assert trigger.get_next_fire_time(None, start_date - timedelta(1)) == \
            start_date.replace(day=12, hour=4, minute=0)
        assert trigger.get_next_fire_time(None, start_date) is None
Beispiel #2
0
    def test_end_date(self, timezone):
        end_date = timezone.localize(datetime(2014, 4, 13, 3))
        trigger = CronTrigger(year=2014, hour=4, end_date=end_date)

        start_date = timezone.localize(datetime(2014, 4, 13, 2, 30))
        assert trigger.get_next_fire_time(None, start_date - timedelta(1)) == \
            start_date.replace(day=12, hour=4, minute=0)
        assert trigger.get_next_fire_time(None, start_date) is None
Beispiel #3
0
 def test_week_1(self, timezone):
     trigger = CronTrigger(year=2009, month=2, week=8, timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009', month='2', week='8')>"
     assert str(trigger) == "cron[year='2009', month='2', week='8']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 2, 16))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #4
0
 def test_next_fire_time(self):
     trigger_obj2 = CronTrigger(second='5')
     tz_obj = pytz.timezone('UTC')
     curr = datetime.datetime.now(tz_obj)
     next_dt = trigger_obj2.get_next_fire_time(None, curr)
     print next_dt
     assert next_dt == 0
Beispiel #5
0
 def test_previous_fire_time_3(self, timezone):
     trigger = CronTrigger(day="*", timezone=timezone)
     previous_fire_time = timezone.localize(datetime(2016, 4, 25))
     now = timezone.localize(datetime(2016, 4, 25))
     correct_next_date = timezone.localize(datetime(2016, 4, 26))
     assert trigger.get_next_fire_time(previous_fire_time,
                                       now) == correct_next_date
 def test_cron_year_list(self, timezone):
     trigger = CronTrigger(year='2009,2008', timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009,2008', timezone='Europe/Berlin')>"
     assert str(trigger) == "cron[year='2009,2008']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 1, 1))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
 def test_previous_fire_time_1(self, timezone):
     """Test for previous_fire_time arg in get_next_fire_time()"""
     trigger = CronTrigger(day="*", timezone=timezone)
     previous_fire_time = timezone.localize(datetime(2015, 11, 23))
     now = timezone.localize(datetime(2015, 11, 26))
     correct_next_date = timezone.localize(datetime(2015, 11, 24))
     assert trigger.get_next_fire_time(previous_fire_time, now) == correct_next_date
 def test_cron_trigger_4(self, timezone):
     trigger = CronTrigger(year='2012', month='2', day='last', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2012', month='2', day='last', "
                              "timezone='Europe/Berlin')>")
     start_date = timezone.localize(datetime(2012, 2, 1))
     correct_next_date = timezone.localize(datetime(2012, 2, 29))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #9
0
 def test_week_2(self, timezone):
     trigger = CronTrigger(year=2009, week=15, day_of_week=2, timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009', week='15', day_of_week='2')>"
     assert str(trigger) == "cron[year='2009', week='15', day_of_week='2']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 4, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #10
0
 def test_cron_trigger_1(self, timezone):
     trigger = CronTrigger(year="2009/2", month="1/3", day="5-13", timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009/2', month='1/3', day='5-13')>"
     assert str(trigger) == "cron[year='2009/2', month='1/3', day='5-13']"
     start_date = timezone.localize(datetime(2008, 12, 1))
     correct_next_date = timezone.localize(datetime(2009, 1, 5))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #11
0
 def test_cron_trigger_3(self, timezone):
     trigger = CronTrigger(year='2009', month='feb-dec', hour='8-10', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', month='feb-dec', hour='8-10', "
                              "timezone='Europe/Berlin')>")
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 2, 1, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #12
0
 def test_cron_weekday_positional(self, timezone):
     trigger = CronTrigger(year=2009, month=1, day="4th wed", timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009', month='1', day='4th wed')>"
     assert str(trigger) == "cron[year='2009', month='1', day='4th wed']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 1, 28))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #13
0
 def test_cron_weekday_nomatch(self, timezone):
     trigger = CronTrigger(year=2009, month=1, day="6-10", day_of_week="0,6", timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009', month='1', day='6-10', day_of_week='0,6')>"
     assert str(trigger) == "cron[year='2009', month='1', day='6-10', day_of_week='0,6']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = None
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
 def test_cron_trigger_4(self, timezone):
     trigger = CronTrigger(year='2012', month='2', day='last', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2012', month='2', day='last', "
                              "timezone='Europe/Berlin')>")
     start_date = timezone.localize(datetime(2012, 2, 1))
     correct_next_date = timezone.localize(datetime(2012, 2, 29))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
 def test_cron_trigger_3(self, timezone):
     trigger = CronTrigger(year='2009', month='2', hour='8-10', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', month='2', hour='8-10', "
                              "timezone='Europe/Berlin')>")
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 2, 1, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
 def test_previous_fire_time_1(self, timezone):
     """Test for previous_fire_time arg in get_next_fire_time()"""
     trigger = CronTrigger(day="*", timezone=timezone)
     previous_fire_time = timezone.localize(datetime(2015, 11, 23))
     now = timezone.localize(datetime(2015, 11, 26))
     correct_next_date = timezone.localize(datetime(2015, 11, 24))
     assert trigger.get_next_fire_time(previous_fire_time, now) == correct_next_date
 def test_cron_year_list(self, timezone):
     trigger = CronTrigger(year='2009,2008', timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009,2008', timezone='Europe/Berlin')>"
     assert str(trigger) == "cron[year='2009,2008']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 1, 1))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #18
0
    def add_random_tasks(self, module_name, cls_name, function_name, start_hour, end_hour, day_of_week, num_times_per_day, ignore_scheduler_lock=False):
        # This function is fired at startup, and every day at midnight.
        if end_hour < start_hour:
            raise Exception("start_hour is after end_hour!")

        # Get the next appropriate date
        now = datetime.datetime.now()

        # Work around crontab bug where if the hour has started, it's skipped.
        adjusted_start_hour = start_hour
        if adjusted_start_hour != 23:
            adjusted_start_hour += 1    
        adjusted_start_hour = "%s" % adjusted_start_hour

        ct = CronTrigger(hour=adjusted_start_hour, day_of_week=day_of_week)
        fire_time = ct.get_next_fire_time(now)

        # If it's today, schedule it. Otherwise, it'll be scheduled at midnight of its run day.
        if fire_time.day == now.day:

            # There are more efficient ways to do this, but this supports almost any n for num_times_per_day,
            # and that seems more useful.
            possible_times = []
            for i in range(start_hour, end_hour):
                for j in range(60):
                    possible_times.append((i,j))

            times = random.sample(possible_times, num_times_per_day)
            for hour, minute in times:
                when = datetime.datetime(now.year, now.month, now.day, hour, minute)

                # If we're starting up mid-day, this may not be true.
                if when >= now:
                    self.add_single_random_task(when, module_name, cls_name, function_name, start_hour, end_hour, day_of_week, num_times_per_day, ignore_scheduler_lock=ignore_scheduler_lock)
Beispiel #19
0
 def test_week_2(self, timezone):
     trigger = CronTrigger(year=2009, week=15, day_of_week=2, timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', week='15', day_of_week='2', "
                              "timezone='Europe/Berlin')>")
     assert str(trigger) == "cron[year='2009', week='15', day_of_week='2']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 4, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #20
0
 def test_cron_extra_coverage(self, timezone):
     # This test has no value other than patching holes in test coverage
     trigger = CronTrigger(day='6,8', timezone=timezone)
     assert repr(trigger) == "<CronTrigger (day='6,8', timezone='Europe/Berlin')>"
     assert str(trigger) == "cron[day='6,8']"
     start_date = timezone.localize(datetime(2009, 12, 31))
     correct_next_date = timezone.localize(datetime(2010, 1, 6))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #21
0
 def test_week_1(self, timezone):
     trigger = CronTrigger(year=2009, month=2, week=8, timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', month='2', week='8', "
                              "timezone='Europe/Berlin')>")
     assert str(trigger) == "cron[year='2009', month='2', week='8']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 2, 16))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #22
0
 def test_cron_weekday_positional(self, timezone):
     trigger = CronTrigger(year=2009, month=1, day='4th wed', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', month='1', day='4th wed', "
                              "timezone='Europe/Berlin')>")
     assert str(trigger) == "cron[year='2009', month='1', day='4th wed']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 1, 28))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #23
0
 def test_cron_weekday_nomatch(self, timezone):
     trigger = CronTrigger(year=2009, month=1, day='6-10', day_of_week='0,6', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', month='1', day='6-10', "
                              "day_of_week='0,6', timezone='Europe/Berlin')>")
     assert str(trigger) == "cron[year='2009', month='1', day='6-10', day_of_week='0,6']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = None
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #24
0
 def test_cron_trigger_1(self, timezone):
     trigger = CronTrigger(year='2009/2', month='1/3', day='5-13', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009/2', month='1/3', day='5-13', "
                              "timezone='Europe/Berlin')>")
     assert str(trigger) == "cron[year='2009/2', month='1/3', day='5-13']"
     start_date = timezone.localize(datetime(2008, 12, 1))
     correct_next_date = timezone.localize(datetime(2009, 1, 5))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
 def test_cron_weekday_overlap(self, timezone):
     trigger = CronTrigger(year=2009, month=1, day='6-10', day_of_week='2-4', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', month='1', day='6-10', "
                              "day_of_week='2-4', timezone='Europe/Berlin')>")
     assert str(trigger) == "cron[year='2009', month='1', day='6-10', day_of_week='2-4']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 1, 7))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #26
0
 def test_different_tz(self, timezone):
     alter_tz = pytz.FixedOffset(-600)
     trigger = CronTrigger(year=2009, week=15, day_of_week=2, timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009', week='15', day_of_week='2')>"
     assert str(trigger) == "cron[year='2009', week='15', day_of_week='2']"
     start_date = alter_tz.localize(datetime(2008, 12, 31, 22))
     correct_next_date = timezone.localize(datetime(2009, 4, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #27
0
    def test_dst_change(self):
        """
        Making sure that CronTrigger works correctly when crossing the DST switch threshold.
        Note that you should explicitly compare datetimes as strings to avoid the internal datetime comparison which
        would test for equality in the UTC timezone.
        """

        eastern = pytz.timezone("US/Eastern")
        trigger = CronTrigger(minute="*/30", timezone=eastern)

        datetime_edt = eastern.localize(datetime(2013, 11, 3, 1, 5), is_dst=True)
        correct_next_date = eastern.localize(datetime(2013, 11, 3, 1, 30), is_dst=True)
        assert str(trigger.get_next_fire_time(None, datetime_edt)) == str(correct_next_date)

        datetime_edt = eastern.localize(datetime(2013, 11, 3, 1, 35), is_dst=True)
        correct_next_date = eastern.localize(datetime(2013, 11, 3, 1), is_dst=False)
        assert str(trigger.get_next_fire_time(None, datetime_edt)) == str(correct_next_date)
Beispiel #28
0
 def test_cron_start_date(self, timezone):
     trigger = CronTrigger(year='2009', month='2', hour='8-10', start_date='2009-02-03 11:00:00', timezone=timezone)
     assert repr(trigger) == \
         "<CronTrigger (year='2009', month='2', hour='8-10', start_date='2009-02-03 11:00:00 CET')>"
     assert str(trigger) == "cron[year='2009', month='2', hour='8-10']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 2, 4, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
 def test_cron_extra_coverage(self, timezone):
     # This test has no value other than patching holes in test coverage
     trigger = CronTrigger(day='6,8', timezone=timezone)
     assert repr(trigger) == "<CronTrigger (day='6,8', timezone='Europe/Berlin')>"
     assert str(trigger) == "cron[day='6,8']"
     start_date = timezone.localize(datetime(2009, 12, 31))
     correct_next_date = timezone.localize(datetime(2010, 1, 6))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #30
0
    def test_jitter_with_end_date(self, timezone):
        now = timezone.localize(datetime(2017, 11, 12, 6, 55, 30))
        end_date = timezone.localize(datetime(2017, 11, 12, 6, 56, 0))
        trigger = CronTrigger(minute='*', jitter=5, end_date=end_date)

        for _ in range(0, 100):
            next_fire_time = trigger.get_next_fire_time(None, now)
            assert next_fire_time is None or next_fire_time <= end_date
Beispiel #31
0
    def test_jitter_dst_change(self, trigger_args, start_date, start_date_dst, correct_next_date):
        timezone = pytz.timezone('US/Eastern')
        trigger = CronTrigger(timezone=timezone, jitter=5, **trigger_args)
        start_date = timezone.localize(start_date, is_dst=start_date_dst)
        correct_next_date = timezone.localize(correct_next_date, is_dst=not start_date_dst)

        for _ in range(0, 100):
            next_fire_time = trigger.get_next_fire_time(None, start_date)
            assert abs(next_fire_time - correct_next_date) <= timedelta(seconds=5)
Beispiel #32
0
 def test_cron_trigger_2(self, timezone):
     trigger = CronTrigger(year='2009/2',
                           month='1/3',
                           day='5-13',
                           timezone=timezone)
     start_date = timezone.localize(datetime(2009, 10, 14))
     correct_next_date = timezone.localize(datetime(2011, 1, 5))
     assert trigger.get_next_fire_time(None,
                                       start_date) == correct_next_date
Beispiel #33
0
 def test_different_tz(self, timezone):
     alter_tz = pytz.FixedOffset(-600)
     trigger = CronTrigger(year=2009, week=15, day_of_week=2, timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', week='15', day_of_week='2', "
                              "timezone='Europe/Berlin')>")
     assert str(trigger) == "cron[year='2009', week='15', day_of_week='2']"
     start_date = alter_tz.localize(datetime(2008, 12, 31, 22))
     correct_next_date = timezone.localize(datetime(2009, 4, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #34
0
 def test_jitter_with_timezone(self, timezone):
     est = pytz.FixedOffset(-300)
     cst = pytz.FixedOffset(-360)
     trigger = CronTrigger(hour=11, minute='*/5', timezone=est, jitter=5)
     start_date = cst.localize(datetime(2009, 9, 26, 10, 16))
     correct_next_date = est.localize(datetime(2009, 9, 26, 11, 20))
     for _ in range(0, 100):
         assert abs(trigger.get_next_fire_time(None, start_date) -
                    correct_next_date) <= timedelta(seconds=5)
Beispiel #35
0
 def test_cron_start_date(self, timezone):
     trigger = CronTrigger(year='2009', month='2', hour='8-10',
                           start_date='2009-02-03 11:00:00', timezone=timezone)
     assert repr(trigger) == ("<CronTrigger (year='2009', month='2', hour='8-10', "
                              "start_date='2009-02-03 11:00:00 CET', "
                              "timezone='Europe/Berlin')>")
     assert str(trigger) == "cron[year='2009', month='2', hour='8-10']"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 2, 4, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #36
0
 def test_cron_offset_nextmonth_2(self, timezone):
     trigger = CronTrigger(year='*',
                           day=28,
                           hour=6,
                           timezone=timezone,
                           offset=timedelta(days=2, hours=2))
     start_date = timezone.localize(datetime(2016, 3, 1, 8, 0, 1))
     prev_fire_date = timezone.localize(datetime(2016, 3, 1, 8))
     correct_next_date = timezone.localize(datetime(2016, 3, 30, 8))
     assert trigger.get_next_fire_time(prev_fire_date,
                                       start_date) == correct_next_date
Beispiel #37
0
    def test_jitter_produces_differrent_valid_results(self, timezone):
        trigger = CronTrigger(minute='*', jitter=5)
        now = timezone.localize(datetime(2017, 11, 12, 6, 55, 30))

        results = set()
        for _ in range(0, 100):
            next_fire_time = trigger.get_next_fire_time(None, now)
            results.add(next_fire_time)
            assert timedelta(seconds=25) <= (next_fire_time - now) <= timedelta(seconds=35)

        assert 1 < len(results)
Beispiel #38
0
    def test_cron_increment_weekday(self, timezone):
        """
        Tests that incrementing the weekday field in the process of calculating the next matching
        date won't cause problems.

        """
        trigger = CronTrigger(hour='5-6', timezone=timezone)
        assert repr(trigger) == "<CronTrigger (hour='5-6', timezone='Europe/Berlin')>"
        assert str(trigger) == "cron[hour='5-6']"
        start_date = timezone.localize(datetime(2009, 9, 25, 7))
        correct_next_date = timezone.localize(datetime(2009, 9, 26, 5))
        assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #39
0
 def test_cron_offset_negative_2(self, timezone):
     trigger = CronTrigger(year='*',
                           month='*',
                           day=1,
                           hour=6,
                           timezone=timezone,
                           offset=-86400)
     start_date = timezone.localize(datetime(2015, 12, 31, 6, 0, 1))
     prev_fire_date = timezone.localize(datetime(2015, 12, 31, 6))
     correct_next_date = timezone.localize(datetime(2016, 1, 31, 6))
     assert trigger.get_next_fire_time(prev_fire_date,
                                       start_date) == correct_next_date
    def test_dst_change(self, trigger_args, start_date, start_date_dst, correct_next_date):
        """
        Making sure that CronTrigger works correctly when crossing the DST switch threshold.
        Note that you should explicitly compare datetimes as strings to avoid the internal datetime
        comparison which would test for equality in the UTC timezone.

        """
        timezone = pytz.timezone('US/Eastern')
        trigger = CronTrigger(timezone=timezone, **trigger_args)
        start_date = timezone.localize(start_date, is_dst=start_date_dst)
        correct_next_date = timezone.localize(correct_next_date, is_dst=not start_date_dst)
        assert str(trigger.get_next_fire_time(None, start_date)) == str(correct_next_date)
Beispiel #41
0
    def add_random_tasks(self,
                         module_name,
                         cls_name,
                         function_name,
                         start_hour,
                         end_hour,
                         day_of_week,
                         num_times_per_day,
                         ignore_scheduler_lock=False):
        # This function is fired at startup, and every day at midnight.
        if end_hour < start_hour:
            raise Exception("start_hour is after end_hour!")

        # Get the next appropriate date
        now = pytz_now()

        # Work around crontab bug where if the hour has started, it's skipped.
        adjusted_start_hour = start_hour
        if adjusted_start_hour != 23:
            adjusted_start_hour += 1
        adjusted_start_hour = "%s" % adjusted_start_hour

        ct = CronTrigger(hour=adjusted_start_hour, day_of_week=day_of_week)
        fire_time = ct.get_next_fire_time(None, now)

        # If it's today, schedule it. Otherwise, it'll be scheduled at midnight of its run day.
        if fire_time.day == now.day:

            # There are more efficient ways to do this, but this supports almost any n for num_times_per_day,
            # and that seems more useful.
            possible_times = []
            for i in range(start_hour, end_hour):
                for j in range(60):
                    possible_times.append((i, j))

            times = random.sample(possible_times, num_times_per_day)
            for hour, minute in times:
                when = pytz_datetime(
                    datetime.datetime(now.year, now.month, now.day, hour,
                                      minute))

                # If we're starting up mid-day, this may not be true.
                if when >= now:
                    self.add_single_random_task(
                        when,
                        module_name,
                        cls_name,
                        function_name,
                        start_hour,
                        end_hour,
                        day_of_week,
                        num_times_per_day,
                        ignore_scheduler_lock=ignore_scheduler_lock)
Beispiel #42
0
    def test_dst_change(self, trigger_args, start_date, start_date_dst, correct_next_date):
        """
        Making sure that CronTrigger works correctly when crossing the DST switch threshold.
        Note that you should explicitly compare datetimes as strings to avoid the internal datetime
        comparison which would test for equality in the UTC timezone.

        """
        timezone = pytz.timezone('US/Eastern')
        trigger = CronTrigger(timezone=timezone, **trigger_args)
        start_date = timezone.localize(start_date, is_dst=start_date_dst)
        correct_next_date = timezone.localize(correct_next_date, is_dst=not start_date_dst)
        assert str(trigger.get_next_fire_time(None, start_date)) == str(correct_next_date)
Beispiel #43
0
    def test_timezone_change(self, timezone):
        """
        Ensure that get_next_fire_time method returns datetimes in the timezone of the trigger and
        not in the timezone of the passed in start_date.

        """
        est = pytz.FixedOffset(-300)
        cst = pytz.FixedOffset(-360)
        trigger = CronTrigger(hour=11, minute='*/5', timezone=est)
        start_date = cst.localize(datetime(2009, 9, 26, 10, 16))
        correct_next_date = est.localize(datetime(2009, 9, 26, 11, 20))
        assert str(trigger.get_next_fire_time(None, start_date)) == str(correct_next_date)
    def test_timezone_change(self, timezone):
        """
        Ensure that get_next_fire_time method returns datetimes in the timezone of the trigger and
        not in the timezone of the passed in start_date.

        """
        est = pytz.FixedOffset(-300)
        cst = pytz.FixedOffset(-360)
        trigger = CronTrigger(hour=11, minute='*/5', timezone=est)
        start_date = cst.localize(datetime(2009, 9, 26, 10, 16))
        correct_next_date = est.localize(datetime(2009, 9, 26, 11, 20))
        assert str(trigger.get_next_fire_time(None, start_date)) == str(correct_next_date)
    def test_cron_increment_weekday(self, timezone):
        """
        Tests that incrementing the weekday field in the process of calculating the next matching
        date won't cause problems.

        """
        trigger = CronTrigger(hour='5-6', timezone=timezone)
        assert repr(trigger) == "<CronTrigger (hour='5-6', timezone='Europe/Berlin')>"
        assert str(trigger) == "cron[hour='5-6']"
        start_date = timezone.localize(datetime(2009, 9, 25, 7))
        correct_next_date = timezone.localize(datetime(2009, 9, 26, 5))
        assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #46
0
 def add_periodic_task(self, module_name, cls_name, function_name, sched_args, sched_kwargs, ignore_scheduler_lock=False):
     now = datetime.datetime.now()
     ct = CronTrigger(*sched_args, **sched_kwargs)
     when = ct.get_next_fire_time(now)
     item = {
         "type": "periodic_task",
         "module_name": module_name,
         "class_name": cls_name,
         "function_name": function_name,
         "sched_args": sched_args,
         "sched_kwargs": sched_kwargs,
     }
     self.add_to_schedule(when, item, periodic_list=True, ignore_scheduler_lock=ignore_scheduler_lock)
Beispiel #47
0
    def test_dst_change(self):
        """
        Making sure that CronTrigger works correctly when crossing the DST switch threshold.
        Note that you should explicitly compare datetimes as strings to avoid the internal datetime comparison which
        would test for equality in the UTC timezone.
        """

        eastern = pytz.timezone('US/Eastern')
        trigger = CronTrigger(minute='*/30', timezone=eastern)

        datetime_edt = eastern.localize(datetime(2013, 11, 3, 1, 5),
                                        is_dst=True)
        correct_next_date = eastern.localize(datetime(2013, 11, 3, 1, 30),
                                             is_dst=True)
        assert str(trigger.get_next_fire_time(
            None, datetime_edt)) == str(correct_next_date)

        datetime_edt = eastern.localize(datetime(2013, 11, 3, 1, 35),
                                        is_dst=True)
        correct_next_date = eastern.localize(datetime(2013, 11, 3, 1),
                                             is_dst=False)
        assert str(trigger.get_next_fire_time(
            None, datetime_edt)) == str(correct_next_date)
Beispiel #48
0
 def test_cron_offset_nextmonth(self, timezone):
     trigger = CronTrigger(year='*',
                           day=28,
                           hour=6,
                           timezone=timezone,
                           offset=timedelta(days=2, hours=2))
     assert repr(trigger) == (
         "<CronTrigger (year='*', day='28', "
         "hour='6', offset='datetime.timedelta(2, 7200)')>")
     assert str(trigger) == ("cron[year='*', day='28', "
                             "hour='6', offset=180000]")
     start_date = timezone.localize(datetime(2016, 2, 2))
     correct_next_date = timezone.localize(datetime(2016, 3, 1, 8))
     assert trigger.get_next_fire_time(None,
                                       start_date) == correct_next_date
Beispiel #49
0
 def test_cron_offset_negative(self, timezone):
     trigger = CronTrigger(year='*',
                           month='*',
                           day=1,
                           hour=6,
                           timezone=timezone,
                           offset=-86400)
     assert repr(trigger) == ("<CronTrigger (year='*', month='*', day='1', "
                              "hour='6', offset='datetime.timedelta(-1)')>")
     assert str(trigger) == ("cron[year='*', month='*', day='1', "
                             "hour='6', offset=-86400]")
     start_date = timezone.localize(datetime(2015, 11, 30, 7))
     correct_next_date = timezone.localize(datetime(2015, 12, 31, 6))
     assert trigger.get_next_fire_time(None,
                                       start_date) == correct_next_date
Beispiel #50
0
 def add_periodic_task(self, module_name, cls_name, function_name, sched_args,
                       sched_kwargs, ignore_scheduler_lock=False):
     now = datetime.datetime.now()
     ct = CronTrigger(*sched_args, **sched_kwargs)
     when = ct.get_next_fire_time(now)
     logging.info("ct.get_next_fire_time(now)")
     logging.info(when)
     item = {
         "type": "periodic_task",
         "module_name": module_name,
         "class_name": cls_name,
         "function_name": function_name,
         "sched_args": sched_args,
         "sched_kwargs": sched_kwargs,
     }
     self.add_to_schedule(when, item, periodic_list=True, ignore_scheduler_lock=ignore_scheduler_lock)
def beat_init_handler(sender=None, **kwargs):
    
#     def get_run_times(trigger, first_run_time, end_time):
#         if end_time == None:
#             end_time = datetime.datetime.now(pytz.utc)
#         run_times = []
#         while first_run_time and first_run_time <= end_time:
#             run_times.append(first_run_time)
#             first_run_time = trigger.get_next_fire_time(first_run_time, first_run_time)
#         return run_times
    
    for v in CELERYBEAT_SCHEDULE.itervalues():
        trigger = CronTrigger(hour=','.join(str(h) for h in v['schedule'].hour),
                          start_date=v['options']['eta'],
                          end_date=v['options'].get('expires', None),
                          timezone=pytz.utc)
        next_fire_time = trigger.start_date
        while next_fire_time and next_fire_time <= (trigger.end_date if trigger.end_date else datetime.datetime.now(pytz.utc)):
            task = signature(v['task'], v.get('args', ()) + (next_fire_time,), v.get('kwargs', {}), v.get('options', {}), app)
            try:
                task()
            except Exception as e:
                logger.exception(e)
            next_fire_time = trigger.get_next_fire_time(next_fire_time, next_fire_time)
Beispiel #52
0
 def test_previous_fire_time_4(self, timezone):
     trigger = CronTrigger(day="*", timezone=timezone)
     previous_fire_time = timezone.localize(datetime(2016, 6, 4).replace(microsecond=0))
     now = timezone.localize(datetime(2016, 6, 4).replace(microsecond=0))
     correct_next_date = timezone.localize(datetime(2016, 6, 5).replace(microsecond=0))
     assert trigger.get_next_fire_time(previous_fire_time, now) == correct_next_date
Beispiel #53
0
 def test_cron_trigger_3(self, timezone):
     trigger = CronTrigger(year="2009", month="2", hour="8-10", timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2009', month='2', hour='8-10')>"
     start_date = timezone.localize(datetime(2009, 1, 1))
     correct_next_date = timezone.localize(datetime(2009, 2, 1, 8))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #54
0
 def test_cron_trigger_4(self, timezone):
     trigger = CronTrigger(year="2012", month="2", day="last", timezone=timezone)
     assert repr(trigger) == "<CronTrigger (year='2012', month='2', day='last')>"
     start_date = timezone.localize(datetime(2012, 2, 1))
     correct_next_date = timezone.localize(datetime(2012, 2, 29))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date
Beispiel #55
0
 def test_month_rollover(self, timezone):
     trigger = CronTrigger(timezone=timezone, day=30)
     now = timezone.localize(datetime(2016, 2, 1))
     expected = timezone.localize(datetime(2016, 3, 30))
     assert trigger.get_next_fire_time(None, now) == expected
 def test_previous_fire_time_3(self, timezone):
     trigger = CronTrigger(day="*", timezone=timezone)
     previous_fire_time = timezone.localize(datetime(2016, 4, 25))
     now = timezone.localize(datetime(2016, 4, 25))
     correct_next_date = timezone.localize(datetime(2016, 4, 26))
     assert trigger.get_next_fire_time(previous_fire_time, now) == correct_next_date
 def test_cron_trigger_2(self, timezone):
     trigger = CronTrigger(year='2009/2', month='1/3', day='5-13', timezone=timezone)
     start_date = timezone.localize(datetime(2009, 10, 14))
     correct_next_date = timezone.localize(datetime(2011, 1, 5))
     assert trigger.get_next_fire_time(None, start_date) == correct_next_date