-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathref.pl
65 lines (53 loc) · 1.7 KB
/
ref.pl
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl
#############################
# Ref v0.1 #
# Created by Josh McDougall #
#############################
# This should be run inside a screen session
# Ref.pl makes sure no player has a query over ~1 minute.
# Logging could be added here to monitor players trying to cuase problems and disable their accounts
# use module
use DBI;
# Config Variables
my $db_name = "schemaversus";
my $db_username = "schemaverse";
while (1){
# Make the master database connection
my $master_connection = DBI->connect("dbi:Pg:dbname=${db_name};host=localhost", $db_username);
my $sql = <<SQLSTATEMENT;
select
pid as pid,
pg_notify(get_player_error_channel(usename::character varying), 'The following query was canceled due to timeout: ' ||query ),
disable_fleet(CASE WHEN application_name ~ '^[0-9]+\$' THEN application_name::integer ELSE 0 END) as disabled,
usename as username,
query as current_query,
pg_cancel_backend(pid) as canceled
from
pg_stat_activity
where
datname = '${db_name}'
AND usename <> '${db_username}'
AND usename <> 'postgres'
AND
(
(
query LIKE '%FLEET_SCRIPT_%'
AND (now() - query_start) > COALESCE(
GET_FLEET_RUNTIME(CASE WHEN application_name ~ '^[0-9]+\$' THEN application_name::integer ELSE 0 END, usename::character varying),
'60 seconds'::interval)
)
OR
(
query NOT LIKE '<IDLE>%'
AND query NOT LIKE '%FLEET_SCRIPT_%'
AND now() - query_start > interval '60 seconds'
)
)
SQLSTATEMENT
my $rs = $master_connection->prepare($sql);
$rs->execute();
#while (($pid, $error_channel, $username, $current_query, $canceled) = $rs->fetchrow()) {}
$rs->finish;
$master_connection->disconnect();
sleep(30);
}