Class: Loompa

Inherits:
Object
  • Object
show all
Includes:
DefaultLogger
Defined in:
lib/loompa.rb

Defined Under Namespace

Classes: Child, Children

Constant Summary collapse

@@children =
Children.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DefaultLogger

debug, error, info

Constructor Details

#initialize(forks_to_run, port, log_method = DefaultLogger) ⇒ Loompa

Returns a new instance of Loompa.



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

def initialize(forks_to_run, port, log_method = DefaultLogger)
  @handle_signal = false
  @min_forks = 1
  @max_forks = forks_to_run
  @port = port
  @child_count = 0
  Loompa.logger = log_method
end

Instance Attribute Details

#child_countObject (readonly)

Returns the value of attribute child_count.



22
23
24
# File 'lib/loompa.rb', line 22

def child_count
  @child_count
end

#handle_signalObject

Returns the value of attribute handle_signal.



109
110
111
# File 'lib/loompa.rb', line 109

def handle_signal
  @handle_signal
end

#max_serversObject

Returns the value of attribute max_servers.



109
110
111
# File 'lib/loompa.rb', line 109

def max_servers
  @max_servers
end

#on_child_exit(&block) ⇒ Object



139
140
141
142
143
144
# File 'lib/loompa.rb', line 139

def on_child_exit(&block)
  if block == nil then
    raise "block required"
  end
  @on_child_exit = block
end

#on_child_start(&block) ⇒ Object



132
133
134
135
136
137
# File 'lib/loompa.rb', line 132

def on_child_start(&block)
  if block == nil then
    raise "block required"
  end
  @on_child_start = block
end

Class Method Details

.loggerObject



112
113
114
# File 'lib/loompa.rb', line 112

def logger
  @logger
end

.logger=(log_meth) ⇒ Object



116
117
118
# File 'lib/loompa.rb', line 116

def logger=(log_meth)
  @logger = log_meth
end

Instance Method Details

#closeObject



187
188
189
190
191
192
193
194
# File 'lib/loompa.rb', line 187

def close()
  if @flag != :out_of_loop then
    raise "close() must be called out of start() loop"
  end
  @socks.each do |s|
    s.close
  end
end

#interruptObject



206
207
208
# File 'lib/loompa.rb', line 206

def interrupt()
  Process.kill "TERM", *(@@children.pids) rescue nil
end

#start(&block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/loompa.rb', line 146

def start(&block)
  if block == nil then
    raise "block required"
  end
  (@min_forks - @@children.size).times do
    make_child block
  end
  @flag = :in_loop
  while @flag == :in_loop do
    log = false
    r, = IO.select(@@children.fds, nil, nil, 1)
    if r then
      log = true
      r.each do |f|
        c = @@children.by_fd f
        c.event f.gets
      end
    end
    as = @@children.active.size
    @@children.cleanup if @@children.size > as
    break if @flag != :in_loop
    n = 0
    if as < @min_forks then
      n = @min_forks - as
    else
      if @@children.idle.size <= 2 then
        n = 2
      end
    end
    if as + n > @max_forks then
      n = @max_forks - as
    end
    #Loompa.logger.debug "p: max:#{@max_forks}, min:#{@min_forks}, cur:#{as}, idle:#{@@children.idle.size}: new:#{n}" if n > 0 or log
    n.times do
    	make_child block
    end
  end
  @flag = :out_of_loop
  terminate
end

#stopObject



196
197
198
# File 'lib/loompa.rb', line 196

def stop()
  @flag = :exit_loop
end

#terminateObject



200
201
202
203
204
# File 'lib/loompa.rb', line 200

def terminate()
  @@children.each do |c|
    c.close
  end
end