Class: ChupaText::ExternalCommand
- Inherits:
-
Object
- Object
- ChupaText::ExternalCommand
- Includes:
- Loggable
- Defined in:
- lib/chupa-text/external-command.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .default_limit_as ⇒ Object
- .default_limit_as=(as) ⇒ Object
- .default_limit_cpu ⇒ Object
- .default_limit_cpu=(cpu) ⇒ Object
- .default_timeout ⇒ Object
- .default_timeout=(timeout) ⇒ Object
Instance Method Summary collapse
- #exist? ⇒ Boolean
-
#initialize(path) ⇒ ExternalCommand
constructor
A new instance of ExternalCommand.
- #run(*arguments) ⇒ Object
Constructor Details
#initialize(path) ⇒ ExternalCommand
Returns a new instance of ExternalCommand.
62 63 64 |
# File 'lib/chupa-text/external-command.rb', line 62 def initialize(path) @path = Pathname.new(path) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
61 62 63 |
# File 'lib/chupa-text/external-command.rb', line 61 def path @path end |
Class Method Details
.default_limit_as ⇒ Object
45 46 47 |
# File 'lib/chupa-text/external-command.rb', line 45 def default_limit_as @default_limit_as || limit_env("AS") end |
.default_limit_as=(as) ⇒ Object
49 50 51 |
# File 'lib/chupa-text/external-command.rb', line 49 def default_limit_as=(as) @default_limit_as = as end |
.default_limit_cpu ⇒ Object
37 38 39 |
# File 'lib/chupa-text/external-command.rb', line 37 def default_limit_cpu @default_limit_cpu || limit_env("CPU") end |
.default_limit_cpu=(cpu) ⇒ Object
41 42 43 |
# File 'lib/chupa-text/external-command.rb', line 41 def default_limit_cpu=(cpu) @default_limit_cpu = cpu end |
.default_timeout ⇒ Object
29 30 31 |
# File 'lib/chupa-text/external-command.rb', line 29 def default_timeout @default_timeout || ENV["CHUPA_TEXT_EXTERNAL_COMMAND_TIMEOUT"] end |
.default_timeout=(timeout) ⇒ Object
33 34 35 |
# File 'lib/chupa-text/external-command.rb', line 33 def default_timeout=(timeout) @default_timeout = timeout end |
Instance Method Details
#exist? ⇒ Boolean
97 98 99 100 101 102 103 104 105 |
# File 'lib/chupa-text/external-command.rb', line 97 def exist? if @path.absolute? @path.file? and @path.executable? else (ENV['PATH'] || "").split(File::PATH_SEPARATOR).any? do |path| (Pathname.new(path) + @path)..exist? end end end |
#run(*arguments) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/chupa-text/external-command.rb', line 66 def run(*arguments) if arguments.last.is_a?(Hash) = arguments.pop else = {} end data = [:data] pid = spawn([:env] || {}, @path.to_s, *arguments, ([:spawn_options], data)) if data soft_timeout = data.timeout else soft_timeout = nil end status = nil begin status = wait_process(pid, [:timeout], soft_timeout) ensure unless status begin Process.kill(:KILL, pid) Process.waitpid(pid) rescue SystemCallError end end end status.success? end |