loompa 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/Rakefile +1 -1
  2. data/lib/loompa.rb +38 -18
  3. data/loompa.gemspec +3 -3
  4. metadata +5 -5
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('loompa', '0.0.2') do |p|
5
+ Echoe.new('loompa', '0.0.3') do |p|
6
6
  p.description = "Manage a fork pool"
7
7
  p.url = "http://github.com/pmamediagroup/loompa"
8
8
  p.author = "Tracey Eubanks"
data/lib/loompa.rb CHANGED
@@ -80,7 +80,7 @@ class Loompa
80
80
  when "connect" then @status = :connect
81
81
  when "disconnect" then @status = :idle
82
82
  else
83
- $stderr.puts "unknown status: #{s}"
83
+ Loompa.logger.error "unknown status: #{s}"
84
84
  end
85
85
  end
86
86
  end
@@ -106,8 +106,9 @@ class Loompa
106
106
  end
107
107
 
108
108
  attr_writer :on_child_start, :on_child_exit, :max_forks
109
- attr_accessor :max_servers, :handle_signal
110
109
 
110
+ # These class methods actually set up the logger that's used
111
+ # to print out useful information
111
112
  class << self
112
113
  def logger
113
114
  @logger
@@ -118,24 +119,29 @@ class Loompa
118
119
  end
119
120
  end
120
121
 
121
- def initialize(forks_to_run, port, log_method = DefaultLogger)
122
- @handle_signal = false
122
+ def initialize(forks_to_run, log_method = DefaultLogger)
123
123
  @min_forks = 1
124
124
  @max_forks = forks_to_run
125
- @port = port
126
- @child_count = 0
127
125
  Loompa.logger = log_method
128
126
  end
129
127
 
128
+ # class variable holding all the children
130
129
  @@children = Children.new
131
130
 
131
+ # A block to be executed just before calling the block a child
132
+ # will be executing
133
+ #
134
+ # [block] block The block that will be executed
132
135
  def on_child_start(&block)
133
136
  if block == nil then
134
137
  raise "block required"
135
138
  end
136
139
  @on_child_start = block
137
140
  end
138
-
141
+
142
+ # A block to be executed upon exiting a child process.
143
+ #
144
+ # [block] block The block that will be executed upon termination of a child process
139
145
  def on_child_exit(&block)
140
146
  if block == nil then
141
147
  raise "block required"
@@ -143,6 +149,9 @@ class Loompa
143
149
  @on_child_exit = block
144
150
  end
145
151
 
152
+ # Starts the child processes, the number of which is determined by the @max_forks variable
153
+ #
154
+ # [block] &block The block that will be executed by the child processes
146
155
  def start(&block)
147
156
  if block == nil then
148
157
  raise "block required"
@@ -183,37 +192,39 @@ class Loompa
183
192
  @flag = :out_of_loop
184
193
  terminate
185
194
  end
186
-
187
- def close()
188
- if @flag != :out_of_loop then
189
- raise "close() must be called out of start() loop"
190
- end
191
- @socks.each do |s|
192
- s.close
193
- end
194
- end
195
195
 
196
+ # sets the @flag to :exit_loop, essentially stopping the parent and child processes
197
+ # since the loop will die and all children will receive the close() call
196
198
  def stop()
197
199
  @flag = :exit_loop
198
200
  end
199
201
 
202
+ # Calls the close method on each child which sets its status to :closed
200
203
  def terminate()
204
+ raise "Cannot terminate while still in the loop" if @flag == :in_loop
201
205
  @@children.each do |c|
202
206
  c.close
203
207
  end
204
208
  end
205
209
 
210
+ # Sends the TERM signal to all child processes via their pids
206
211
  def interrupt()
207
212
  Process.kill "TERM", *(@@children.pids) rescue nil
208
213
  end
209
214
 
210
215
  private
211
-
216
+
217
+ # called by the child process when it's finished the block passed to it
212
218
  def exit_child
213
- #Loompa.logger.debug "c: exiting"
219
+ @on_child_exit.call if defined? @on_child_exit
214
220
  exit!
215
221
  end
216
222
 
223
+ # Creates a child process and tells that process to execute a block
224
+ # It also sets up the to and from pipes to be shared between the
225
+ # parent and child processes.
226
+ #
227
+ # [block] block The block to be executed by the child
217
228
  def make_child(block)
218
229
  #Loompa.logger.debug "p: make child"
219
230
  to_child = IO.pipe
@@ -235,8 +246,14 @@ class Loompa
235
246
  to_parent[1].close
236
247
  end
237
248
 
249
+ # Method to call the block that's been passed to it
250
+ #
251
+ # [block] the block that will be called within the child process
238
252
  def child(block)
239
253
  #Loompa.logger.debug "c: start"
254
+ # Handle these different signals the child might encounter
255
+ # This signal trapping actually does get handled within the child
256
+ # since it's called from within the fork method
240
257
  handle_signals(["TERM", "INT", "HUP"])
241
258
  @on_child_start.call if defined? @on_child_start
242
259
  #Loompa.logger.debug "c: connect from client"
@@ -252,6 +269,9 @@ class Loompa
252
269
  exit_child
253
270
  end
254
271
 
272
+ # Creates signal traps for the array of signals passed to it
273
+ #
274
+ # [Array] sigs The signals that will be trapped
255
275
  def handle_signals(sigs)
256
276
  sigs.each do |signal|
257
277
  trap(signal) { exit_child }
data/loompa.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{loompa}
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Tracey Eubanks"]
9
- s.date = %q{2010-06-30}
9
+ s.date = %q{2010-08-03}
10
10
  s.description = %q{Manage a fork pool}
11
11
  s.email = %q{traceye@pmamediagroup.com}
12
12
  s.extra_rdoc_files = ["lib/loompa.rb"]
13
13
  s.files = ["Rakefile", "lib/loompa.rb", "readme", "Manifest", "loompa.gemspec"]
14
14
  s.homepage = %q{http://github.com/pmamediagroup/loompa}
15
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Loompa", "--main", "README"]
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Loompa", "--main", "readme"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{loompa}
18
18
  s.rubygems_version = %q{1.3.7}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loompa
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tracey Eubanks
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-30 00:00:00 -06:00
18
+ date: 2010-08-03 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -44,7 +44,7 @@ rdoc_options:
44
44
  - --title
45
45
  - Loompa
46
46
  - --main
47
- - README
47
+ - readme
48
48
  require_paths:
49
49
  - lib
50
50
  required_ruby_version: !ruby/object:Gem::Requirement