def _get_scheduled_routing_rules(self, rules, current_dt_utc): """ Iterates rules list and returns the list of rules that are scheduled. :param list rules: routing rules to check :param datetime current_dt_utc: the value to take as the current time in UTC :return: the rules scheduled to be appplied at `current_dt_utc` :rtype: list """ # make it a timezone-aware object current_dt_utc = current_dt_utc.replace(tzinfo=pytz.utc) delta_minute = timedelta(minutes=1) scheduled_rules = [] for rule in rules: is_scheduled = True schedule = rule.get('schedule', {}) if schedule: # adjust current time to the schedule's timezone tz_name = schedule.get('time_zone') schedule_tz = pytz.timezone(tz_name) if tz_name else pytz.utc now_tz_schedule = current_dt_utc.astimezone(tz=schedule_tz) # Create start and end time-of-day limits. If start time is not # defined, the beginning of the day is assumed. If end time # is not defined, the end of the day is assumed (excluding the # midnight, since at that point a new day has already begun). hour_of_day_from = schedule.get('hour_of_day_from') if not hour_of_day_from: hour_of_day_from = '00:00:00' # might be both '' or None from_time = set_time(now_tz_schedule, hour_of_day_from) hour_of_day_to = schedule.get('hour_of_day_to') if hour_of_day_to: to_time = set_time(now_tz_schedule, hour_of_day_to) if hour_of_day_to[-2:] == '00': to_time = to_time + delta_minute else: to_time = set_time(now_tz_schedule, '23:59:59') + delta_minute # check if the current day of week and time of day both match day_of_week_matches = Weekdays.is_scheduled_day( now_tz_schedule, schedule.get('day_of_week', [])) time_of_day_matches = (from_time <= now_tz_schedule < to_time) is_scheduled = (day_of_week_matches and time_of_day_matches) if is_scheduled: scheduled_rules.append(rule) return scheduled_rules
def get_next_run(schedule, now=None): """Get next run time based on schedule. Schedule is day of week and time. :param dict schedule: dict with `day_of_week` and `create_at` params :param datetime now :return datetime """ if not schedule.get('is_active', False): return None allowed_days = [ Weekdays[day.upper()].value for day in schedule.get('day_of_week', []) ] if not allowed_days: return None if now is None: now = utcnow() now = now.replace(second=0) # adjust current time to the schedule's timezone tz_name = schedule.get('time_zone', 'UTC') if tz_name != 'UTC': next_run = local_to_utc(tz_name, set_time(now, schedule.get('create_at'))) else: next_run = set_time(now, schedule.get('create_at')) # if the time passed already today do it tomorrow earliest if next_run <= now: next_run += timedelta(days=1) while next_run.weekday() not in allowed_days: next_run += timedelta(days=1) return next_run
def get_next_run(schedule, now=None): """Get next run time based on schedule. Schedule is day of week and time. :param dict schedule: dict with `day_of_week` and `create_at` params :param datetime now :return datetime """ if not schedule.get('is_active', False): return None allowed_days = [Weekdays[day.upper()].value for day in schedule.get('day_of_week', [])] if not allowed_days: return None if now is None: now = utcnow() now = now.replace(second=0) # adjust current time to the schedule's timezone tz_name = schedule.get('time_zone', 'UTC') if tz_name != 'UTC': next_run = local_to_utc(tz_name, set_time(now, schedule.get('create_at'))) else: next_run = set_time(now, schedule.get('create_at')) # if the time passed already today do it tomorrow earliest if next_run <= now: next_run += timedelta(days=1) while next_run.weekday() not in allowed_days: next_run += timedelta(days=1) return next_run