Class: Pwrake::Executor

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

Constant Summary collapse

LIST =
{}
CHARS =
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
TLEN =
32

Instance Method Summary collapse

Constructor Details

#initialize(dir_class, id, shell_cmd, shell_rc) ⇒ Executor

Returns a new instance of Executor.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pwrake/worker/executor.rb', line 9

def initialize(dir_class,id,shell_cmd,shell_rc)
  @id = id
  @shell_rc = shell_rc
  @shell_cmd = shell_cmd || ENV['SHELL'] || '/bin/sh'
  @terminator = ""
  TLEN.times{ @terminator << CHARS[rand(CHARS.length)] }
  @out = Writer.instance
  @log = LogExecutor.instance
  @queue = Queue.new
  @dir = dir_class.new
  @spawn_in, @sh_in = IO.pipe
  @sh_out, @spawn_out = IO.pipe
  @sh_err, @spawn_err = IO.pipe
  LIST[@id] = self
  @exec_thread = start_exec_thread
end

Instance Method Details

#closeObject



142
143
144
# File 'lib/pwrake/worker/executor.rb', line 142

def close
  execute(nil)  # threads end
end

#execute(cmd) ⇒ Object



26
27
28
# File 'lib/pwrake/worker/executor.rb', line 26

def execute(cmd)
  @queue.enq(cmd)
end

#joinObject



146
147
148
149
# File 'lib/pwrake/worker/executor.rb', line 146

def join
  LIST.delete(@id)
  @exec_thread.join(15) if @exec_thread
end

#kill(sig) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pwrake/worker/executor.rb', line 151

def kill(sig)
  @queue.clear
  if @pid
    # kill process group
    s = `ps ho pid --ppid=#{@pid}`
    s.each_line do |x|
      pid = x.to_i
      Process.kill(sig,pid)
      @log.warn "Executor(id=#{@id})#kill pid=#{pid} sig=#{sig}"
    end
    if s.empty?
      @log.warn "Executor(id=#{@id})#kill nothing killed"
    end
  end
  @spawn_out.flush
  @spawn_err.flush
end

#run(cmd) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pwrake/worker/executor.rb', line 74

def run(cmd)
  case cmd
  when Proc
    cmd.call
  when "cd"
    @dir.cd
    run_command("cd "+@dir.current)
    #
  when /^cd\s+(.*)$/
    @dir.cd($1)
    run_command("cd "+@dir.current)
    #
  when /^exit\b/
    close
    @out.puts "#{@id}:exit"
    #
  when String
    run_command(cmd)
    #
  else
    raise RuntimeError,"invalid cmd: #{cmd.inspect}"
  end
end

#run_command(cmd) ⇒ Object



102
103
104
# File 'lib/pwrake/worker/executor.rb', line 102

def run_command(cmd)
  run_command_main(cmd){|s| @out.puts s}
end

#run_command_main(cmd) {|"#{@id}:z:#{status}"| ... } ⇒ Object

Yields:

  • ("#{@id}:z:#{status}")


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
133
134
135
136
137
138
139
140
# File 'lib/pwrake/worker/executor.rb', line 106

def run_command_main(cmd)
  if /\\$/ =~ cmd  # command line continues
    @sh_in.puts(cmd)
    @sh_in.flush
    return
  end
  term = "\necho '#{@terminator}':$? \necho '#{@terminator}' 1>&2"
  @sh_in.puts(cmd+term)
  @sh_in.flush
  status = ""
  io_set = [@sh_out,@sh_err]
  loop do
    io_sel, = IO.select(io_set,nil,nil)
    for io in io_sel
      s = io.gets.chomp
      case io
      when @sh_out
        if s[0,TLEN] == @terminator
          status = s[TLEN+1..-1]
          io_set.delete(@sh_out)
        else
          yield "#{@id}:o:"+s
        end
      when @sh_err
        if s[0,TLEN] == @terminator
          io_set.delete(@sh_err)
        else
          yield "#{@id}:e:"+s
        end
      end
    end
    break if io_set.empty?
  end
  yield "#{@id}:z:#{status}"
end

#run_rc(cmd) ⇒ Object



98
99
100
# File 'lib/pwrake/worker/executor.rb', line 98

def run_rc(cmd)
  run_command_main(cmd){|s| @log.info "<"+s if @log}
end

#start_exec_threadObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pwrake/worker/executor.rb', line 30

def start_exec_thread
  Thread.new do
    begin
      @dir.open
      @dir.open_messages.each{|m| @log.info(m)}
      @pid = Kernel.spawn(@shell_cmd,
                          :out=>@spawn_out,
                          :err=>@spawn_err,
                          :in=>@spawn_in,
                          :chdir=>@dir.current)
      begin
        @out.puts "#{@id}:open"
        @shell_rc.each do |cmd|
          run_rc(cmd)
        end
        while cmd = @queue.deq
          run(cmd)
        end
        @sh_in.puts("exit")
        @sh_in.flush
      ensure
        status = nil
        begin
          Timeout.timeout(5){
            pid,status = Process.waitpid2(@pid)
          }
        rescue => exc
          @log.error ([exc.to_s]+exc.backtrace).join("\n")
          @log.info("#{@id}:kill INT sh @pid=#{@pid}")
          Process.kill("INT",@pid)
          pid,status = Process.waitpid2(@pid)
        end
        @log.info("shell exit status: "+status.inspect)
      end
    rescue => exc
      @out.puts "#{@id}:exc:#{exc}"
      @log.error ([exc.to_s]+exc.backtrace).join("\n")
    ensure
      @dir.close_messages.each{|m| @log.info(m)}
      @dir.close
    end
  end
end