2018-08-06 20:22:28 +00:00
|
|
|
"""
|
|
|
|
Provides Service class
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-04-02 22:41:10 +00:00
|
|
|
from time import sleep
|
|
|
|
from threading import Thread
|
|
|
|
from lib import Logger, Loader, Loadable
|
|
|
|
from Config import cnf
|
|
|
|
|
|
|
|
class Service(Loadable):
|
2018-04-02 23:47:50 +00:00
|
|
|
"""Base class for loadale service"""
|
|
|
|
# service consists of running thread and storage attached
|
2018-04-02 22:41:10 +00:00
|
|
|
def __init__(self, thread, id, root=cnf):
|
|
|
|
super().__init__(id, root)
|
|
|
|
|
2018-08-02 02:12:12 +00:00
|
|
|
self._data = Loader.by_id('storage', self.lcnf.get("storage"))
|
2018-04-02 22:41:10 +00:00
|
|
|
|
|
|
|
self._stop_timeout = 10
|
|
|
|
self._running = False
|
|
|
|
self._thread_to_run = thread
|
|
|
|
self._run_thread = None
|
|
|
|
self._logger = Logger('Service')
|
|
|
|
|
|
|
|
self._init()
|
|
|
|
|
|
|
|
def _init(self):
|
|
|
|
pass
|
2018-08-06 20:22:28 +00:00
|
|
|
|
2018-04-02 22:41:10 +00:00
|
|
|
def start(self):
|
2018-08-06 20:22:28 +00:00
|
|
|
"""Executes pre_start, starts thread and executes post_start"""
|
2018-04-02 22:41:10 +00:00
|
|
|
self._logger.debug('pre_start')
|
|
|
|
self._pre_start()
|
|
|
|
|
|
|
|
self._logger.debug('start')
|
|
|
|
self._running = True
|
|
|
|
self._run_thread = Thread(target=self._thread_to_run)
|
|
|
|
self._run_thread.daemon = True
|
|
|
|
self._run_thread.start()
|
|
|
|
|
|
|
|
self._logger.debug('post_start')
|
|
|
|
self._post_start()
|
|
|
|
|
|
|
|
self._logger.info('start finished')
|
|
|
|
|
|
|
|
def stop(self):
|
2018-08-06 20:22:28 +00:00
|
|
|
"""Executes pre_stop, stops thread and executes post_stop"""
|
2018-04-02 22:41:10 +00:00
|
|
|
self._logger.debug('pre_stop')
|
|
|
|
self._pre_stop()
|
|
|
|
|
|
|
|
self._logger.debug('stop')
|
|
|
|
self._running = False
|
|
|
|
self._run_thread.join(timeout=self._stop_timeout)
|
|
|
|
|
|
|
|
self._logger.debug('post_stop')
|
|
|
|
self._post_stop()
|
|
|
|
|
|
|
|
self._logger.info('stop finished')
|
2018-08-06 20:22:28 +00:00
|
|
|
|
2018-04-02 22:41:10 +00:00
|
|
|
def __run(self):
|
|
|
|
while self._running:
|
|
|
|
self._logger.debug('NOOP')
|
|
|
|
sleep(1)
|
2018-08-06 20:22:28 +00:00
|
|
|
|
2018-04-02 22:41:10 +00:00
|
|
|
def _pre_stop(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _post_stop(self):
|
|
|
|
pass
|
2018-08-06 20:22:28 +00:00
|
|
|
|
2018-04-02 22:41:10 +00:00
|
|
|
def _pre_start(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _post_start(self):
|
|
|
|
pass
|