-
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.
Enhance graceful cleanup and termination
- Loading branch information
Showing
3 changed files
with
65 additions
and
25 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
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,50 @@ | ||
from types import FrameType | ||
from typing import Optional | ||
|
||
import logging | ||
import signal | ||
import threading | ||
import sys | ||
|
||
from utils import flag, processes | ||
|
||
_SHUTDOWN_FLAG = flag.Flag(False) | ||
_HANDLERS = {} | ||
|
||
|
||
def _timeout() -> None: | ||
logging.error('Graceful cleanup timeout! Shutting down forcefully...') | ||
sys.exit() | ||
|
||
|
||
_GRACEFUL_TIMER = threading.Timer(2.5, _timeout) | ||
|
||
|
||
def init(context: str) -> None: | ||
_HANDLERS[2] = signal.getsignal(signal.SIGINT) | ||
_HANDLERS[15] = signal.getsignal(signal.SIGTERM) | ||
signal.signal(signal.SIGINT, lambda s, f: cleanup(s, f, context)) | ||
signal.signal(signal.SIGTERM, lambda s, f: cleanup(s, f, context)) | ||
|
||
|
||
def cleanup(sig: int, frame: Optional[FrameType], context: str) -> None: | ||
logging.info(f'(Context: {context}) Cleaning up...') | ||
_SHUTDOWN_FLAG.set() | ||
processes.terminate_subprocesses() | ||
handler = _HANDLERS[sig] | ||
if callable(handler): | ||
handler(sig, frame) | ||
logging.info(f'(Context: {context}) Cleanup done!{ | ||
" Bye-bye!" if context == "main" else ""}') | ||
|
||
# Set forceful cleanup timeout to 2.5 seconds | ||
if not _GRACEFUL_TIMER.is_alive(): | ||
_GRACEFUL_TIMER.start() | ||
|
||
# Stop timeout timer if everything finished | ||
elif context == 'main': | ||
_GRACEFUL_TIMER.cancel() | ||
|
||
|
||
def get_flag() -> bool: | ||
return _SHUTDOWN_FLAG.get() |