mlanett-hive 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -4,11 +4,12 @@ source "http://rubygems.org"
4
4
  gemspec
5
5
 
6
6
  group :development, :test do
7
+ gem "growl_notify" # for guard
7
8
  gem "guard-rspec"
9
+ gem "rake"
8
10
  gem "rb-fsevent" # for guard
9
- gem "growl_notify" # for guard
10
11
  gem "rspec"
11
- gem "ruby-debug19", require: false
12
+ ### "ruby-debug19", require: false
12
13
  end
13
14
 
14
15
  group :test do
data/bin/hive CHANGED
@@ -20,17 +20,14 @@
20
20
  File.expand_path(File.dirname(__FILE__)+"/../lib").tap { |d| $:.unshift(d) if ! $:.member?(d) }
21
21
  require "hive"
22
22
 
23
- my = Hive::Configuration.parse(ARGV)
23
+ options = Hive::Configuration.parse(ARGV)
24
24
 
25
- # TODO use my.options_for_daemon_spawn
26
- # TODO use my.args_for_daemon_spawn
27
-
28
- if ! my.dry_run then
25
+ if ! options.dry_run then
29
26
  case ARGV.first
30
27
  when "stop"
31
- Hive::Monitor.new(my).stop_all
28
+ Hive::Monitor.new(options).stop_all
32
29
  when "monitor"
33
- Hive::Monitor.new(my).monitor
30
+ Hive::Monitor.new(options).monitor
34
31
  else
35
32
  Kernel.abort "Unknown command #{ARGV.first}"
36
33
  end
File without changes
@@ -27,6 +27,7 @@ end
27
27
 
28
28
  module Hive::Utilities
29
29
  autoload :AirbrakeObserver, "hive/utilities/airbrake_observer"
30
+ autoload :Hash, "hive/utilities/hash"
30
31
  autoload :HoptoadObserver, "hive/utilities/hoptoad_observer"
31
32
  autoload :LogObserver, "hive/utilities/log_observer"
32
33
  autoload :NullObserver, "hive/utilities/null_observer"
@@ -145,20 +145,6 @@ class Hive::Configuration
145
145
  self
146
146
  end
147
147
 
148
- def options_for_daemon_spawn
149
- mkdirp root, "#{root}/log", "#{root}/tmp", "#{root}/tmp/pids" if ! dry_run
150
- return {
151
- working_dir: root,
152
- log_file: "#{root}/log/#{name}_#{env}.log",
153
- pid_file: "#{root}/tmp/pids/#{name}_#{env}.pid",
154
- sync_log: local?
155
- }
156
- end
157
-
158
- def args_for_daemon_spawn
159
- args + [self]
160
- end
161
-
162
148
  # ----------------------------------------------------------------------------
163
149
  # DSL
164
150
  # ----------------------------------------------------------------------------
@@ -1,7 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- require "ostruct"
4
-
5
3
  class Hive::Policy
6
4
 
7
5
  DEFAULTS = {
@@ -19,19 +17,9 @@ class Hive::Policy
19
17
  observers: []
20
18
  }
21
19
 
22
- class Instance
23
-
24
- # including options[:policy] will merge over these options
25
20
  def initialize( options = {} )
26
- if options[:policy] then
27
- policy = options.delete(:policy)
28
- defaults = policy.dup
29
- else
30
- defaults = DEFAULTS
31
- end
32
-
33
21
  options = Hash[ options.map { |k,v| [ k.to_sym, v ] } ] # poor man's symbolize keys
34
- @options = defaults.merge( options )
22
+ @options = DEFAULTS.merge( options )
35
23
  end
36
24
 
37
25
  def storage
@@ -42,8 +30,13 @@ class Hive::Policy
42
30
  @options[symbol.to_sym]
43
31
  end
44
32
 
33
+ def merge( options = {} )
34
+ @options.merge!( options )
35
+ self
36
+ end
37
+
45
38
  def dup
46
- @options.dup
39
+ self.class.new( @options.dup )
47
40
  end
48
41
 
49
42
  def before_fork
@@ -54,13 +47,12 @@ class Hive::Policy
54
47
  (after_forks || []).each { |f| f.call }
55
48
  end
56
49
 
57
- end # Instance
58
-
59
50
  class << self
60
-
61
- def resolve( options = {} )
62
- # this will dup either an Instance or a Hash
63
- Hive::Policy::Instance.new(options.dup)
51
+ # @returns a new policy
52
+ # @options_or_policy can be a set of options or a source policy
53
+ def resolve( options_or_policy = {} )
54
+ # if it is a policy, just dup it
55
+ options_or_policy.kind_of?(Hive::Policy) ? options_or_policy.dup : new(options_or_policy.dup)
64
56
  end
65
57
 
66
58
  end # class
@@ -10,6 +10,7 @@
10
10
  class Hive::Pool
11
11
 
12
12
  include Hive::Log
13
+ include Hive::Utilities::Hash
13
14
 
14
15
  attr :kind # job class
15
16
  attr :name
@@ -36,8 +37,8 @@ class Hive::Pool
36
37
  # @param options[:log] can be true
37
38
  # @returns the checked worker lists
38
39
  def synchronize( options = {} )
39
- do_log = options.delete(:log)
40
- raise if options.size > 0
40
+ assert_valid_keys( options, :log )
41
+ do_log = options[:log]
41
42
 
42
43
  checklist = registry.checked_workers( policy )
43
44
  live_count = checklist.live.size
@@ -91,8 +92,8 @@ class Hive::Pool
91
92
  # this really should be protected but it's convenient to be able to force a spawn
92
93
  # param options[:wait] can true to wait until after the process is spawned
93
94
  def spawn( options = {} )
94
- wait = options.delete(:wait)
95
- raise if options.size > 0
95
+ assert_valid_keys( options, :wait )
96
+ wait = options[:wait]
96
97
 
97
98
  if ! wait then
98
99
  Hive::Worker.spawn kind, registry: registry, policy: policy, name: name
@@ -113,8 +114,8 @@ class Hive::Pool
113
114
 
114
115
  # shut down a worker
115
116
  def reap( key, options = {} )
116
- wait = options.delete(:wait)
117
- raise if options.size > 0
117
+ assert_valid_keys( options, :wait )
118
+ wait = options[:wait]
118
119
 
119
120
  if key.host == Hive::Key.local_host then
120
121
  ::Process.kill( "TERM", key.pid )
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ =begin
4
+ Gives you assert_valid_keys without infecting Hash
5
+ =end
6
+
7
+ module Hive::Utilities::Hash
8
+
9
+ def assert_valid_keys( hash, *valid_keys )
10
+ hash.each_key do |key|
11
+ raise(ArgumentError, "Unknown key: #{key}") unless valid_keys.include?(key)
12
+ end
13
+ end
14
+
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Hive
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -9,7 +9,7 @@
9
9
  =end
10
10
 
11
11
  require "bundler/setup" # set up gem paths
12
- #require "ruby-debug" # because sometimes you need it
12
+ ####### "ruby-debug" # because sometimes you need it
13
13
 
14
14
  #require "simplecov" # code coverage
15
15
  #SimpleCov.start # must be loaded before our own code
@@ -27,7 +27,7 @@ describe Hive::Policy do
27
27
 
28
28
  it "is copied from another policy" do
29
29
  p1 = Hive::Policy.resolve worker_max_jobs: 7, worker_max_lifetime: 999
30
- p2 = Hive::Policy.resolve policy: p1, worker_max_jobs: 12
30
+ p2 = p1.dup.merge worker_max_jobs: 12
31
31
 
32
32
  p2.worker_max_lifetime.should eq(p1.worker_max_lifetime)
33
33
  p1.worker_max_jobs.should eq(7)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mlanett-hive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-07 00:00:00.000000000Z
12
+ date: 2012-02-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &70198172375900 !ruby/object:Gem::Requirement
16
+ requirement: &70292391849280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70198172375900
24
+ version_requirements: *70292391849280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: redis-namespace
27
- requirement: &70198172375480 !ruby/object:Gem::Requirement
27
+ requirement: &70292391848860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70198172375480
35
+ version_requirements: *70292391848860
36
36
  description:
37
37
  email:
38
38
  - mark.lanett@gmail.com
@@ -56,6 +56,7 @@ files:
56
56
  - demo/job2.rb
57
57
  - demo/job3.rb
58
58
  - demo/populate.rb
59
+ - demo/squiggly.rb
59
60
  - hive.gemspec
60
61
  - lib/hive.rb
61
62
  - lib/hive/checker.rb
@@ -71,9 +72,9 @@ files:
71
72
  - lib/hive/pool.rb
72
73
  - lib/hive/redis/storage.rb
73
74
  - lib/hive/registry.rb
74
- - lib/hive/squiggly.rb
75
75
  - lib/hive/trace.rb
76
76
  - lib/hive/utilities/airbrake_observer.rb
77
+ - lib/hive/utilities/hash.rb
77
78
  - lib/hive/utilities/hoptoad_observer.rb
78
79
  - lib/hive/utilities/log_observer.rb
79
80
  - lib/hive/utilities/observeable.rb