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):
|
2018-04-10 00:20:59 +02:00
|
|
|
self.logger = logging.getLogger(f'{self.__class__.__name__}')
|
2018-03-30 14:33:33 +02:00
|
|
|
self.logger.setLevel(loglevel)
|
2018-04-10 00:20:59 +02:00
|
|
|
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):
|
2018-04-10 00:20:59 +02:00
|
|
|
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:
|
2018-04-10 00:20:59 +02:00
|
|
|
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
|
2018-04-10 00:20:59 +02:00
|
|
|
self.logger.info(f'Shutting down {self.__class__.__name__}')
|