#NEWLINE# def _install_signal_handlers( self ) : #NEWLINE# #INDENT# 'Installs signal handlers for handling SIGINT and SIGTERM\n        gracefully.\n        ' #NEWLINE# def request_force_stop( signum , frame ) : #NEWLINE# #INDENT# 'Terminates the application (cold shutdown).\n            ' #NEWLINE# self . log . warning ( 'Cold shut down.' ) #NEWLINE# if self . horse_pid : #NEWLINE# #INDENT# msg = ( 'Taking down horse %d with me.' % self . horse_pid ) #NEWLINE# self . log . warning ( msg ) #NEWLINE# try : #NEWLINE# #INDENT# os . kill ( self . horse_pid , signal . SIGKILL ) #NEWLINE# #UNINDENT# except OSError as e : #NEWLINE# #INDENT# if ( e . errno != errno . ESRCH ) : #NEWLINE# #INDENT# self . log . debug ( 'Horse already down.' ) #NEWLINE# raise #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# raise SystemExit ( ) #NEWLINE# #UNINDENT# def request_stop( signum , frame ) : #NEWLINE# #INDENT# 'Stops the current worker loop but waits for child processes to\n            end gracefully (warm shutdown).\n            ' #NEWLINE# self . log . debug ( ( 'Got signal %s.' % signal_name ( signum ) ) ) #NEWLINE# signal . signal ( signal . SIGINT , request_force_stop ) #NEWLINE# signal . signal ( signal . SIGTERM , request_force_stop ) #NEWLINE# msg = 'Warm shut down requested.' #NEWLINE# self . log . warning ( msg ) #NEWLINE# if ( self . state == 'busy' ) : #NEWLINE# #INDENT# self . _stopped = True #NEWLINE# self . log . debug ( 'Stopping after current horse is finished. Press Ctrl+C again for a cold shutdown.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise StopRequested ( ) #NEWLINE# #UNINDENT# #UNINDENT# signal . signal ( signal . SIGINT , request_stop ) #NEWLINE# signal . signal ( signal . SIGTERM , request_stop )