-
Notifications
You must be signed in to change notification settings - Fork 0
shut down gracefully on SIGTERM. #1
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import asyncio | ||
import logging | ||
import json | ||
import signal | ||
from . import settings | ||
from .app_state import AppState | ||
from .util.exceptions import LicensingError | ||
|
@@ -398,4 +399,18 @@ def main(): | |
runner, host=app["settings"]["host_interface"], port=app["settings"]["app_port"] | ||
) | ||
loop.run_until_complete(site.start()) | ||
loop.run_forever() | ||
|
||
fu = loop.create_future() | ||
# on SIGTERM setting the future result allowing the loop to exit. | ||
loop.add_signal_handler(signal.SIGTERM, lambda : fu.set_result(True)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove space after |
||
loop.run_until_complete(fu) | ||
|
||
async def shutdown(): | ||
logger.info("Shutting down MATLAB proxy-app") | ||
await app.shutdown() | ||
await app.cleanup() | ||
# waiting here to allow matlab to finish exiting. | ||
await asyncio.sleep(5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this required? looks like the app cleanup is waiting for the MATLAB process to exist There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so. i tried several times and in few cases matlab did not completed the exit and I got locked out of the matworks account for about half an hour. I will verify again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. verified. without this delay matlab is not finishing properly and the user is locked out. |
||
|
||
loop.run_until_complete(shutdown()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option is to keep the
loop.run_forever()
, add the signal handler before it, and then callloop.stop()
fromshutdown
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must use a synchronous method for the signal handler and shutdown func must remain async so it would wait for app.shutdown() and app.cleanup().
so the lambda now calls loop.stop and then I call loop.run_until_complete(shutdown())