Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mysql service connectivity check #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/nerve/service_watcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'nerve/service_watcher/tcp'
require 'nerve/service_watcher/http'
require 'nerve/service_watcher/rabbitmq'
require 'nerve/service_watcher/mysql'

module Nerve
class ServiceWatcher
Expand Down
40 changes: 40 additions & 0 deletions lib/nerve/service_watcher/mysql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'nerve/service_watcher/base'

module Nerve
module ServiceCheck
class MysqlServiceCheck < BaseServiceCheck
require 'mysql2'

def initialize(opts={})
super

raise ArgumentError, "missing required argument 'user' in mysql check" unless opts['user']

@user = opts['user']
@pass = opts['password'] || ''
@host = opts['host'] || '127.0.0.1'
@port = opts['port'] || '3306'
end

def check
# the idea of health check is similar to haproxy option mysql-check
log.debug "nerve: running mysql health check #{@name}"

begin
log.debug "nerve: mysql connect #{@host}:#{@port} as #{@user}"
conn = Mysql2::Client.new(:host => @host, :username => @user, :password => @pass, :port => @port)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think timeouts are integral to the notion of a health check. is there a way to specify a connection timeout for Mysql2 ? i don't think we should merge without it.

rescue Mysql2::Error => e
log.debug "nerve: mysql check error #{e.errno}: #{e.error}"
return false
ensure
conn.close if conn
end

return true
end
end

CHECKS ||= {}
CHECKS['mysql'] = MysqlServiceCheck
end
end
1 change: 1 addition & 0 deletions nerve.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "zk", "~> 1.9.2"
gem.add_runtime_dependency "bunny", "= 1.1.0"
gem.add_runtime_dependency "etcd", "~> 0.2.3"
gem.add_runtime_dependency "mysql2", "~> 0.4.2"

gem.add_development_dependency "rake"
gem.add_development_dependency "rspec", "~> 3.1.0"
Expand Down