Class: ChupaText::ExternalCommand

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/chupa-text/external-command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#pathObject (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_asObject



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_cpuObject



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_timeoutObject



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

Returns:

  • (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).expand_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)
    options = arguments.pop
  else
    options = {}
  end
  data = options[:data]
  pid = spawn(options[:env] || {},
              @path.to_s,
              *arguments,
              spawn_options(options[:spawn_options], data))
  if data
    soft_timeout = data.timeout
  else
    soft_timeout = nil
  end
  status = nil
  begin
    status = wait_process(pid, options[:timeout], soft_timeout)
  ensure
    unless status
      begin
        Process.kill(:KILL, pid)
        Process.waitpid(pid)
      rescue SystemCallError
      end
    end
  end
  status.success?
end