Class: Pwrake::Executor

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

Instance Method Summary collapse

Constructor Details

#initialize(selector, dir_class, id) ⇒ Executor

Returns a new instance of Executor.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pwrake/worker/executor.rb', line 5

def initialize(selector,dir_class,id)
  @selector = selector
  @id = id
  @out = Writer.instance
  @log = LogExecutor.instance
  @queue = FiberQueue.new
  @rd_list = []
  @dir = dir_class.new
  @dir.open
  @dir.open_messages.each{|m| @log.info(m)}
  @out.puts "#{@id}:open"

  r,w = IO.pipe
  @command_pipe_r = NBIO::Reader.new(@selector,r)
  @command_pipe_w = NBIO::Writer.new(@selector,w)
  @start_process_fiber = Fiber.new do
    while line = @queue.deq
      cmd = line
      while /\\$/ =~ line  # line continues
        line = @queue.deq
        break if !line
        cmd += line
      end
      break if @stopped
      cmd.chomp!
      if !cmd.empty?
        start_process(cmd)
      end
      Fiber.yield
    end
  end
end

Instance Method Details

#callback(rd, mode) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pwrake/worker/executor.rb', line 101

def callback(rd,mode)
  while s = rd.gets
    @out.puts "#{@id}:#{mode}:#{s.chomp}"
  end
  if rd.eof?
    @rd_list.delete(rd)
    if @rd_list.empty?  # process_end
      @thread = @pid = nil
      @log.info inspect_status
      @out.puts "#{@id}:z:#{exit_status}"
      @sh_in.close
      @sh_out.close
      @sh_err.close
      @start_process_fiber.resume # next process
    end
  end
rescue => exc
  @log.error(([exc.to_s]+exc.backtrace).join("\n"))
  stop
end

#closeObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pwrake/worker/executor.rb', line 43

def close
  if @thread
    @thread.join(15)
    sleep 0.1
  end
  @thread = Thread.new do
    @dir.close_messages.each{|m| @log.info(m)}
    @dir.close
  end
rescue => exc
  @log.error(([exc.to_s]+exc.backtrace).join("\n"))
end

#execute(cmd) ⇒ Object



64
65
66
67
68
# File 'lib/pwrake/worker/executor.rb', line 64

def execute(cmd)
  return if @stopped
  @queue.enq(cmd)
  @start_process_fiber.resume
end

#exit_statusObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/pwrake/worker/executor.rb', line 140

def exit_status
  s = @status
  case
  when s.signaled?
    if s.coredump?
      "core_dumped"
    else
      "killed:#{s.termsig}"
    end
  when s.stopped?
    "stopped:#{s.stopsig}"
  when s.exited?
    "#{s.exitstatus}"
  else
    "unknown:%#x" % s.to_i
  end
end

#inspect_statusObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/pwrake/worker/executor.rb', line 122

def inspect_status
  s = @status
  case
  when s.signaled?
    if s.coredump?
      "pid=#{s.pid} dumped core."
    else
      "pid=#{s.pid} was killed by signal #{s.termsig}"
    end
  when s.stopped?
    "pid=#{s.pid} was stopped by signal #{s.stopsig}"
  when s.exited?
    "pid=#{s.pid} exited normally. status=#{s.exitstatus}"
  else
    "unknown status %#x" % s.to_i
  end
end

#joinObject



56
57
58
59
60
61
62
# File 'lib/pwrake/worker/executor.rb', line 56

def join
  if @thread
    @thread.join(15)
  end
rescue => exc
  @log.error(([exc.to_s]+exc.backtrace).join("\n"))
end

#kill(sig) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/pwrake/worker/executor.rb', line 158

def kill(sig)
  stop
  if @pid
    Process.kill(sig,-@pid)
    @log.warn "Executor(id=#{@id})#kill pid=#{@pid} sig=#{sig}"
  end
end

#start_process(command) ⇒ Object



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

def start_process(command)
  return if @thread      # running
  return if !command     # empty queue
  @spawn_in, @sh_in = IO.pipe
  @sh_out, @spawn_out = IO.pipe
  @sh_err, @spawn_err = IO.pipe

  @pid = Kernel.spawn(command,
                      :in=>@spawn_in,
                      :out=>@spawn_out,
                      :err=>@spawn_err,
                      :chdir=>@dir.current,
                      :pgroup=>true
                     )
  @log.info "pid=#{@pid} started. command=#{command.inspect}"

  @thread = Thread.new do
    @pid2,@status = Process.waitpid2(@pid)
    @spawn_in.close
    @spawn_out.close
    @spawn_err.close
  end

  @rd_out = NBIO::Reader.new(@selector,@sh_out)
  @rd_err = NBIO::Reader.new(@selector,@sh_err)
  @rd_list = [@rd_out,@rd_err]

  Fiber.new{callback(@rd_err,"e")}.resume
  Fiber.new{callback(@rd_out,"o")}.resume
end

#stopObject



38
39
40
41
# File 'lib/pwrake/worker/executor.rb', line 38

def stop
  @stopped = true
  @queue.finish
end