collective 0.2.0 → 0.2.1
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.
- data/.gitignore +1 -0
- data/README +2 -0
- data/lib/collective/configuration.rb +38 -5
- data/lib/collective/monitor.rb +2 -2
- data/lib/collective/policy.rb +8 -0
- data/lib/collective/version.rb +1 -1
- data/lib/collective/worker.rb +2 -3
- data/spec/configuration_spec.rb +26 -0
- metadata +5 -5
data/.gitignore
CHANGED
data/README
CHANGED
@@ -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 :
|
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
|
-
@
|
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
|
-
|
191
|
-
|
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
|
data/lib/collective/monitor.rb
CHANGED
@@ -8,8 +8,8 @@ class Collective::Monitor
|
|
8
8
|
attr :pools
|
9
9
|
|
10
10
|
def initialize( configuration )
|
11
|
-
@pools = configuration.
|
12
|
-
pool = Collective::Pool.new( kind,
|
11
|
+
@pools = configuration.policies.map do |kind,policy|
|
12
|
+
pool = Collective::Pool.new( kind, policy )
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
data/lib/collective/policy.rb
CHANGED
data/lib/collective/version.rb
CHANGED
data/lib/collective/worker.rb
CHANGED
@@ -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
|
-
|
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! }
|
data/spec/configuration_spec.rb
CHANGED
@@ -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.
|
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: &
|
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: *
|
25
|
+
version_requirements: *70335485309520
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: redis-namespace
|
28
|
-
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: *
|
36
|
+
version_requirements: *70335485309100
|
37
37
|
description: !!null
|
38
38
|
email:
|
39
39
|
- mark.lanett@gmail.com
|