Exemple #1
0
    def test_nextprev(self):
        hon = timezone('Pacific/Honolulu')
        ho = CrabSchedule('0 * * * *', 'UTC')
        fm = CrabSchedule('0-55/5 * * * *', 'UTC')
        lt = CrabSchedule('0 12 * * * *', 'Pacific/Honolulu')
        d = datetime(2020, 2, 1, 12, 30, tzinfo=UTC)
        dl = datetime(2020, 2, 1, 12, 30, tzinfo=hon)

        self.assertEqual(ho.next_datetime(d),
                         datetime(2020, 2, 1, 13, 0, tzinfo=UTC),
                         'Next hourly datetime correct')
        self.assertEqual(ho.previous_datetime(d),
                         datetime(2020, 2, 1, 12, 0, tzinfo=UTC),
                         'Previous hourly datetime correct')

        self.assertEqual(fm.next_datetime(d),
                         datetime(2020, 2, 1, 12, 35, tzinfo=UTC),
                         'Next five minute datetime correct')
        self.assertEqual(fm.previous_datetime(d),
                         datetime(2020, 2, 1, 12, 25, tzinfo=UTC),
                         'Previous five minute datetime correct')

        self.assertEqual(lt.next_datetime(d),
                         datetime(2020, 2, 1, 22, 0, tzinfo=UTC),
                         'Next lunchtime correct as UTC')
        self.assertEqual(lt.previous_datetime(d),
                         datetime(2020, 1, 31, 22, 0, tzinfo=UTC),
                         'Previous lunchtime correct as UTC')

        self.assertEqual(lt.next_datetime(dl),
                         datetime(2020, 2, 2, 12, 0, tzinfo=hon),
                         'Next lunchtime correct')
        self.assertEqual(lt.previous_datetime(dl),
                         datetime(2020, 2, 1, 12, 0, tzinfo=hon),
                         'Previous lunchtime correct')
Exemple #2
0
    def test_nextprev(self):
        hon = timezone('Pacific/Honolulu')
        ho = CrabSchedule('0 * * * *', 'UTC')
        fm = CrabSchedule('0-55/5 * * * *', 'UTC')
        lt = CrabSchedule('0 12 * * * *', 'Pacific/Honolulu')
        d = datetime(2020, 2, 1, 12, 30, tzinfo=UTC)
        dl = datetime(2020, 2, 1, 12, 30, tzinfo=hon)

        self.assertEqual(ho.next_datetime(d),
                         datetime(2020, 2, 1, 13, 0, tzinfo=UTC),
                         'Next hourly datetime correct')
        self.assertEqual(ho.previous_datetime(d),
                         datetime(2020, 2, 1, 12, 0, tzinfo=UTC),
                         'Previous hourly datetime correct')

        self.assertEqual(fm.next_datetime(d),
                         datetime(2020, 2, 1, 12, 35, tzinfo=UTC),
                         'Next five minute datetime correct')
        self.assertEqual(fm.previous_datetime(d),
                         datetime(2020, 2, 1, 12, 25, tzinfo=UTC),
                         'Previous five minute datetime correct')

        self.assertEqual(lt.next_datetime(d),
                         datetime(2020, 2, 1, 22, 0, tzinfo=UTC),
                         'Next lunchtime correct as UTC')
        self.assertEqual(lt.previous_datetime(d),
                         datetime(2020, 1, 31, 22, 0, tzinfo=UTC),
                         'Previous lunchtime correct as UTC')

        self.assertEqual(lt.next_datetime(dl),
                         datetime(2020, 2, 2, 12, 0, tzinfo=hon),
                         'Next lunchtime correct')
        self.assertEqual(lt.previous_datetime(dl),
                         datetime(2020, 2, 1, 12, 0, tzinfo=hon),
                         'Previous lunchtime correct')
Exemple #3
0
    def run_minutely(self, datetime_):
        """Issues notifications if any are scheduled for the given minute."""

        current = []
        match_daily = self.schedule.match(datetime_)

        if match_daily:
            daily_start = self.schedule.previous_datetime(datetime_)
        else:
            daily_start = None

        try:
            notifications = self.store.get_notifications()

        except CrabError as err:
            print('Error fetching notifications:', str(err))
            return

        for notification in notifications:
            n_id = notification['notifyid']

            if (n_id in self.config and n_id in self.sched
                    and notification['time'] == self.config[n_id]['time'] and
                    notification['timezone'] == self.config[n_id]['timezone']):
                schedule = self.sched[n_id]
            else:
                self.config[n_id] = notification
                if notification['time'] is not None:
                    try:
                        schedule = CrabSchedule(notification['time'],
                                                notification['timezone'])
                    except CrabError as err:
                        schedule = None
                        print('Warning: could not read notification schedule:',
                              str(err))
                else:
                    schedule = None

                self.sched[n_id] = schedule

            if schedule is None:
                if match_daily:
                    current.append(
                        CrabNotifyJob(notification, daily_start, datetime_))
            else:
                if schedule.match(datetime_):
                    current.append(
                        CrabNotifyJob(notification,
                                      schedule.previous_datetime(datetime_),
                                      datetime_))

        if current:
            self.notify(current)
Exemple #4
0
    def run_minutely(self, datetime_):
        """Issues notifications if any are scheduled for the given minute."""

        current = []
        match_daily = self.schedule.match(datetime_)

        if match_daily:
            daily_start = self.schedule.previous_datetime(datetime_)
        else:
            daily_start = None

        try:
            notifications = self.store.get_notifications()

        except CrabError as err:
            logger.exception('Error fetching notifications')
            return

        for notification in notifications:
            n_id = notification['notifyid']

            if (n_id in self.config and n_id in self.sched and
                    notification['time'] == self.config[n_id]['time'] and
                    notification['timezone'] == self.config[n_id]['timezone']):
                schedule = self.sched[n_id]
            else:
                self.config[n_id] = notification
                if notification['time'] is not None:
                    try:
                        schedule = CrabSchedule(notification['time'],
                                                notification['timezone'])
                    except CrabError as err:
                        schedule = None
                        logger.exception(
                            'Warning: could not read notification schedule')
                else:
                    schedule = None

                self.sched[n_id] = schedule

            if schedule is None:
                if match_daily:
                    current.append(CrabNotifyJob(
                        notification, daily_start, datetime_))
            else:
                if schedule.match(datetime_):
                    current.append(CrabNotifyJob(
                        notification, schedule.previous_datetime(datetime_),
                        datetime_))

        if current:
            self.notify(current)
Exemple #5
0
    def run_minutely(self, datetime_):
        """Issues notifications if any are scheduled for the given minute."""

        current = []
        end = datetime_.replace(second=0, microsecond=0)
        match_daily = self.schedule.match(datetime_)

        if match_daily:
            daily_start = self.schedule.previous_datetime(end)
        else:
            daily_start = None

        try:
            notifications = self.store.get_notifications()

        except CrabError as err:
            print('Error fetching notifications:', str(err))
            return

        for notification in notifications:
            n_id = notification['notifyid']

            if (n_id in self.config and n_id in self.sched
                and notification['time'] == self.config[n_id]['time']
                and notification['timezone'] == self.config[n_id]['timezone']):
                    schedule = self.sched[n_id]
            else:
                self.config[n_id] = notification
                if notification['time'] is not None:
                    schedule = CrabSchedule(notification['time'],
                                            notification['timezone'])
                else:
                    schedule = None

                self.sched[n_id] = schedule

            if schedule is None:
                if match_daily:
                    current.append(CrabNotifyJob(
                        notification, daily_start, end))
            else:
                if schedule.match(datetime_):
                    current.append(CrabNotifyJob(
                        notification, schedule.previous_datetime(end), end))

        if current:
            self.notify(current)
Exemple #6
0
class CrabNotifyService(CrabMinutely):
    """Service to send notifications as required.

    Currently only a single daily schedule is implemented."""

    def __init__(self, config, store, notify):
        """Constructor method.

        Stores CrabNotify object and daily CrabSchedule object."""

        CrabMinutely.__init__(self)

        self.store = store
        self.notify = notify
        self.schedule = CrabSchedule(config['daily'],
                                     config['timezone'])
        self.config = {}
        self.sched = {}

    def run_minutely(self, datetime_):
        """Issues notifications if any are scheduled for the given minute."""

        current = []
        end = datetime_.replace(second=0, microsecond=0)
        match_daily = self.schedule.match(datetime_)

        if match_daily:
            daily_start = self.schedule.previous_datetime(end)
        else:
            daily_start = None

        try:
            notifications = self.store.get_notifications()

        except CrabError as err:
            print('Error fetching notifications:', str(err))
            return

        for notification in notifications:
            n_id = notification['notifyid']

            if (n_id in self.config and n_id in self.sched
                and notification['time'] == self.config[n_id]['time']
                and notification['timezone'] == self.config[n_id]['timezone']):
                    schedule = self.sched[n_id]
            else:
                self.config[n_id] = notification
                if notification['time'] is not None:
                    schedule = CrabSchedule(notification['time'],
                                            notification['timezone'])
                else:
                    schedule = None

                self.sched[n_id] = schedule

            if schedule is None:
                if match_daily:
                    current.append(CrabNotifyJob(
                        notification, daily_start, end))
            else:
                if schedule.match(datetime_):
                    current.append(CrabNotifyJob(
                        notification, schedule.previous_datetime(end), end))

        if current:
            self.notify(current)
Exemple #7
0
class CrabNotifyService(CrabMinutely):
    """Service to send notifications as required.

    Currently only a single daily schedule is implemented."""
    def __init__(self, config, store, notify):
        """Constructor method.

        Stores CrabNotify object and daily CrabSchedule object."""

        CrabMinutely.__init__(self)

        self.store = store
        self.notify = notify
        self.schedule = CrabSchedule(config['daily'], config['timezone'])
        self.config = {}
        self.sched = {}

    def run_minutely(self, datetime_):
        """Issues notifications if any are scheduled for the given minute."""

        current = []
        match_daily = self.schedule.match(datetime_)

        if match_daily:
            daily_start = self.schedule.previous_datetime(datetime_)
        else:
            daily_start = None

        try:
            notifications = self.store.get_notifications()

        except CrabError as err:
            print('Error fetching notifications:', str(err))
            return

        for notification in notifications:
            n_id = notification['notifyid']

            if (n_id in self.config and n_id in self.sched
                    and notification['time'] == self.config[n_id]['time'] and
                    notification['timezone'] == self.config[n_id]['timezone']):
                schedule = self.sched[n_id]
            else:
                self.config[n_id] = notification
                if notification['time'] is not None:
                    try:
                        schedule = CrabSchedule(notification['time'],
                                                notification['timezone'])
                    except CrabError as err:
                        schedule = None
                        print('Warning: could not read notification schedule:',
                              str(err))
                else:
                    schedule = None

                self.sched[n_id] = schedule

            if schedule is None:
                if match_daily:
                    current.append(
                        CrabNotifyJob(notification, daily_start, datetime_))
            else:
                if schedule.match(datetime_):
                    current.append(
                        CrabNotifyJob(notification,
                                      schedule.previous_datetime(datetime_),
                                      datetime_))

        if current:
            self.notify(current)