Class: RbbtProcessQueue::RbbtProcessQueueWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/util/concurrency/processes/worker.rb

Defined Under Namespace

Classes: Respawn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, callback_queue = nil, cleanup = nil, respawn = false, &block) ⇒ RbbtProcessQueueWorker

Returns a new instance of RbbtProcessQueueWorker.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 113

def initialize(queue, callback_queue = nil, cleanup = nil, respawn = false, &block)
  @queue, @callback_queue, @cleanup, @block = queue, callback_queue, cleanup, block

  @pid = Process.fork do
    Misc.pre_fork

    @cleanup.call if @cleanup
    @queue.close_write 

    if @callback_queue
      Misc.purge_pipes(@callback_queue.swrite) 
      @callback_queue.close_read 
    else
      Misc.purge_pipes
    end

    if respawn
      run_with_respawn respawn
    else
      run
    end
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



4
5
6
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4

def block
  @block
end

#callback_queueObject (readonly)

Returns the value of attribute callback_queue.



4
5
6
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4

def callback_queue
  @callback_queue
end

#cleanupObject (readonly)

Returns the value of attribute cleanup.



4
5
6
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4

def cleanup
  @cleanup
end

#pidObject (readonly)

Returns the value of attribute pid.



4
5
6
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4

def pid
  @pid
end

#queueObject (readonly)

Returns the value of attribute queue.



4
5
6
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 4

def queue
  @queue
end

Instance Method Details

#abortObject



142
143
144
145
146
147
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 142

def abort
  begin
    Process.kill :INT, @pid
  rescue
  end
end

#done?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 149

def done?
  begin
    Process.waitpid @pid, Process::WNOHANG
  rescue Errno::ECHILD
    true
  rescue
    false
  end
end

#joinObject

Raises:



137
138
139
140
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 137

def join
  Process.waitpid @pid
  raise ProcessFailed unless $?.success?
end

#runObject



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
37
38
39
40
41
42
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 8

def run
  begin
    Signal.trap(:INT){ 
      Kernel.exit! -1
    }

    @stop = false
    Signal.trap(:USR1){ 
      @stop = true
    }

    loop do
      p = @queue.pop
      next if p.nil?
      raise p if Exception === p
      raise p.first if Exception === p.first
      res = @block.call *p
      @callback_queue.push res if @callback_queue
      raise Respawn if @stop
    end
    Kernel.exit! 0
  rescue Respawn
    Kernel.exit! 28
  rescue ClosedStream
  rescue Aborted, Interrupt
    Log.info "Worker #{Process.pid} aborted"
  rescue Exception
    Log.exception $!
    @callback_queue.push($!) if @callback_queue
    Kernel.exit! -1
  ensure
    @callback_queue.close_write if @callback_queue 
  end
  Kernel.exit! 0
end

#run_with_respawn(multiplier = nil) ⇒ Object



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
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
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rbbt/util/concurrency/processes/worker.rb', line 44

def run_with_respawn(multiplier = nil)
  multiplier = case multiplier
               when String
                 multiplier.to_s
               when Fixnum
                 multiplier.to_i
               else
                 3
               end

  status = nil
  begin
    @current = Process.fork do
      run
    end
    @asked = false

    initial = Misc.memory_use(Process.pid)
    memory_cap = multiplier * initial
    Log.medium "Worker #{Process.pid} started with #{@current} -- initial: #{initial} - multiplier: #{multiplier} - cap: #{memory_cap}"

    @monitor_thread = Thread.new do
      begin
        while true
          current = Misc.memory_use(@current) 
          if current > memory_cap and not @asked
            Log.medium "Worker #{@current} for #{Process.pid} asked to respawn -- initial: #{initial} - multiplier: #{multiplier} - cap: #{memory_cap} - current: #{current}"
            RbbtSemaphore.synchronize(@callback_queue.write_sem) do
              Process.kill "USR1", @current
            end
            @asked = true
          end
          sleep 3 + rand(5)
        end
      rescue
        Log.exception $!
      end
    end

    while true
      pid, status = Process.waitpid2 @current
      code = status.to_i >> 8 
      break unless code == 28
      @current = Process.fork do
        run
      end
      @asked = false
      Log.high "Worker #{Process.pid} respawning to #{@current}"
    end
  rescue Aborted, Interrupt
    Log.warn "Worker #{Process.pid} aborted"
    Kernel.exit! 0
    Process.kill "INT", @current 
  rescue Exception
    Log.exception $!
    raise $!
  ensure
    @monitor_thread.kill
    Process.kill "INT", @current if Misc.pid_exists? @current
    @callback_queue.close_write if @callback_queue 
  end

  if status
    Kernel.exit! status.to_i >> 8
  else
    Kernel.exit! -1
  end
end