Пример #1
0
def start_github_monitor(github_monitor: GitHubMonitor,
                         monitor_period: int,
                         logger: logging.Logger,
                         internal_config: InternalConfig = InternalConf):
    # Set up alert limiter
    github_error_alert_limiter = TimedTaskLimiter(
        internal_config.github_error_interval_seconds)

    # Start
    while True:
        # Read GitHub releases page
        try:
            logger.debug('Reading %s.', github_monitor.releases_page)
            github_monitor.monitor()
            logger.debug('Done reading %s.', github_monitor.releases_page)

            # Save all state
            github_monitor.save_state()

            # Reset alert limiter
            github_error_alert_limiter.reset()
        except (ReqConnectionError, ReadTimeout) as conn_err:
            if github_error_alert_limiter.can_do_task():
                github_monitor.channels.alert_error(
                    CannotAccessGitHubPageAlert(github_monitor.releases_page))
                github_error_alert_limiter.did_task()
            logger.error('Error occurred when accessing {}: {}.'
                         ''.format(github_monitor.releases_page, conn_err))
        except JSONDecodeError as json_error:
            logger.error(json_error)  # Ignore such errors
        except Exception as e:
            logger.error(e)
            raise e

        # Sleep
        logger.debug('Sleeping for %s seconds.', monitor_period)
        time.sleep(monitor_period)
Пример #2
0
class TestGitHubMonitorWithRedis(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        # Same as in setUp(), to avoid running all tests if Redis is offline

        logger = logging.getLogger('dummy')
        db = TestInternalConf.redis_test_database
        host = TestUserConf.redis_host
        port = TestUserConf.redis_port
        password = TestUserConf.redis_password
        redis = RedisApi(logger, db, host, port, password)

        try:
            redis.ping_unsafe()
        except RedisConnectionError:
            raise Exception('Redis is not online.')

    def setUp(self) -> None:
        self.logger = logging.getLogger('dummy')
        self.monitor_name = 'testmonitor'
        self.counter_channel = CounterChannel(self.logger)
        self.channel_set = ChannelSet([self.counter_channel])
        self.repo_name = 'dummy/repository/'
        self.releases_page = 'dummy.releases.page'
        self.redis_prefix = TestInternalConf.redis_github_releases_key_prefix

        self.db = TestInternalConf.redis_test_database
        self.host = TestUserConf.redis_host
        self.port = TestUserConf.redis_port
        self.password = TestUserConf.redis_password
        self.redis = RedisApi(self.logger, self.db, self.host,
                              self.port, self.password)
        self.redis.delete_all_unsafe()

        try:
            self.redis.ping_unsafe()
        except RedisConnectionError:
            self.fail('Redis is not online.')

        self.monitor = GitHubMonitor(self.monitor_name, self.channel_set,
                                     self.logger, self.redis, self.repo_name,
                                     self.releases_page, self.redis_prefix)
        self.monitor._internal_conf = TestInternalConf

    def test_load_state_changes_nothing_if_nothing_saved(self):
        self.monitor.load_state()

        self.assertIsNone(self.monitor._prev_no_of_releases)

    def test_load_state_sets_values_to_saved_values(self):
        # Set Redis values manually
        key = self.redis_prefix + self.repo_name
        self.redis.set_unsafe(key, 10)

        # Load the values from Redis
        self.monitor.load_state()

        # Assert
        self.assertEqual(10, self.monitor._prev_no_of_releases)

    def test_save_state_sets_values_to_current_values(self):
        # Set monitor values manually
        self.monitor._prev_no_of_releases = 10

        # Save the values to Redis
        self.monitor.save_state()

        # Assert
        key = self.redis_prefix + self.repo_name
        self.assertEqual(10, self.redis.get_int(key))

    def test_save_state_sets_nothing_if_no_previous_state(self):
        # Save the values to Redis
        self.monitor.save_state()

        # Assert
        key = self.redis_prefix + self.repo_name
        self.assertIsNone(self.redis.get_int(key))