class Enparallel::Task

Attributes

stderr[RW]
stdout[RW]

Public Class Methods

new(command, input) click to toggle source
# File lib/enparallel/task.rb, line 6
def initialize(command, input)
    @command = command
    @input = input
    @running = false
    @stdout = ''
    @stderr = ''
    @ran_at = nil
    @exit_status = nil
end

Public Instance Methods

char() click to toggle source
# File lib/enparallel/task.rb, line 28
def char
    if @running
        'R'
    elsif @exit_status.nil?
        'S'
    elsif has_succeeded?
        'D'.green
    else
        'F'.red
    end
end
has_succeeded?() click to toggle source
# File lib/enparallel/task.rb, line 56
def has_succeeded?
    raise 'Task not resolved' if @ran_at.nil?
    @exit_status == 0
end
run() click to toggle source
# File lib/enparallel/task.rb, line 40
def run
    @running = true
    @ran_at = Time.now

    Open3.popen3(command_line_safe) do |stdin, stdout, stderr, thread|
        @stdout = stdout.read.chomp
        @stderr = stderr.read.chomp
        @exit_status = thread.value.exitstatus
    end
rescue => e
    @stderr = e.message
    @exit_status = 1
ensure
    @running = false
end
to_s() click to toggle source
# File lib/enparallel/task.rb, line 16
def to_s
    document = SOML::Document.new

    document.add('CommandLine', command_line_unsafe)
    document.add('ExitStatus', @exit_status)
    document.add('RanAt', @ran_at)
    document.add('StandardOutput', @stdout) unless @stdout.empty?
    document.add('StandardError', @stderr) unless @stderr.empty?

    document.to_s
end

Private Instance Methods

command_line_safe() click to toggle source
# File lib/enparallel/task.rb, line 63
def command_line_safe
    @command.interpolate_safe(@input)
end
command_line_unsafe() click to toggle source
# File lib/enparallel/task.rb, line 67
def command_line_unsafe
    @command.interpolate_unsafe(@input)
end