Class: Forkme

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

Direct Known Subclasses

Child

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, suppress_exceptions = false, log_method = DefaultLogger) ⇒ Forkme

Returns a new instance of Forkme.



23
24
25
26
27
28
# File 'lib/forkme/forkme.rb', line 23

def initialize(forks_to_run, suppress_exceptions = false, log_method = DefaultLogger)
  @min_forks = 1
  @max_forks = forks_to_run
  @suppress_exceptions = suppress_exceptions
  Forkme.logger = log_method
end

Instance Attribute Details

#child_countObject (readonly)

Returns the value of attribute child_count.



3
4
5
# File 'lib/forkme/forkme.rb', line 3

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.



5
6
7
# File 'lib/forkme/forkme.rb', line 5

def max_forks=(value)
  @max_forks = value
end

#on_child_exit_blk=(value) ⇒ Object (writeonly)

Sets the attribute on_child_exit_blk

Parameters:

  • value

    the value to set the attribute on_child_exit_blk to.



5
6
7
# File 'lib/forkme/forkme.rb', line 5

def on_child_exit_blk=(value)
  @on_child_exit_blk = value
end

#on_child_start_blk=(value) ⇒ Object (writeonly)

Sets the attribute on_child_start_blk

Parameters:

  • value

    the value to set the attribute on_child_start_blk to.



5
6
7
# File 'lib/forkme/forkme.rb', line 5

def on_child_start_blk=(value)
  @on_child_start_blk = value
end

#suppress_exceptionsObject

Returns the value of attribute suppress_exceptions.



4
5
6
# File 'lib/forkme/forkme.rb', line 4

def suppress_exceptions
  @suppress_exceptions
end

Class Method Details

.loggerObject



10
11
12
# File 'lib/forkme/forkme.rb', line 10

def logger
  @logger
end

.logger=(log_meth) ⇒ Object



14
15
16
# File 'lib/forkme/forkme.rb', line 14

def logger=(log_meth)
  @logger = log_meth
end

.start(forks_to_run, suppress_exceptions = false, log_method = DefaultLogger, &block) ⇒ Object



19
20
21
# File 'lib/forkme/forkme.rb', line 19

def self.start(forks_to_run, suppress_exceptions = false, log_method = DefaultLogger, &block)
  self.new(forks_to_run, suppress_exceptions, log_method).start(block)
end

Instance Method Details

#interruptObject

Sends the TERM signal to all child processes via their pids



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

def interrupt()
  Process.kill "TERM", *(@@children.pids) rescue nil
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



45
46
47
48
# File 'lib/forkme/forkme.rb', line 45

def on_child_exit(&block)
  raise "block required" unless block_given?
  @on_child_exit_blk = 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



37
38
39
40
# File 'lib/forkme/forkme.rb', line 37

def on_child_start(&block)
  raise "block required" unless block_given?
  @on_child_start_blk = block
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



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
# File 'lib/forkme/forkme.rb', line 53

def start(&block)
  raise "block required" unless block_given?

  (@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
    #Forkme.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



95
96
97
98
# File 'lib/forkme/forkme.rb', line 95

def stop()
  @flag = :exit_loop
  terminate
end

#terminateObject

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



101
102
103
104
105
106
# File 'lib/forkme/forkme.rb', line 101

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