Skip to content

Commit

Permalink
Added sighandler for web
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaserlang committed Mar 3, 2022
1 parent d58a360 commit 0b12824
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def load(path=None):
'../tbot.yaml',
'/etc/tbot/tbot.yaml',
'/etc/tbot.yaml',
'/etc/tbot/tbot.yml',
'/etc/tbot.yml',
]
if not path:
path = os.environ.get('TBOT_CONFIG', None)
Expand Down
1 change: 0 additions & 1 deletion tbot/dev_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def main():
)

manager.loop()
sys.exit(manager.returncode)

if __name__ == '__main__':
main()
8 changes: 7 additions & 1 deletion tbot/web/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from functools import partial
import logging, os
import asyncio, aioredis, aiohttp
import signal
from tornado import web
from tbot import config, db
from tbot.web import handlers
from tbot.web.io_sighandler import sig_handler

def App():
return web.Application(
Expand Down Expand Up @@ -82,8 +85,11 @@ def main():
loop = asyncio.get_event_loop()
app = App()
app.loop = loop
app.listen(config['web']['port'])
server = app.listen(config['web']['port'])
signal.signal(signal.SIGTERM, partial(sig_handler, server, app))
signal.signal(signal.SIGINT, partial(sig_handler, server, app))
loop.create_task(db_connect(app))
logging.info(f'Web started on port: {config["web"]["port"]}')
loop.run_forever()

async def db_connect(app):
Expand Down
41 changes: 41 additions & 0 deletions tbot/web/io_sighandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging, time, asyncio
from tornado import ioloop
from tbot import config

SHUTDOWN_WAIT = 5 # seconds
def sig_handler(server, application, sig, frame):
io_loop = ioloop.IOLoop.instance()
if hasattr(application, 'shutting_down') and application.shutting_down == True:
io_loop.stop()
return

application.shutting_down = True

def stop_loop(server, deadline: float):
now = time.time()
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task() and not t.done()]
if (now < deadline and len(tasks) > 0) and not config['debug']:
logging.debug(f'Awaiting {len(tasks)} pending tasks {tasks}')
io_loop.add_timeout(now + 1, stop_loop, server, deadline)
return

pending_connection = len(server._connections)
if (now < deadline and pending_connection > 0) and not config['debug']:
logging.debug(f'Waiting on {pending_connection} connections to finish {server._connections}')
io_loop.add_timeout(now + 1, stop_loop, server, deadline)
else:
logging.debug(f'Shutting down. {pending_connection} connections left')
application.db.close()
application.redis.close()
asyncio.run(application.db.wait_closed())
asyncio.run(application.redis.wait_closed())
io_loop.stop()

def shutdown():
logging.debug(f'Waiting for up to {SHUTDOWN_WAIT} seconds to shutdown ...')
try:
stop_loop(server, time.time() + SHUTDOWN_WAIT)
except BaseException as e:
logging.error(f'Error trying to shutdown Tornado: {str(e)}')

io_loop.add_callback_from_signal(shutdown)

0 comments on commit 0b12824

Please sign in to comment.