nerve 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,7 +3,6 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- Gemfile.lock
7
6
  InstalledFiles
8
7
  _yardoc
9
8
  coverage
@@ -19,4 +18,4 @@ tmp
19
18
  \#*\#
20
19
  .\#*
21
20
  vendor
22
- .vagrant
21
+ .vagrant
data/.mailmap ADDED
@@ -0,0 +1,2 @@
1
+ <igor.serebryany@airbedandbreakfast.com> <igor47@moomers.org>
2
+ <martin.rhoads@airbnb.com> <ermal14@gmail.com>
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nerve (0.3.0)
5
+ bunny (= 1.0.0.rc2)
6
+ zk (~> 1.9.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ amq-protocol (1.8.0)
12
+ bunny (1.0.0.rc2)
13
+ amq-protocol (>= 1.8.0)
14
+ little-plugger (1.1.3)
15
+ logging (1.7.2)
16
+ little-plugger (>= 1.1.3)
17
+ zk (1.9.2)
18
+ logging (~> 1.7.2)
19
+ zookeeper (~> 1.4.0)
20
+ zookeeper (1.4.7)
21
+
22
+ PLATFORMS
23
+ java
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ nerve!
data/README.md CHANGED
@@ -38,6 +38,7 @@ The config file is composed of two main sections:
38
38
 
39
39
  * `instance_id`: the name under which your services will be registered in zookeeper
40
40
  * `services`: the hash (from service name to config) of the services nerve will be monitoring
41
+ * `service_conf_dir`: path to a directory in which each json file will be interpreted as a service with the basename of the file minus the .json extension
41
42
 
42
43
  ### Services Config ###
43
44
 
data/bin/nerve CHANGED
@@ -31,20 +31,31 @@ end
31
31
  # parse command line arguments
32
32
  optparse.parse!
33
33
 
34
-
35
- # parse nerve config file
36
- begin
37
- config = JSON::parse(File.read(options[:config]))
38
- rescue TypeError => e
39
- raise ArgumentError, "you must pass in a '--config' option"
40
- rescue Errno::ENOENT => e
41
- raise ArgumentError, "config file does not exist:\n#{e.inspect}"
42
- rescue Errno::EACCES => e
43
- raise ArgumentError, "could not open config file:\n#{e.inspect}"
44
- rescue JSON::ParserError => e
45
- raise "config file #{options[:config]} is not json:\n#{e.inspect}"
34
+ def parseconfig(filename)
35
+ # parse synapse config file
36
+ begin
37
+ c = JSON::parse(File.read(filename))
38
+ rescue Errno::ENOENT => e
39
+ raise ArgumentError, "config file does not exist:\n#{e.inspect}"
40
+ rescue Errno::EACCES => e
41
+ raise ArgumentError, "could not open config file:\n#{e.inspect}"
42
+ rescue JSON::ParserError => e
43
+ raise "config file #{filename} is not json:\n#{e.inspect}"
44
+ end
45
+ return c
46
46
  end
47
47
 
48
+ config = parseconfig(options[:config])
49
+ config['services'] ||= {}
50
+
51
+ if config.has_key?('service_conf_dir')
52
+ cdir = File.expand_path(config['service_conf_dir'])
53
+ if ! Dir.exists?(cdir) then
54
+ raise "service conf dir does not exist:#{cdir}"
55
+ end
56
+ cfiles = Dir.glob(File.join(cdir, '*.json'))
57
+ cfiles.each { |x| config['services'][File.basename(x[/(.*)\.json$/, 1])] = parseconfig(x) }
58
+ end
48
59
 
49
60
  # create nerve object
50
61
  s = Nerve::Nerve.new config
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "instance_id": "mymachine",
3
+ "service_conf_dir": "example/nerve_services",
3
4
  "services": {
4
5
  "your_http_service": {
5
6
  "port": 3000,
@@ -0,0 +1,16 @@
1
+ {
2
+ "port": 3000,
3
+ "host": "127.0.0.1",
4
+ "zk_hosts": ["localhost:2181"],
5
+ "zk_path": "/nerve/services/your_http_service/services",
6
+ "check_interval": 2,
7
+ "checks": [
8
+ {
9
+ "type": "http",
10
+ "uri": "/health",
11
+ "timeout": 0.2,
12
+ "rise": 3,
13
+ "fall": 2
14
+ }
15
+ ]
16
+ }
@@ -16,7 +16,7 @@ module Nerve
16
16
 
17
17
  def up?
18
18
  # do the check
19
- check_result = !!ignore_errors do
19
+ check_result = !!catch_errors do
20
20
  check
21
21
  end
22
22
 
@@ -45,6 +45,15 @@ module Nerve
45
45
  # otherwise return the last result
46
46
  return @last_result
47
47
  end
48
+
49
+ def catch_errors(&block)
50
+ begin
51
+ return yield
52
+ rescue Object => error
53
+ log.info "nerve: service check #{@name} got error #{error.inspect}"
54
+ return false
55
+ end
56
+ end
48
57
  end
49
58
  end
50
59
  end
@@ -31,10 +31,11 @@ module Nerve
31
31
  response = connection.get(@uri)
32
32
  code = response.code.to_i
33
33
 
34
- log.debug "nerve: check #{@name} got response code #{code}"
35
34
  if code >= 200 and code < 300
35
+ log.debug "nerve: check #{@name} got response code #{code} with body \"#{response.body}\""
36
36
  return true
37
37
  else
38
+ log.error "nerve: check #{@name} got response code #{code} with body \"#{response.body}\""
38
39
  return false
39
40
  end
40
41
  end
data/lib/nerve/utils.rb CHANGED
@@ -4,14 +4,5 @@ module Nerve
4
4
  res = `#{command}`.chomp
5
5
  raise "command '#{command}' failed to run:\n#{res}" unless $?.success?
6
6
  end
7
-
8
- def ignore_errors(&block)
9
- begin
10
- return yield
11
- rescue Object => error
12
- log.debug "ignoring error #{error.inspect}"
13
- return false
14
- end
15
- end
16
7
  end
17
8
  end
data/lib/nerve/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nerve
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/nerve.rb CHANGED
@@ -15,18 +15,11 @@ module Nerve
15
15
  include Logging
16
16
 
17
17
  def initialize(opts={})
18
+ log.info 'nerve: starting up!'
19
+
18
20
  # set global variable for exit signal
19
21
  $EXIT = false
20
22
 
21
- # trap int signal and set exit to true
22
- %w{INT TERM}.each do |signal|
23
- trap(signal) do
24
- $EXIT = true
25
- end
26
- end
27
-
28
- log.info 'nerve: starting up!'
29
-
30
23
  # required options
31
24
  log.debug 'nerve: checking for required inputs'
32
25
  %w{instance_id services}.each do |required|
@@ -34,36 +27,43 @@ module Nerve
34
27
  end
35
28
 
36
29
  @instance_id = opts['instance_id']
37
-
38
- # create service watcher objects
39
- log.debug 'nerve: creating service watchers'
40
- @service_watchers=[]
41
- opts['services'].each do |name, config|
42
- @service_watchers << ServiceWatcher.new(config.merge({'instance_id' => @instance_id, 'name' => name}))
43
- end
30
+ @services = opts['services']
31
+ @watchers = {}
44
32
 
45
33
  log.debug 'nerve: completed init'
46
34
  end
47
35
 
48
36
  def run
49
37
  log.info 'nerve: starting run'
50
- begin
51
- children = []
52
38
 
53
- log.debug 'nerve: launching service check threads'
54
- @service_watchers.each do |watcher|
55
- children << Thread.new{watcher.run}
56
- end
39
+ @services.each do |name, config|
40
+ launch_watcher(name, config)
41
+ end
57
42
 
58
- log.debug 'nerve: main thread done, waiting for children'
59
- children.each do |child|
60
- child.join
61
- end
62
- ensure
43
+ begin
44
+ sleep
45
+ rescue
46
+ log.debug 'nerve: sleep interrupted; exiting'
63
47
  $EXIT = true
48
+ @watchers.each do |name, watcher_thread|
49
+ reap_watcher(name)
50
+ end
64
51
  end
52
+
65
53
  log.info 'nerve: exiting'
54
+ ensure
55
+ $EXIT = true
56
+ end
57
+
58
+ def launch_watcher(name, config)
59
+ log.debug "nerve: launching service watcher #{name}"
60
+ watcher = ServiceWatcher.new(config.merge({'instance_id' => @instance_id, 'name' => name}))
61
+ @watchers[name] = Thread.new{watcher.run}
66
62
  end
67
63
 
64
+ def reap_watcher(name)
65
+ watcher_thread = @watchers.delete(name)
66
+ watcher_thread.join()
67
+ end
68
68
  end
69
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nerve
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-10-24 00:00:00.000000000 Z
14
+ date: 2014-01-27 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: zk
@@ -55,14 +55,17 @@ extensions: []
55
55
  extra_rdoc_files: []
56
56
  files:
57
57
  - .gitignore
58
+ - .mailmap
58
59
  - .nerve.rc
59
60
  - Gemfile
61
+ - Gemfile.lock
60
62
  - LICENSE.txt
61
63
  - README.md
62
64
  - Rakefile
63
65
  - Vagrantfile
64
66
  - bin/nerve
65
67
  - example/nerve.conf.json
68
+ - example/nerve_services/service1.json
66
69
  - lib/nerve.rb
67
70
  - lib/nerve/log.rb
68
71
  - lib/nerve/reporter.rb