elstat/elstat/manager.py

49 lines
1.1 KiB
Python
Raw Normal View History

import logging
from .consts import ADAPTERS
2018-07-07 23:51:43 +00:00
from .worker import ServiceWorker
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()
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():
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
self.state[name] = None