forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ar_kill.rb
31 lines (27 loc) · 1.1 KB
/
ar_kill.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
include Vmdb::Logging
def kill(pid)
pid_numeric = pid.to_i
return if pid_numeric == 0
return if pid_numeric == @spid
data = select(<<-SQL, "Client Connections")
SELECT pid AS spid,
current_query AS query,
age(now(),pg_stat_activity.query_start) AS age
FROM pg_stat_activity
WHERE pid = #{pid_numeric}
AND datname = #{quote(current_database)}
SQL
item = data.first
if item.nil?
_log.info("SPID=[#{pid_numeric}] not found")
else
_log.info("Sending CANCEL Request for SPID=[#{pid_numeric}], age=[#{item['age']}], query=[#{item['query']}]")
select(<<-SQL, "Cancel SPID")
SELECT pg_cancel_backend(#{pid_numeric})
FROM pg_stat_activity
WHERE datname = #{quote(current_database)}
SQL
end
end
end