forked from airbnb/nerve
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request airbnb#51 from airbnb/next_release
Next release
- Loading branch information
Showing
14 changed files
with
191 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"host": "1.2.3.4", | ||
"port": 3000, | ||
"reporter_type": "etcd", | ||
"etcd_host": "localhost", | ||
"etcd_port": 4001, | ||
"etcd_path": "/nerve/services/your_http_service/services", | ||
"check_interval": 2, | ||
"checks": [ | ||
{ | ||
"type": "http", | ||
"uri": "/health", | ||
"timeout": 0.2, | ||
"rise": 3, | ||
"fall": 2 | ||
} | ||
] | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require 'nerve/utils' | ||
require 'nerve/log' | ||
|
||
module Nerve | ||
class Reporter | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'nerve/reporter/base' | ||
require 'etcd' | ||
|
||
class Nerve::Reporter | ||
class Etcd < Base | ||
def initialize(service) | ||
%w{etcd_host instance_id host port}.each do |required| | ||
raise ArgumentError, "missing required argument #{required} for new service watcher" unless service[required] | ||
end | ||
@host = service['etcd_host'] | ||
@port = service['etcd_port'] || 4003 | ||
path = service['etcd_path'] || '/' | ||
@key = path.split('/').push(service['instance_id']).join('/') | ||
@data = parse_data({'host' => service['host'], 'port' => service['port'], 'name' => service['instance_id']}) | ||
end | ||
|
||
def start() | ||
log.info "nerve: waiting to connect to etcd at #{@path}" | ||
@etcd = ::Etcd.client(:host => @host, :port => @port) | ||
log.info "nerve: successfully created etcd connection to #{@key}" | ||
end | ||
|
||
def stop() | ||
report_down | ||
@etcd = nil | ||
end | ||
|
||
def report_up() | ||
etcd_save | ||
end | ||
|
||
def report_down | ||
etcd_delete | ||
end | ||
|
||
def update_data(new_data='') | ||
@data = parse_data(new_data) | ||
etcd_save | ||
end | ||
|
||
def ping? | ||
if @full_key | ||
etcd_save | ||
else | ||
@etcd.leader | ||
end | ||
end | ||
|
||
private | ||
|
||
def etcd_delete | ||
return unless @etcd and @full_key | ||
begin | ||
@etcd.delete(@full_key) | ||
rescue ::Etcd::KeyNotFound | ||
rescue Errno::ECONNREFUSED | ||
end | ||
end | ||
|
||
def etcd_create | ||
@full_key = @etcd.create_in_order(@key, :value => @data).key | ||
log.info "Created etcd node path #{@full_key}" | ||
end | ||
|
||
def etcd_save | ||
return etcd_create unless @full_key | ||
begin | ||
@etcd.set(@key, :value => @data, ttl => 30) | ||
rescue ::Etcd::KeyNotFound | ||
etcd_create | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require 'nerve/ring_buffer' | ||
|
||
module Nerve | ||
module ServiceCheck | ||
class BaseServiceCheck | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'json' | ||
require 'nerve/reporter' | ||
require 'nerve/service_watcher' | ||
|
||
describe "example services are valid" do | ||
Dir.foreach("#{File.dirname(__FILE__)}/../example/nerve_services") do |item| | ||
next if item == '.' or item == '..' | ||
service_data = JSON.parse(IO.read("#{File.dirname(__FILE__)}/../example/nerve_services/#{item}")) | ||
service_data['name'] = item.gsub(/\.json$/, '') | ||
service_data['instance_id'] = '1' | ||
context "when #{item} can be initialized as a valid reporter" do | ||
reporter = nil | ||
it 'Can new_from_service' do | ||
expect { reporter = Nerve::Reporter.new_from_service(service_data) }.to_not raise_error() | ||
end | ||
it 'Created a reporter object' do | ||
expect(reporter.is_a?(Nerve::Reporter::Base)).to eql(true) | ||
end | ||
end | ||
context "when #{item} can be initialized as a valid service watcher" do | ||
it do | ||
watcher = nil | ||
expect { watcher = Nerve::ServiceWatcher.new(service_data) }.to_not raise_error() | ||
expect(watcher.is_a?(Nerve::ServiceWatcher)).to eql(true) | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'spec_helper' | ||
require 'nerve/reporter/etcd' | ||
|
||
describe Nerve::Reporter::Etcd do | ||
let(:subject) { { | ||
'etcd_host' => 'etcdhost1', | ||
'etcd_port' => 4001, | ||
'etcd_path' => '/path', | ||
'instance_id' => 'instance_id', | ||
'host' => 'host', | ||
'port' => 'port' | ||
} | ||
} | ||
it 'actually constructs an instance' do | ||
expect(Nerve::Reporter::Etcd.new(subject).is_a?(Nerve::Reporter::Etcd)).to eql(true) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters