Class: EY::Snaplock::Database::MySQL

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_snaplock/database/mysql.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ MySQL

Returns a new instance of MySQL.



5
6
7
# File 'lib/ey_snaplock/database/mysql.rb', line 5

def initialize(uri)
  @mysql = mysql_command(uri)
end

Instance Method Details

#dump_process_list(logfile) ⇒ Object



35
36
37
# File 'lib/ey_snaplock/database/mysql.rb', line 35

def dump_process_list(logfile)
  system("#{@mysql} -N -e 'show processlist;' >> #{logfile}")
end

#has_long_running_queries?(cutoff_in_seconds) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ey_snaplock/database/mysql.rb', line 23

def has_long_running_queries?(cutoff_in_seconds)
  %x<#{@mysql} -B -N -e 'show processlist;'>.split("\n").each do |process_entry|
    id, user, host, db, command, time, state, info = process_entry.split("\t")

    next if user == 'system user'
    next if ['Sleep', 'Binlog Dump'].include? command

    return true if time.to_i > cutoff_in_seconds
  end
  false
end

#lock_filenameObject



9
10
11
# File 'lib/ey_snaplock/database/mysql.rb', line 9

def lock_filename
  ENV["MYSQL_LOCK_FILENAME"] || "/var/run/ey_snaplock_mysql.pid"
end

#with_lock(timeout) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/ey_snaplock/database/mysql.rb', line 13

def with_lock(timeout)
  acquire_lock_within_timeout(timeout)
  write_master_status(ENV["MASTER_STATUS_FILE"] || "/db/mysql/.snapshot_backup_master_status.txt")
  yield
rescue Timeout::Error
  lock_timeout(timeout)
ensure
  release_lock
end