Skip to content

Commit 9546c98

Browse files
committed
feat: add sigterm handler to gracefully shutdown worker and log
1 parent 0189c65 commit 9546c98

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

azure_functions_worker/logging.py

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ def enable_console_logging() -> None:
9191
if logger and handler:
9292
logger.addHandler(handler)
9393

94+
def flush_logger(logger: logging.Logger):
95+
for handler in logger.handlers:
96+
handler.flush()
9497

9598
def is_system_log_category(ctg: str) -> bool:
9699
"""Check if the logging namespace belongs to system logs. Category starts

azure_functions_worker/main.py

+44
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,16 @@ def main():
4747

4848
import asyncio
4949

50+
5051
from . import logging
5152
from .logging import error_logger, format_exception, logger
5253

5354
args = parse_args()
5455
logging.setup(log_level=args.log_level, log_destination=args.log_to)
5556

57+
# register sigterm handler
58+
register_sigterm_handler()
59+
5660
logger.info('Starting Azure Functions Python Worker.')
5761
logger.info('Worker ID: %s, Request ID: %s, Host Address: %s:%s',
5862
args.worker_id, args.request_id, args.host, args.port)
@@ -66,6 +70,46 @@ def main():
6670
format_exception(ex)))
6771
raise
6872

73+
def register_sigterm_handler():
74+
import signal
75+
"""
76+
Registers a custom handler for the SIGTERM signal.
77+
78+
This function will set up a signal handler to intercept the SIGTERM signal,
79+
which is typically sent to gracefully terminate a process. When SIGTERM is
80+
received, the program will log the signal and perform a graceful shutdown
81+
by calling sys.exit().
82+
83+
Windows Python Function is not supported and Windows does not support
84+
SIGTERM.
85+
"""
86+
87+
def handle_sigterm(signum, frame):
88+
from . import dispatcher
89+
import sys
90+
from .logging import logger, flush_logger
91+
import traceback
92+
from azure_functions_worker.dispatcher import DispatcherMeta
93+
from azure_functions_worker import loader
94+
# Log the received signal
95+
logger.info(f"SIGTERM received (signal: {signum}). "
96+
"Shutting down gracefully...")
97+
98+
# Log the frame details to see whats executed when the signal was received
99+
logger.info("Frame details at signal receipt:"
100+
f"\n{''.join(traceback.format_stack(frame))}")
101+
102+
DispatcherMeta.__current_dispatcher__ = None
103+
104+
loader.uninstall()
105+
106+
flush_logger()
107+
108+
dispatcher.Dispatcher.stop()
109+
sys.exit(0) # Exit the program gracefully
110+
111+
signal.signal(signal.SIGTERM, handle_sigterm)
112+
69113

70114
async def start_async(host, port, worker_id, request_id):
71115
from . import dispatcher

0 commit comments

Comments
 (0)