Class: Loompa

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

Defined Under Namespace

Classes: Child, Children

Constant Summary collapse

@@children =

class variable holding all the 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, log_method = DefaultLogger) ⇒ Loompa

Returns a new instance of Loompa.



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

def initialize(forks_to_run, log_method = DefaultLogger)
  @min_forks = 1
  @max_forks = forks_to_run
  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

#max_forks=(value) ⇒ Object (writeonly)

Sets the attribute max_forks

Parameters:

  • value

    the value to set the attribute max_forks to.



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

def max_forks=(value)
  @max_forks = value
end

#on_child_exit(&block) ⇒ Object

A block to be executed upon exiting a child process.

block

block The block that will be executed upon termination of a child process



145
146
147
148
149
150
# File 'lib/loompa.rb', line 145

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

#on_child_start(&block) ⇒ Object

A block to be executed just before calling the block a child will be executing

block

block The block that will be executed



135
136
137
138
139
140
# File 'lib/loompa.rb', line 135

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

Class Method Details

.loggerObject



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

def logger
  @logger
end

.logger=(log_meth) ⇒ Object



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

def logger=(log_meth)
  @logger = log_meth
end

Instance Method Details

#interruptObject

Sends the TERM signal to all child processes via their pids



211
212
213
# File 'lib/loompa.rb', line 211

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

#start(&block) ⇒ Object

Starts the child processes, the number of which is determined by the @max_forks variable

block

&block The block that will be executed by the child processes



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
186
187
188
189
190
191
192
193
194
# File 'lib/loompa.rb', line 155

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

sets the @flag to :exit_loop, essentially stopping the parent and child processes since the loop will die and all children will receive the close() call



198
199
200
# File 'lib/loompa.rb', line 198

def stop()
  @flag = :exit_loop
end

#terminateObject

Calls the close method on each child which sets its status to :closed



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

def terminate()
  raise "Cannot terminate while still in the loop" if @flag == :in_loop
  @@children.each do |c|
    c.close
  end
end