Class: Mutagem::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/mutagem/task.rb

Overview

A simple external process management wrapper

Examples:


cmd = "diff file1.txt file2.txt"
task = Mutagem::Task.new(cmd)
task.join

if (task.exitstatus == 0)
  puts "files match"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Task

Create a new Task

Parameters:

  • cmd (String)

    the cmd to execute



23
24
25
26
# File 'lib/mutagem/task.rb', line 23

def initialize(cmd)
  $stdout.sync = true
  @cmd = cmd
end

Instance Attribute Details

#cmdString (readonly)

Returns command to run.

Returns:

  • (String)

    command to run



18
19
20
# File 'lib/mutagem/task.rb', line 18

def cmd
  @cmd
end

Instance Method Details

#exceptionException

Returns exception returned if one is thrown by Task.

Returns:

  • (Exception)

    exception returned if one is thrown by Task



44
45
46
# File 'lib/mutagem/task.rb', line 44

def exception
  @exception
end

#exitstatusObject

Returns subprocess exit status.

Returns:

  • subprocess exit status



34
35
36
# File 'lib/mutagem/task.rb', line 34

def exitstatus
  @exitstatus
end

#outputArray

Returns array of strings from the subprocess output.

Returns:

  • (Array)

    array of strings from the subprocess output



29
30
31
# File 'lib/mutagem/task.rb', line 29

def output
  @output
end

#pidObject

Returns subprocess pid.

Returns:

  • subprocess pid



39
40
41
# File 'lib/mutagem/task.rb', line 39

def pid
  @pid
end

#runObject Also known as: join

run the cmd



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mutagem/task.rb', line 49

def run
  pipe = IO.popen(@cmd + " 2>&1")
  @pid = pipe.pid
  begin
    @output = pipe.readlines
    pipe.close
    @exitstatus = $?.exitstatus
  rescue => e
    @exception = e
  end
end