Skip to content

Commit

Permalink
Merge pull request #223 from Tumblr/ruby2
Browse files Browse the repository at this point in the history
Fix pry output on ruby 2
  • Loading branch information
Graham Christensen authored and GitHub Enterprise committed Mar 28, 2017
2 parents d64bf16 + 8357ebb commit 7bea316
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 86 deletions.
46 changes: 25 additions & 21 deletions lib/jetpants/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'json'

module Jetpants

# A Jetpants::DB is a specific mysql instance running on a particular IP and port.
# It also contains a Jetpants::Host object corresponding to the IP; any missing
# method calls get delegated to the Host.
Expand All @@ -13,17 +13,17 @@ module Jetpants
class DB
include CallbackHandler
include Output

# IP address (as a string) of the MySQL instance
attr_reader :ip

# Port number of the MySQL instance. The base Jetpants implementation only supports
# port 3306, since this is necessary to crawl a replication hierarchy using SHOW
# PROCESSLIST, which does not include slave port numbers. However, plugins may
# override this behavior to support nonstandard ports and multi-instance-per-host
# topologies.
attr_reader :port

# Jetpants::Host object that this MySQL instance runs on.
attr_reader :host

Expand All @@ -41,16 +41,16 @@ class DB
# duplicates.
@@all_dbs = {}
@@all_dbs_mutex = Mutex.new

def self.clear
@@all_dbs_mutex.synchronize {@@all_dbs = {}}
end

# Because this class is rather large, methods have been grouped together
# and moved to separate files in lib/jetpants/db. We load these all now.
# They each just re-open the DB class and add some methods.
Dir[File.join File.dirname(__FILE__), 'db', '*'].each {|f| require f}

# We override DB.new so that attempting to create a duplicate DB object
# (that is, one with the same IP and port as an existing DB object)
# returns the original object.
Expand All @@ -63,31 +63,31 @@ def self.new(ip, port=3306)
@@all_dbs[addr] ||= super
end
end

def initialize(ip, port=3306)
@ip, @port = ip, port.to_i
@host = Host.new(ip)

# These get set upon DB#probe being run
@master = nil
@slaves = nil
@repl_paused = nil
@running = nil

# These get set upon DB#connect being run
@user = nil
@schema = nil

# This is ephemeral, only known to Jetpants if you previously called
# DB#start_mysql or DB#restart_mysql in this process
@options = []

# Mutex used to lock probing state
@probe_mutex = Mutex.new
end

###### Host methods ########################################################

# Jetpants::DB delegates missing methods to its Jetpants::Host.
def method_missing(name, *args, &block)
if @host.respond_to? name
Expand All @@ -96,40 +96,44 @@ def method_missing(name, *args, &block)
super
end
end

# Alters respond_to? logic to account for delegation of missing methods
# to the instance's Host.
def respond_to?(name, include_private=false)
super || @host.respond_to?(name)
end

# Returns true if the supplied Jetpants::DB is on the same Jetpants::Host
# as self.
def same_host_as?(db)
@ip == db.ip
end

###### Misc methods ########################################################

# DB objects are sorted as strings, ie, by calling to_s
def <=> other
to_s <=> other.to_s
end


def inspect
to_s
end

# Returns a string in the form "ip:port"
def to_s
"#{@ip}:#{@port}"
end

# Returns self, since self is already a Jetpants::DB.
def to_db
self
end

# Returns the instance's Jetpants::Host.
def to_host
@host
end

end
end
4 changes: 4 additions & 0 deletions lib/jetpants/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,10 @@ def to_s
return @ip
end

def inspect
to_s
end

# Returns self, since this object is already a Host.
def to_host
self
Expand Down
Loading

0 comments on commit 7bea316

Please sign in to comment.