From 8d4124d610b4703d79a808c6bcd4a856184d12cc Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Fri, 20 Mar 2015 11:58:42 +0000 Subject: Handle a child that cannot be terminated. Basic handling that is the same as for other process errors - just write to stderr. Add TODO for allowing the calling code to specify an error stream to allow other handling. In different circumstances, the calling code might want to raise an exception, write to the log, or do something else. At the moment this code assumes a cron context and writes to stderr, which will cause an email to be sent from the cron process, but that's not always the context the calling code is being run in. So we may be missing errors that are, for example, being written to stderr during the request cycle. In fact, this code may be called from the same place with different contexts (i.e. as part of a cron reindexing task and in the request cycle), so at the moment some substantial refactoring of the calling code would be required to make sure calls to this code handle errors appropriately for each context. --- lib/alaveteli_external_command.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lib/alaveteli_external_command.rb') diff --git a/lib/alaveteli_external_command.rb b/lib/alaveteli_external_command.rb index d32af4c90..ddf968f90 100644 --- a/lib/alaveteli_external_command.rb +++ b/lib/alaveteli_external_command.rb @@ -18,6 +18,9 @@ module AlaveteliExternalCommand # Run an external program, and return its output. # Standard error is suppressed unless the program # fails (i.e. returns a non-zero exit status). + # If the program fails, returns nil and writes any error to stderr. + # TODO: calling code should be able to specify error stream - may want to log it or + # otherwise act upon it. opts = {} if !args.empty? && args.last.is_a?(Hash) opts = args.last @@ -25,7 +28,12 @@ module AlaveteliExternalCommand program_path = find_program(program_name) xc = ExternalCommand.new(program_path, *args) - xc.run + begin + xc.run + rescue ExternalCommand::ChildUnterminated => e + $stderr.puts(e.message) + return nil + end if !xc.exited # Crash or timeout -- cgit v1.2.3