2424from contextlib import contextmanager
2525from select import poll , POLLPRI , POLLIN
2626from enum import Enum
27+ import signal
2728
2829try :
2930 from .inotify_helper import InotifyHelper
@@ -107,7 +108,7 @@ def __init__(self, dpu_name):
107108 self .pci_dev_path = []
108109 self .verbosity = False
109110
110- def setup_logger (self , use_print = False ):
111+ def setup_logger (self , use_print = False , use_notice_level = False ):
111112 def print_with_time (msg ):
112113 timestamp = time .strftime ("%Y-%m-%d %H:%M:%S" )
113114 print (f"[{ timestamp } ] { msg } " )
@@ -118,8 +119,12 @@ def print_with_time(msg):
118119 self .logger_warning = print_with_time
119120 self .logger_debug = print_with_time
120121 return
121- self .logger_debug = logger .log_debug
122- self .logger_info = logger .log_info
122+ if use_notice_level :
123+ self .logger_debug = logger .log_notice
124+ self .logger_info = logger .log_notice
125+ else :
126+ self .logger_debug = logger .log_debug
127+ self .logger_info = logger .log_info
123128 self .logger_error = logger .log_error
124129 self .logger_warning = logger .log_warning
125130
@@ -414,20 +419,32 @@ def update_boot_prog_once(self, poll_var):
414419 read_value = self .read_boot_prog ()
415420 if read_value != self .boot_prog_state :
416421 self .dpu_boot_prog_update (read_value )
417- self .log_error (f"The boot_progress status is changed to = { self .boot_prog_indication } " )
422+ self .log_info (f"The boot_progress status is changed to = { self .boot_prog_indication } " )
418423
419424 def watch_boot_prog (self ):
420425 """Read boot_progress and update the value in an infinite loop"""
426+ def signal_handler (signum , frame ):
427+ self .log_info ("Received termination signal, shutting down..." )
428+ raise SystemExit ("Terminated by signal" )
429+
430+ # Register signal handler for SIGTERM
431+ signal .signal (signal .SIGTERM , signal_handler )
432+
433+ file = None
434+ file = open (self .boot_prog_path , "r" )
435+ p = poll ()
436+ p .register (file .fileno (), POLLPRI )
421437 try :
422- self .dpu_boot_prog_update ()
423- self .log_info (f"The initial boot_progress status is = { self .boot_prog_indication } " )
424- file = open (self .boot_prog_path , "r" )
425- p = poll ()
426- p .register (file .fileno (), POLLPRI )
427438 while True :
428- self .update_boot_prog_once (p )
429- except Exception :
430- self .log_error (f"Exception occured during watch_boot_progress!" )
439+ try :
440+ self .update_boot_prog_once (p )
441+ except SystemExit :
442+ break # Exit on termination signal
443+ except Exception as e :
444+ self .log_error (f"Error during watch_boot_progress: { e } " )
445+ finally :
446+ if file :
447+ file .close ()
431448
432449 @contextmanager
433450 def boot_prog_context (self ):
@@ -444,6 +461,8 @@ def boot_prog_context(self):
444461 finally :
445462 if self .boot_prog_proc and self .boot_prog_proc .is_alive ():
446463 self .boot_prog_proc .terminate ()
464+ self .boot_prog_proc .join (timeout = 3 )
465+ self .boot_prog_proc .kill ()
447466 self .boot_prog_proc .join ()
448467 else :
449468 yield
0 commit comments