@@ -103,12 +103,16 @@ async def health_check(self):
103103 await self .run_health_check ()
104104
105105 async def run_health_check (self ):
106- # Run mediainfo on health check file
107- media_info = await self .run_command (f"mediainfo { HEALTHCHECK_FILE } " )
108- if "Video" not in media_info :
109- logger .error (f"MediaInfo failed for { HEALTHCHECK_FILE } " )
110- self .update_health_status (False )
111- return
106+ try :
107+ # Run mediainfo on health check file
108+ media_info = await self .run_command (f"mediainfo { HEALTHCHECK_FILE } " )
109+ if "Video" not in media_info :
110+ logger .error (f"MediaInfo failed for { HEALTHCHECK_FILE } " )
111+ self .update_health_status (False )
112+ return
113+ except asyncio .CancelledError :
114+ logger .info ("Health check task canceled." )
115+ raise
112116
113117 cleanup_command = f"rm -f { HEALTHCHECK_OUTPUT } "
114118 await self .run_command (cleanup_command )
@@ -217,10 +221,31 @@ def handle_signals():
217221async def shutdown (signame ):
218222 logger .info (f"Received signal { signame } , shutting down..." )
219223 tasks = [task for task in asyncio .all_tasks () if task is not asyncio .current_task ()]
224+
225+ # Terminate all subprocesses if any exist
220226 for task in tasks :
221- task .cancel ()
222- await asyncio .gather (* tasks , return_exceptions = True )
227+ if hasattr (task , 'process' ) and task .process is not None :
228+ task .process .terminate ()
229+ else :
230+ task .cancel ()
231+ await asyncio .gather (* tasks , return_exceptions = True , timeout = 5 )
232+ logger .info ("Shutdown complete." )
233+
234+ async def main ():
235+ try :
236+ await ffmpeg_server ()
237+ except asyncio .CancelledError :
238+ logger .info ("Main task canceled." )
239+ raise
223240
224241if __name__ == '__main__' :
225242 handle_signals ()
226- asyncio .run (ffmpeg_server ())
243+ try :
244+ asyncio .run (main ())
245+ sys .exit (0 ) # Exit with 0 on successful shutdown
246+ except KeyboardInterrupt :
247+ logger .info ("Keyboard interrupt received." )
248+ sys .exit (0 ) # Exit with 0 on Ctrl+C
249+ except Exception as e :
250+ logger .error (f"Unhandled exception: { e } " )
251+ sys .exit (1 ) # Exit with 1 on unhandled exceptions
0 commit comments