collective 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  .bundle
4
4
  coverage
5
5
  Gemfile.lock
6
+ pkg
data/README CHANGED
@@ -5,3 +5,5 @@ A pool contains several workers.
5
5
  Workers are processes which run jobs.
6
6
 
7
7
  Jobs are simple tasks.
8
+
9
+ Collective was originally known as Hive.
@@ -76,16 +76,42 @@ class Collective::Configuration
76
76
  attr :verbose, true
77
77
  attr :dry_run, true
78
78
  attr :args, true
79
+ attr :before_forks
79
80
  attr :after_forks
80
81
 
81
82
  attr :defaults
82
- attr :jobs
83
+ attr :pools
84
+
85
+ # I'm not sure why this is so complicated.
86
+ class PoolEnumerator
87
+ def initialize pools
88
+ @pools = pools
89
+ end
90
+ include Enumerable
91
+ def each(&block)
92
+ them = @pools.each
93
+ it = nil
94
+ loop do
95
+ begin
96
+ it = them.next
97
+ rescue StopIteration => x
98
+ break
99
+ end
100
+ policy = Collective::Policy.resolve(it.last)
101
+ yield([ it.first, policy ])
102
+ end
103
+ end
104
+ end
105
+
106
+ def policies
107
+ PoolEnumerator.new(pools)
108
+ end
83
109
 
84
110
  def initialize( filename = nil )
85
111
  @verbose = 0
86
112
  @dry_run = false
87
113
  @defaults = {}
88
- @jobs = {}
114
+ @pools = {}
89
115
  load_file(filename) if filename
90
116
  end
91
117
 
@@ -186,13 +212,20 @@ class Collective::Configuration
186
212
  options.each { |k,v| set_default(k,v) }
187
213
  end
188
214
 
189
- def add_pool(name,options)
190
- options = defaults.merge(options)
191
- jobs[name] = options
215
+ def add_pool( name, options = {} )
216
+ before_forks = (options[:before_forks] || []) + (self.before_forks || [])
217
+ after_forks = (options[:after_forks] || []) + (self.after_forks || [])
218
+ options = defaults.merge(options).merge before_forks: before_forks, after_forks: after_forks
219
+ pools[name] = options
192
220
  log "Added pool for #{name}" if verbose == 1
193
221
  log "Added pool for #{name} with #{options}" if verbose >= 2
194
222
  end
195
223
 
224
+ def before_fork(&block)
225
+ @before_forks ||= []
226
+ @before_forks << block
227
+ end
228
+
196
229
  def after_fork(&block)
197
230
  @after_forks ||= []
198
231
  @after_forks << block
@@ -8,8 +8,8 @@ class Collective::Monitor
8
8
  attr :pools
9
9
 
10
10
  def initialize( configuration )
11
- @pools = configuration.jobs.map do |kind,options|
12
- pool = Collective::Pool.new( kind, Collective::Policy.resolve(options) )
11
+ @pools = configuration.policies.map do |kind,policy|
12
+ pool = Collective::Pool.new( kind, policy )
13
13
  end
14
14
  end
15
15
 
@@ -46,6 +46,14 @@ class Collective::Policy
46
46
  @options.dup
47
47
  end
48
48
 
49
+ def before_fork
50
+ (before_forks || []).each { |f| f.call }
51
+ end
52
+
53
+ def after_fork
54
+ (after_forks || []).each { |f| f.call }
55
+ end
56
+
49
57
  end # Instance
50
58
 
51
59
  class << self
@@ -1,3 +1,3 @@
1
1
  module Collective
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -21,10 +21,9 @@ class Collective::Worker
21
21
  registry = options[:registry] || Collective::Registry.new( name, storage )
22
22
 
23
23
  foptions = { stdout: "/tmp/debug.log" }
24
+ policy.before_fork
24
25
  Collective::Utilities::Process.fork_and_detach( foptions ) do
25
- if after_forks = policy.after_forks then
26
- after_forks.each { |af| af.call }
27
- end
26
+ policy.after_fork
28
27
  # $0 = "$0 #{name}"
29
28
  worker = new( prototype_job, options )
30
29
  trap("TERM") { worker.quit! }
@@ -15,10 +15,36 @@ describe Collective::Configuration do
15
15
  set_env "the_env"
16
16
  set_name "a_name"
17
17
  chdir "."
18
+ before_fork() { true }
19
+ after_fork() { true }
18
20
  EOT
19
21
  c = Collective::Configuration.parse ["--dry-run", "--script", script]
20
22
  c.env.should eq("the_env")
21
23
  c.name.should eq("a_name")
24
+ c.before_forks.size.should eq(1)
25
+ c.after_forks.size.should eq(1)
26
+ end
27
+
28
+ it "enumerates pool names and policies" do
29
+ script = <<-EOT.gsub(/^ +/,'')
30
+ add_pool "Test", pool_max_workers: 1
31
+ EOT
32
+ c = Collective::Configuration.parse ["--dry-run", "--script", script]
33
+ c.policies.first.first.should eq("Test")
34
+ c.policies.first.last.pool_max_workers.should eq(1)
35
+ end
36
+
37
+ it "adds blocks to pools" do
38
+ script = <<-EOT.gsub(/^ +/,'')
39
+ before_fork() { true }
40
+ after_fork() { false }
41
+ add_pool "Test"
42
+ EOT
43
+ c = Collective::Configuration.parse ["--dry-run", "--script", script]
44
+ c.policies.to_a.size.should eq(1)
45
+ pool = c.policies.to_a.first.last
46
+ pool.before_forks.first.call.should eq(true)
47
+ pool.after_forks.first.call.should eq(false)
22
48
  end
23
49
 
24
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collective
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease: !!null
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable: !!null
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: redis
17
- requirement: &70125036305720 !ruby/object:Gem::Requirement
17
+ requirement: &70335485309520 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70125036305720
25
+ version_requirements: *70335485309520
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: redis-namespace
28
- requirement: &70125036305300 !ruby/object:Gem::Requirement
28
+ requirement: &70335485309100 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70125036305300
36
+ version_requirements: *70335485309100
37
37
  description: !!null
38
38
  email:
39
39
  - mark.lanett@gmail.com