@@ -165,33 +165,46 @@ def update_health_status(self, is_healthy):
165165 health_status ['healthy' ] = is_healthy
166166
167167 async def run_command (self , command , exclude_health_check = False ):
168+ """
169+ Executes a shell command asynchronously, ensuring proper cleanup to prevent zombie processes.
170+ """
168171 binary = command .split ()[0 ].split ('/' )[- 1 ]
172+
173+ # Track Prometheus metrics
169174 if binary == 'ffmpeg' and not exclude_health_check :
170175 ffmpeg_gauge .inc ()
171176 elif binary == 'mediainfo' :
172177 mediainfo_counter .inc ()
173178 elif binary == 'ffprobe' :
174179 ffprobe_counter .inc ()
175180
181+ logger .debug (f"Executing command: { command } " )
176182 process = await asyncio .create_subprocess_shell (
177183 command ,
178184 stdout = asyncio .subprocess .PIPE ,
179185 stderr = asyncio .subprocess .PIPE
180186 )
181187
182- stdout , stderr = await process .communicate ()
183-
184- if binary == 'ffmpeg' and not exclude_health_check :
185- ffmpeg_gauge .dec ()
188+ try :
189+ stdout , stderr = await process .communicate ()
190+ if process .returncode != 0 :
191+ logger .error (f"Command failed with exit code { process .returncode } : { stderr .decode ().strip ()} " )
192+ raise RuntimeError (stderr .decode ().strip ())
193+
194+ logger .debug (f"Command output: { stdout .decode ().strip ()} " )
195+ return stdout .decode ().strip ()
196+ finally :
197+ if binary == 'ffmpeg' and not exclude_health_check :
198+ ffmpeg_gauge .dec ()
186199
187- output = stdout .decode ().strip () if stdout else ""
188- error = stderr .decode ().strip () if stderr else ""
200+ output = stdout .decode ().strip () if stdout else ""
201+ error = stderr .decode ().strip () if stderr else ""
189202
190- if process .returncode != 0 :
191- logger .error (f"Command '{ command } ' failed with error: { error } " )
192- return f"Command '{ command } ' failed with error: { error } "
193-
194- return output
203+ if process .returncode != 0 :
204+ logger .error (f"Command '{ command } ' failed with error: { error } " )
205+ return f"Command '{ command } ' failed with error: { error } "
206+
207+ return output
195208
196209 async def is_file_valid (self , filename ):
197210 try :
@@ -241,7 +254,7 @@ async def health_check(request):
241254 app = web .Application ()
242255 app .router .add_get ('/health' , health_check )
243256 app .router .add_route ('*' , '/metrics' , web ._run_app (prometheus_app ))
244-
257+
245258 runner = web .AppRunner (app )
246259 await runner .setup ()
247260 site = web .TCPSite (runner , '0.0.0.0' , 8080 )
0 commit comments