def test_add_duration(self): dt = datetime.datetime(2012, 10, 31) dr = isodate.Duration(months=1) result = dateutils.add_interval_to_datetime(dr, dt) self.assertEqual(result.month, 11) self.assertEqual(result.day, 30)
def __init__(self, call_request, schedule, failure_threshold=None, last_run=None, enabled=True): super(ScheduledCall, self).__init__() schedule_tag = resource_tag(dispatch_constants.RESOURCE_SCHEDULE_TYPE, str(self._id)) call_request.tags.append(schedule_tag) interval, start, runs = dateutils.parse_iso8601_interval(schedule) now = datetime.utcnow() zero = timedelta(seconds=0) start = start and dateutils.to_naive_utc_datetime(start) self.serialized_call_request = call_request.serialize() self.schedule = schedule self.failure_threshold = failure_threshold self.consecutive_failures = 0 self.first_run = start or now # NOTE using != because ordering comparison with a Duration is not allowed while interval != zero and self.first_run <= now: # try to schedule the first run in the future self.first_run = dateutils.add_interval_to_datetime( interval, self.first_run) self.last_run = last_run and dateutils.to_naive_utc_datetime(last_run) self.next_run = None # will calculated and set by the scheduler self.remaining_runs = runs self.enabled = enabled
def test_add_timedelta(self): dt = datetime.datetime(2012, 10, 24) td = datetime.timedelta(days=8) result_1 = dateutils.add_interval_to_datetime(td, dt) result_2 = dt + td self.assertEqual(result_1, result_2)
def _calculate_next_run(scheduled_call): # rip-off from scheduler module if scheduled_call['remaining_runs'] == 0: return None last_run = scheduled_call['last_run'] if last_run is None: return scheduled_call['first_run'] now = datetime.utcnow() interval = dateutils.parse_iso8601_interval(scheduled_call['schedule'])[0] next_run = last_run while next_run < now: next_run = dateutils.add_interval_to_datetime(interval, next_run) return next_run
def calculate_next_run(self, scheduled_call): """ Calculate the next run datetime of a scheduled call @param scheduled_call: scheduled call to schedule @type scheduled_call: dict @return: datetime of scheduled call's next run or None if there is no next run @rtype: datetime.datetime or None """ if scheduled_call['remaining_runs'] == 0: return None last_run = scheduled_call['last_run'] if last_run is None: return scheduled_call['first_run'] # this was calculated by the model constructor now = datetime.datetime.utcnow() interval = dateutils.parse_iso8601_interval(scheduled_call['schedule'])[0] next_run = last_run while next_run < now: next_run = dateutils.add_interval_to_datetime(interval, next_run) return next_run
def __init__(self, call_request, schedule, failure_threshold=None, last_run=None, enabled=True): super(ScheduledCall, self).__init__() schedule_tag = resource_tag(dispatch_constants.RESOURCE_SCHEDULE_TYPE, str(self._id)) call_request.tags.append(schedule_tag) interval, start, runs = dateutils.parse_iso8601_interval(schedule) now = datetime.utcnow() zero = timedelta(seconds=0) start = start and dateutils.to_naive_utc_datetime(start) self.serialized_call_request = call_request.serialize() self.schedule = schedule self.failure_threshold = failure_threshold self.consecutive_failures = 0 self.first_run = start or now # NOTE using != because ordering comparison with a Duration is not allowed while interval != zero and self.first_run <= now: # try to schedule the first run in the future self.first_run = dateutils.add_interval_to_datetime(interval, self.first_run) self.last_run = last_run and dateutils.to_naive_utc_datetime(last_run) self.next_run = None # will calculated and set by the scheduler self.remaining_runs = runs self.enabled = enabled
def calculate_next_run(scheduled_call): """ Given a schedule call, calculate when it should be run next. :param scheduled_call: scheduled call :type scheduled_call: bson.BSON or pulp.server.db.model.dispatch.ScheduledCall :return: when the scheduled call should be run next :rtype: datetime.datetime """ last_run = scheduled_call['last_run'] if last_run is None: return scheduled_call['first_run'] now = datetime.datetime.utcnow() interval = dateutils.parse_iso8601_interval(scheduled_call['schedule'])[0] next_run = last_run while next_run < now: next_run = dateutils.add_interval_to_datetime(interval, next_run) return next_run
def calculate_first_run(schedule): """ Given a schedule in ISO8601 interval format, calculate the first time the schedule should be run. This method make a best effort to calculate a time in the future. :param schedule: ISO8601 interval schedule :type schedule: str :return: when the schedule should be run for the first time :rtype: datetime.datetime """ now = datetime.datetime.utcnow() interval, start = dateutils.parse_iso8601_interval(schedule)[0:2] first_run = dateutils.to_naive_utc_datetime(start) if start else now # the "zero time" handles the really off case where the schedule is a # start time and a single run instead of something recurring while interval != ZERO_TIME and first_run <= now: first_run = dateutils.add_interval_to_datetime(interval, first_run) return first_run