bin/lhm-kill-queue in lhm-1.0.0.rc5 vs bin/lhm-kill-queue in lhm-1.0.0.rc6

- old
+ new

@@ -13,10 +13,11 @@ opts.on("-h", "--hostname HOSTNAME") { |v| @hostname = v } opts.on("-u", "--username USERNAME") { |v| @username = v } opts.on("-p", "--password PASSWORD") { |v| @password = v } opts.on("-d", "--database DATABASE") { |v| @database = v } opts.on("-m", "--mode MODE") { |v| @mode = v.to_sym } + opts.on("-y", "--confirm") { |v| @confirm = true } end.parse! unless(@hostname && @username && @password && @database) abort usage end @@ -31,11 +32,11 @@ def usage <<-desc.gsub(/^ /, '') kills queries on the given server after detecting 'lock table% -- lhm'. usage: lhm-kill-queue -h hostname -u username -p password -d database \\ - (--kill | --master | --slave) + (-m kill | -m master | -m slave) [--confirm] desc end def run @@ -46,21 +47,19 @@ end end def kill lock = trip - puts "killing lock process #{ lock }." kill_process(lock) end def master lock = trip puts "starting to kill non lhm processes in 1 second" - sleep(1) + sleep(1.05) [list_non_lhm].flatten.each do |process| - puts "killing #{ select_statement(process) }" kill_process(process) sleep(0.05) end end @@ -69,11 +68,10 @@ puts "starting to kill non lhm SELECT processes in 1 second" sleep(1) [list_non_lhm].flatten.each do |process| if(select?(process)) - puts "killing #{ select_statement(process) }" kill_process(process) sleep(0.05) end end end @@ -94,29 +92,40 @@ def connection ActiveRecord::Base.connection end def list_non_lhm - select_process("info not like '% -- lhm' and time > 0 and command = 'Query'") + select_processes("info not like '% -- lhm' and time > 0 and command = 'Query'") end def trip - until res = select_process("info like 'lock table% -- lhm'") + until res = select_processes("info like 'lock table% -- lhm'") sleep 0.2 print '.' end res end def kill_process(process_id) + puts "killing #{ select_statement(process_id) }" + + if(@confirm) + print "confirm ('y' to confirm): " + + if(gets.strip != 'y') + puts "skipped." + return + end + end + connection.execute("kill #{ process_id }") + puts "killed #{ process_id }" end def select?(process) if statement = select_statement(process) - case statement when /delete/i then false when /update/i then false when /insert/i then false else @@ -131,23 +140,27 @@ select info from information_schema.processlist where id = #{ process } ) end end - def select_process(predicate) - value %Q( + def select_processes(predicate) + values %Q( select id from information_schema.processlist where db = '#{ @database }' and user = '#{ @username }' and #{ predicate } ) end def value(statement) connection.select_value(statement) end + + def values(statement) + connection.select_values(statement) + end end end killer = Lhm::KillQueue.new killer.run -. +