Module: ProcessExecuter
- Defined in:
- lib/process_executer.rb,
lib/process_executer/errors.rb,
lib/process_executer/result.rb,
lib/process_executer/runner.rb,
lib/process_executer/options.rb,
lib/process_executer/version.rb,
lib/process_executer/destinations.rb,
lib/process_executer/options/base.rb,
lib/process_executer/monitored_pipe.rb,
lib/process_executer/destinations/io.rb,
lib/process_executer/destination_base.rb,
lib/process_executer/destinations/tee.rb,
lib/process_executer/destinations/close.rb,
lib/process_executer/destinations/stderr.rb,
lib/process_executer/destinations/stdout.rb,
lib/process_executer/destinations/writer.rb,
lib/process_executer/options/run_options.rb,
lib/process_executer/options/spawn_options.rb,
lib/process_executer/destinations/file_path.rb,
lib/process_executer/options/option_definition.rb,
lib/process_executer/destinations/file_path_mode.rb,
lib/process_executer/destinations/monitored_pipe.rb,
lib/process_executer/destinations/file_descriptor.rb,
lib/process_executer/destinations/child_redirection.rb,
lib/process_executer/options/spawn_and_wait_options.rb,
lib/process_executer/destinations/file_path_mode_perms.rb
Overview
rubocop:disable Layout/LineLength
Defined Under Namespace
Modules: Destinations, Options Classes: CommandError, DestinationBase, Error, FailedError, MonitoredPipe, ProcessIOError, Result, Runner, SignaledError, SpawnError, TimeoutError
Constant Summary collapse
- VERSION =
The current Gem version
'3.2.3'
Class Method Summary collapse
-
.run(*command, **options_hash) ⇒ ProcessExecuter::Result
Execute the given command as a subprocess blocking until it finishes.
-
.run_options(obj) ⇒ RunOptions
Convert a hash to a RunOptions object.
-
.run_with_options(command, options) ⇒ ProcessExecuter::Result
private
Run a command with the given options.
-
.spawn_and_wait(*command, **options_hash) ⇒ ProcessExecuter::Result
Run a command in a subprocess, wait for it to finish, then return the result.
-
.spawn_and_wait_options(obj) ⇒ SpawnAndWaitOptions
Convert a hash to a SpawnAndWaitOptions object.
-
.spawn_and_wait_with_options(command, options) ⇒ ProcessExecuter::Result
private
Run a command in a subprocess, wait for it to finish, then return the result.
-
.spawn_options(obj) ⇒ SpawnOptions
Convert a hash to a SpawnOptions object.
Class Method Details
.run(*command, **options_hash) ⇒ ProcessExecuter::Result
Execute the given command as a subprocess blocking until it finishes
Works just like spawn, but does the following in addition:
If nothing is specified for
out
, stdout is captured to aStringIO
object which can be accessed via the Result object inresult.options.out
. The same applies toerr
.out
anderr
are automatically wrapped in aProcessExecuter::MonitoredPipe
object so that any object that implements#write
(or an Array of such objects) can be given forout
anderr
.Raises one of the following errors unless
raise_errors
is explicitly set tofalse
:
* `ProcessExecuter::FailedError` if the command returns a non-zero
exitstatus
* `ProcessExecuter::SignaledError` if the command exits because of
an unhandled signal
* `ProcessExecuter::TimeoutError` if the command times out
If `raise_errors` is false, the returned Result object will contain the error.
Raises a
ProcessExecuter::ProcessIOError
if an exception is raised while collecting subprocess output. This can not be turned off.If a
logger
is provided, it will be used to log:
* The command that was executed and its status to `info` level
* The stdout and stderr output to `debug` level
By default, Logger.new(nil) is used for the logger.
This method takes two forms:
The command is executed via a shell when the command is given as a single string:
ProcessExecuter.run([env, ] command_line, options = {}) ->
ResultThe command is executed directly (bypassing the shell) when the command and it arguments are given as an array of strings:
ProcessExecuter.run([env, ] exe_path, *args, options = {}) ->
Result
Optional argument env
is a hash that affects ENV for the new process; see
Execution
Environment.
Argument options
is a hash of options for the new process. See the options listed below.
298 299 300 301 |
# File 'lib/process_executer.rb', line 298 def self.run(*command, **) = ProcessExecuter.() (command, ) end |
.run_options(obj) ⇒ RunOptions
Convert a hash to a RunOptions object
431 432 433 434 435 436 437 438 439 440 |
# File 'lib/process_executer.rb', line 431 def self.(obj) case obj when ProcessExecuter::Options::RunOptions obj when Hash ProcessExecuter::Options::RunOptions.new(**obj) else raise ArgumentError, "Expected a Hash or ProcessExecuter::Options::RunOptions but got a #{obj.class}" end end |
.run_with_options(command, options) ⇒ ProcessExecuter::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Run a command with the given options
313 314 315 |
# File 'lib/process_executer.rb', line 313 def self.(command, ) ProcessExecuter::Runner.new.call(command, ) end |
.spawn_and_wait(*command, **options_hash) ⇒ ProcessExecuter::Result
Run a command in a subprocess, wait for it to finish, then return the result
This method is a thin wrapper around Process.spawn and blocks until the command terminates.
A timeout may be specified with the :timeout_after
option. The command will be
sent the SIGKILL signal if it does not terminate within the specified timeout.
75 76 77 78 |
# File 'lib/process_executer.rb', line 75 def self.spawn_and_wait(*command, **) = ProcessExecuter.() (command, ) end |
.spawn_and_wait_options(obj) ⇒ SpawnAndWaitOptions
Convert a hash to a SpawnAndWaitOptions object
403 404 405 406 407 408 409 410 411 412 |
# File 'lib/process_executer.rb', line 403 def self.(obj) case obj when ProcessExecuter::Options::SpawnAndWaitOptions obj when Hash ProcessExecuter::Options::SpawnAndWaitOptions.new(**obj) else raise ArgumentError, "Expected a Hash or ProcessExecuter::Options::SpawnAndWaitOptions but got a #{obj.class}" end end |
.spawn_and_wait_with_options(command, options) ⇒ ProcessExecuter::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Run a command in a subprocess, wait for it to finish, then return the result
89 90 91 92 93 94 95 96 |
# File 'lib/process_executer.rb', line 89 def self.(command, ) begin pid = Process.spawn(*command, **.) rescue StandardError => e raise ProcessExecuter::SpawnError, "Failed to spawn process: #{e.}" end wait_for_process(pid, command, ) end |
.spawn_options(obj) ⇒ SpawnOptions
Convert a hash to a SpawnOptions object
375 376 377 378 379 380 381 382 383 384 |
# File 'lib/process_executer.rb', line 375 def self.(obj) case obj when ProcessExecuter::Options::SpawnOptions obj when Hash ProcessExecuter::Options::SpawnOptions.new(**obj) else raise ArgumentError, "Expected a Hash or ProcessExecuter::Options::SpawnOptions but got a #{obj.class}" end end |