Commit 77e98b3d authored by Andrey Veltischev's avatar Andrey Veltischev
Browse files

Add wathcer for running main coroutine

parent f22562b7
build_package:
python setup.py sdist bdist_wheel
cp dist/prozorro_metrics-1.0.0-py3-none-any.whl ../procedure/prozorro_metrics-1.0.0-py3-none-any.whl
version:
$(eval GIT_TAG ?= $(shell git describe --abbrev=0))
$(eval VERSION ?= $(shell read -p "Version: " VERSION; echo $$VERSION))
......
......@@ -42,19 +42,29 @@ class AioAppWrapper:
class CoroutineWrapper:
def __init__(self, _coro, _loop):
def __init__(self, _coro, _loop, _stop_if_failed):
self.coro = _coro
self.loop = _loop
self.stop_if_failed = _stop_if_failed
self.task = None
def initialize(self):
LOG.info('Initialize coroutine')
self.task = self.loop.create_task(self.coro)
self.task.stop_if_failed = self.stop_if_failed
def shutdown(self):
self.task.cancel()
async def main_coroutine_watcher():
while True:
await asyncio.sleep(5)
if not [task for task in asyncio.all_tasks() if getattr(task, 'stop_if_failed', None)]:
LOG.critical('Main coroutine is not running. Stopping loop...')
asyncio.get_event_loop().stop()
class ApplicationWrapper:
def __init__(self, _loop=None):
self.loop = _loop
......@@ -72,9 +82,9 @@ class ApplicationWrapper:
AioAppWrapper(_app, _port, self.loop)
)
def add_coroutine(self, _coro):
def add_coroutine(self, _coro, stop_if_failed=False):
self.apps.append(
CoroutineWrapper(_coro, self.loop)
CoroutineWrapper(_coro, self.loop, stop_if_failed)
)
def run_all(self):
......@@ -82,6 +92,7 @@ class ApplicationWrapper:
for app in self.apps:
app.initialize()
try:
self.loop.create_task(main_coroutine_watcher())
LOG.info('Start serving')
self.loop.run_forever()
except KeyboardInterrupt: # pragma: no cover
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment