2018-07-08 03:38:14 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from .consts import ADAPTERS
|
2018-07-07 23:51:43 +00:00
|
|
|
from .worker import ServiceWorker
|
|
|
|
|
2018-07-08 03:38:14 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
_COLUMNS = {
|
|
|
|
'timestamp': 'timestamp bigint',
|
|
|
|
'status': 'status bool',
|
|
|
|
'latency': 'latency bigint',
|
|
|
|
}
|
|
|
|
|
2018-07-07 23:51:43 +00:00
|
|
|
|
|
|
|
class ServiceManager:
|
|
|
|
def __init__(self, app):
|
|
|
|
self.app = app
|
|
|
|
self.cfg = app.cfg
|
|
|
|
self.conn = app.conn
|
|
|
|
self.loop = app.loop
|
|
|
|
|
|
|
|
self.workers = {}
|
|
|
|
self.state = {}
|
|
|
|
|
|
|
|
self._start()
|
|
|
|
|
2018-07-08 03:38:14 +00:00
|
|
|
def _make_db_table(self, name: str, service: dict):
|
|
|
|
adapter = ADAPTERS[service['adapter']]
|
|
|
|
|
|
|
|
columnstr = map(_COLUMNS.get, adapter.spec['db'])
|
|
|
|
columnstr = ',\n'.join(columnstr)
|
|
|
|
|
|
|
|
log.info(f'Making table for {name}')
|
|
|
|
self.conn.executescript(f"""
|
|
|
|
CREATE TABLE IF NOT EXISTS {name} (
|
|
|
|
{columnstr}
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
|
2018-07-07 23:51:43 +00:00
|
|
|
def _start(self):
|
|
|
|
for name, service in self.cfg.SERVICES.items():
|
2018-07-08 03:38:14 +00:00
|
|
|
self._make_db_table(name, service)
|
|
|
|
|
2018-07-07 23:51:43 +00:00
|
|
|
# spawn a service worker
|
|
|
|
serv_worker = ServiceWorker(self, name, service)
|
|
|
|
self.workers[name] = serv_worker
|
|
|
|
|
2018-07-09 05:48:44 +00:00
|
|
|
self.state[name] = None
|