Class: Pwrake::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/branch/shell.rb

Constant Summary collapse

OPEN_LIST =
{}
BY_FIBER =
{}
@@profiler =
ShellProfiler.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chan, comm, task_q, opt = {}) ⇒ Shell

Returns a new instance of Shell.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pwrake/branch/shell.rb', line 25

def initialize(chan,comm,task_q,opt={})
  @chan = chan
  @id   = chan.id
  @host = chan.host
  @comm = comm
  @task_q = task_q
  @lock = DummyMutex.new
  @option = opt
  @work_dir = @option[:work_dir] || Dir.pwd
  @comm.shells[self] = true
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



37
38
39
# File 'lib/pwrake/branch/shell.rb', line 37

def host
  @host
end

#idObject (readonly)

Returns the value of attribute id.



37
38
39
# File 'lib/pwrake/branch/shell.rb', line 37

def id
  @id
end

#profileObject (readonly)

Returns the value of attribute profile.



37
38
39
# File 'lib/pwrake/branch/shell.rb', line 37

def profile
  @profile
end

#statusObject (readonly)

Returns the value of attribute status.



37
38
39
# File 'lib/pwrake/branch/shell.rb', line 37

def status
  @status
end

Class Method Details

.currentObject



21
22
23
# File 'lib/pwrake/branch/shell.rb', line 21

def self.current
  BY_FIBER[Fiber.current]
end

.profilerObject



17
18
19
# File 'lib/pwrake/branch/shell.rb', line 17

def self.profiler
  @@profiler
end

Instance Method Details

#backquote(*command) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/pwrake/branch/shell.rb', line 80

def backquote(*command)
  command = command.join(' ')
  @lock.synchronize do
    a = []
    _execute(command){|x| a << x}
    a.join("\n")
  end
end

#cd(dir = "") ⇒ Object



97
98
99
# File 'lib/pwrake/branch/shell.rb', line 97

def cd(dir="")
  _system("cd #{dir}") or die
end

#create_fiber(master_w) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/pwrake/branch/shell.rb', line 216

def create_fiber(master_w)
  @master_w = master_w
  if !@opened
    Log.warn "not opened: host=#{@host} id=#{@id}"
  end
  Fiber.new do
    BY_FIBER[Fiber.current] = self
    Log.debug "shell start id=#{@id} host=#{@host}"
    begin
      while task_str = @task_q.deq
        #Log.debug "task_str=#{task_str}"
        if /^(\d+):(.*)$/ =~ task_str
          task_id, task_name = $1.to_i, $2
        else
          raise RuntimeError, "invalid task_str: #{task_str}"
        end
        @task_id = task_id
        @task_name = task_name
        task = Rake.application[task_name]
        begin
          task.execute(task.arguments) if task.needed?
          result = "taskend:#{@id}:#{task.name}"
        rescue Exception=>e
          Rake.application.display_error_message(e)
          Log.error e
          result = "taskfail:#{@id}:#{task.name}"
          break if @exited
        ensure
          master_w.put_line result
        end
      end
      Log.debug "shell id=#{@id} fiber end"
      master_w.put_line "retire:#{@comm.id}"
      @comm.shells.delete(self)
      exit
      if @comm.shells.empty?
        @comm.dropout
      end
      @chan.halt
    rescue => e
      m = Log.bt(e)
      #$stderr.puts m
      Log.error(m)
    end
  end
end

#dieObject



101
102
103
# File 'lib/pwrake/branch/shell.rb', line 101

def die
  raise "Failed at #{@host}, id=#{@id}, cmd='#{@cmd}'"
end

#exitObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pwrake/branch/shell.rb', line 55

def exit
  if !@opened
    Log.debug "already exited: host=#{@host} id=#{@id}"
    return
  end
  @opened = false
  _puts("exit")
  if (s = _gets) == "exit"
    OPEN_LIST.delete(__id__)
    Log.debug("Shell#exit: recieve #{s.inspect}")
    true
  else
    Log.debug("Shell#exit: recieve #{s.inspect}")
    false
  end
rescue IOError,Errno::EPIPE => e
  Log.debug("Shell#exit: #{Log.bt(e)}")
  false
end

#finish_task_qObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/pwrake/branch/shell.rb', line 263

def finish_task_q
  @task_q.finish
  #Log.debug "finish_task_q: @task_q=#{@task_q.inspect}"
  while task_str = @task_q.deq_nonblock
    if /^(\d+):(.*)$/ =~ task_str
      task_id, task_name = $1.to_i, $2
    else
      raise RuntimeError, "invalid task_str: #{task_str}"
    end
    @master_w.put_line "taskfail:#{@id}:#{task_name}"
    Log.warn "unexecuted task: #{result}"
  end
  @chan.halt
end

#openObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pwrake/branch/shell.rb', line 39

def open
  if @opened
    Log.warn "already opened: host=#{@host} id=#{@id}"
    return
  end
  @opened = true
  _puts("open")
  if (s = _gets) == "open"
    OPEN_LIST[__id__] = self
    true
  else
    Log.error("Shell#open failed: recieve #{s.inspect}")
    false
  end
end

#set_current_task(task_id, task_name) ⇒ Object



75
76
77
78
# File 'lib/pwrake/branch/shell.rb', line 75

def set_current_task(task_id,task_name)
  @task_id = task_id
  @task_name = task_name
end

#system(*command) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/pwrake/branch/shell.rb', line 89

def system(*command)
  command = command.join(' ')
  @lock.synchronize do
    _execute(command){|x| print x+"\n"}
  end
  @status == 0
end