Class: Instrumentality::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/instrumentality/executor.rb

Defined Under Namespace

Classes: ExecutorError

Class Method Summary collapse

Class Method Details

.execute(cmd, verbose = false) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
# File 'lib/instrumentality/executor.rb', line 10

def self.execute(cmd, verbose = false)
  Logger.log("Executing command: #{cmd}")
  if verbose
    system(cmd)
  else
    `#{cmd}`
  end
  status = $?.exitstatus
  raise ExecutorError, "Previous command execution failed with status: #{status}".red if status != 0
end

.execute_async(cmd) ⇒ Object



21
22
23
24
25
# File 'lib/instrumentality/executor.rb', line 21

def self.execute_async(cmd)
  Process.spawn("#{cmd}").tap do |pid|
    Logger.log("Spawned process (PID: #{pid}) from command: #{cmd}")
  end
end

.timeout(process, seconds = Constants::TIMEOUT) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/instrumentality/executor.rb', line 27

def self.timeout(process, seconds = Constants::TIMEOUT)
  pid = ""
  begin
    Timeout.timeout(seconds) do
      loop do
        pid = `ps aux | grep #{process}.app | grep -v grep | awk '{print $2}'`.strip
        break if pid != ""
      end
    end
  rescue Timeout::Error
    raise ExecutorError, "Timeout while trying to find #{process} process".red
  end
  pid.tap do |pid|
    Logger.log("Found process #{process} with PID: #{pid}")
  end
end