Class: SystemJobs

Inherits:
JobQueue show all
Defined in:
lib/jobqueue.rb

Overview

Special class for runing operating system commands with Ruby’s system call

Instance Attribute Summary

Attributes inherited from JobQueue

#threads, #workers

Instance Method Summary collapse

Methods inherited from JobQueue

#initialize, maxnumber_of_processors, #push

Constructor Details

This class inherits a constructor from JobQueue

Instance Method Details

#runObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jobqueue.rb', line 94

def run
  if :off == @debug then
    $stdout.reopen("/dev/null", "w")
    $stderr.reopen("/dev/null", "w")
  end

  @threads = (1..@workers).map {|i|
    Thread.new(@queue,@debug) {|queue,debug|
      Thread.current.abort_on_exception = true
      until ( queue == ( task = queue.deq ) )
        case debug
        when :buffered
          # output is buffered until jobs are finished
          stderr_and_stdout,waitThr = Open3.capture2e(task.first)
          puts stderr_and_stdout

        when :flushed
          # stdout and stderr are read + printed in parallel
          _, stderr, stdout, waitThr = Open3.popen3(task.first)
          # Create a thread to read from each stream
          [stdout,stderr].map {|stream|
            Thread.new(stream,debug) {|_stream|
              Thread.current.abort_on_exception = true
              puts $_ until _stream.gets.nil?
            }
          }.map(&:join)

        when :off
          # no output at all (switched off globally at the beginning of this method)'
          system(task.first)
        else
          raise ArgumentError,"Unknown debug mode '#{debug}'!"
        end
      end
    }
  }
  @threads.size.times { @queue.enq @queue}
  @threads.map(&:join)
end