Class: Heathen::Executioner

Inherits:
Object
  • Object
show all
Defined in:
lib/heathen/executioner.rb

Overview

An Executioner object will execute the given command, storing exit status, STDOUT and STDERR for perusal.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log) ⇒ Executioner


9
10
11
# File 'lib/heathen/executioner.rb', line 9

def initialize(log)
  @logger = log
end

Instance Attribute Details

#last_commandObject (readonly)

Returns the value of attribute last_command


6
7
8
# File 'lib/heathen/executioner.rb', line 6

def last_command
  @last_command
end

#last_exit_statusObject (readonly)

Returns the value of attribute last_exit_status


6
7
8
# File 'lib/heathen/executioner.rb', line 6

def last_exit_status
  @last_exit_status
end

#last_messagesObject (readonly)

Returns the value of attribute last_messages


6
7
8
# File 'lib/heathen/executioner.rb', line 6

def last_messages
  @last_messages
end

#loggerObject (readonly)

Returns the value of attribute logger


6
7
8
# File 'lib/heathen/executioner.rb', line 6

def logger
  @logger
end

#stderrObject (readonly)

Returns the value of attribute stderr


6
7
8
# File 'lib/heathen/executioner.rb', line 6

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout


6
7
8
# File 'lib/heathen/executioner.rb', line 6

def stdout
  @stdout
end

Instance Method Details

#_execute(*argv, options) ⇒ Object

Executes the given argument vector with Open3.popen3. Returns the pid and exit status as Numeric, stdout and stderr as Strings.


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/heathen/executioner.rb', line 50

def _execute(*argv, options)

  builder = java.lang.ProcessBuilder.new
  builder.command(argv)

  if options[:dir]
    dir = java.io.File.new(options[:dir])
    builder.directory(dir)
  end

  process = builder.start()

  # Dirty hack, works on UNIX only.
  pid = if process.is_a?(Java::JavaLang::UNIXProcess)
    prop = process.get_class.get_declared_field('pid')
    prop.set_accessible true
    prop.get_int(process)
  end

  logger.info "[#{pid}] spawn '#{argv.join(' ')}'"

  stdout = process.get_input_stream.to_io
  stderr = process.get_error_stream.to_io

  if options[:binary]
    stdout.binmode
    stderr.binmode
  end

  wait_thr = Thread.new { process.wait_for; process.exit_value }

  out = Thread.new { stdout.read }.value
  err = Thread.new { stderr.read }.value

  [pid, wait_thr.value, out, err]
end

#execute(*argv) ⇒ Object

Executes the given command.

argv[0] is the command to run
argv[1+] are the command arguments
argv[n] can be a [Hash] of options for the execution:
   :binary - STDOUT, STDERR are expected to be binary, so shouldn't be logged
   :dir    - execution directory

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/heathen/executioner.rb', line 19

def execute(*argv)
  options = argv.last.class == Hash ? argv.pop : {}

  started = Time.now.to_f

  command = argv.map(&:to_s)

  pid, status, @stdout, @stderr = _execute(*command, options)

  elapsed = Time.now.to_f - started

  if status != 0
    logger.error "[#{pid}] exited with status #{status.inspect}"
  end
  logger.info("[#{pid}] completed in %02.4f" % elapsed)

  logger.info "  stdout: '#@stdout'\n" unless @stdout.empty? unless options[:binary]
  logger.info "  stderr: '#@stderr'\n" unless @stderr.empty?

  @last_exit_status = status
  @last_messages = {stdout: @stdout, stderr: @stderr}
  @last_command = command.join(' ')

  return status
end

#quartering(heretics) ⇒ Object

Executes tasks in parallel


120
121
122
123
124
125
126
127
128
# File 'lib/heathen/executioner.rb', line 120

def quartering(heretics)
  @heretics = heretics
  parallel  = (@heretics.size > 4 ? 4 : @heretics.size)

  parallel.times.collect do
    guilty = @heretics.shift
    Thread.fork { slaughter guilty }
  end.map(&:join)
end