Class: PGMonitor

Inherits:
Object show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/replication_monitor.rb

Overview

A class to encapsulate the PG handles.

Constant Summary collapse

VERSION =
%q$Id$
LAG_ALERT =

When to consider a slave as ‘behind’, measured in WAL segments. The default WAL segment size is 16, so we’ll alert after missing two WAL files worth of data.

32

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, hosts) ⇒ PGMonitor

Create a new PGMonitor object.



40
41
42
43
44
45
46
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/replication_monitor.rb', line 40

def initialize( opts, hosts )
	@opts        = opts
	@master      = hosts.shift
	@slaves      = hosts
	@current_wal = {}
	@failures    = []
end

Instance Attribute Details

#current_walObject (readonly)

Returns the value of attribute current_wal.



48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/replication_monitor.rb', line 48

def current_wal
  @current_wal
end

#failuresObject (readonly)

Returns the value of attribute failures.



48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/replication_monitor.rb', line 48

def failures
  @failures
end

#masterObject (readonly)

Returns the value of attribute master.



48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/replication_monitor.rb', line 48

def master
  @master
end

#optsObject (readonly)

Returns the value of attribute opts.



48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/replication_monitor.rb', line 48

def opts
  @opts
end

#slavesObject (readonly)

Returns the value of attribute slaves.



48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/replication_monitor.rb', line 48

def slaves
  @slaves
end

Instance Method Details

#checkObject

Perform the connections and check the lag.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/sample/replication_monitor.rb', line 53

def check
	# clear prior failures, get current xlog info
	@failures = []
	return unless self.get_current_wal

	# check all slaves
	self.slaves.each do |slave|
		begin
			slave_db = PG.connect(
				:dbname   => self.opts.database,
				:host     => slave,
				:port     => self.opts.port,
				:user     => self.opts.user,
				:password => self.opts.pass,
				:sslmode  => 'prefer'
			)

			xlog = slave_db.exec( 'SELECT pg_last_xlog_receive_location()' ).getvalue( 0, 0 )
			slave_db.close

			lag_in_megs = ( self.find_lag( xlog ).to_f / 1024 / 1024 ).abs
			if lag_in_megs >= LAG_ALERT
				failures << { :host => slave,
					:error => "%0.2fMB behind the master." % [ lag_in_megs ] }
			end
		rescue => err
			failures << { :host => slave, :error => err.message }
		end
	end
end