diff options
author | Louise Crow <louise.crow@gmail.com> | 2015-03-20 11:58:42 +0000 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2015-03-23 18:28:28 +0000 |
commit | 8d4124d610b4703d79a808c6bcd4a856184d12cc (patch) | |
tree | 651fd05a50afcc3cccb0296e3720015490b2d1da /lib | |
parent | c050a60b2e27cabfd8f1e753abb8f595146c0ffd (diff) |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/alaveteli_external_command.rb | 10 |
1 files changed, 9 insertions, 1 deletions
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 |