BGP-Ranking/bgpranking/abstractmanager.py

33 lines
989 B
Python
Raw Normal View History

2018-03-30 14:33:33 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from abc import ABC, abstractmethod
import logging
from .libs.helpers import long_sleep, shutdown_requested
class AbstractManager(ABC):
def __init__(self, loglevel: int=logging.DEBUG):
self.logger = logging.getLogger(f'{self.__class__.__name__}')
2018-03-30 14:33:33 +02:00
self.logger.setLevel(loglevel)
self.logger.info(f'Initializing {self.__class__.__name__}')
2018-03-30 14:33:33 +02:00
@abstractmethod
def _to_run_forever(self):
pass
def run(self, sleep_in_sec: int):
self.logger.info(f'Launching {self.__class__.__name__}')
2018-03-30 14:33:33 +02:00
while True:
if shutdown_requested():
break
2018-04-03 23:31:54 +02:00
try:
self._to_run_forever()
except Exception:
self.logger.exception(f'Something went terribly wrong in {self.__class__.__name__}.')
2018-03-30 14:33:33 +02:00
if not long_sleep(sleep_in_sec):
break
self.logger.info(f'Shutting down {self.__class__.__name__}')