-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d58a360
commit 0b12824
Showing
4 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ def main(): | |
) | ||
|
||
manager.loop() | ||
sys.exit(manager.returncode) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |