-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclean_samples_cache.pl
More file actions
executable file
·71 lines (57 loc) · 1.5 KB
/
clean_samples_cache.pl
File metadata and controls
executable file
·71 lines (57 loc) · 1.5 KB
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
66
67
68
69
70
71
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use DBI;
use Time::HiRes qw( usleep );
use Nabovarme::Db;
use Nabovarme::Utils;
$SIG{USR1} = \&clean_samples_cache;
$SIG{INT} = \&sig_int_handler;
log_info("starting...");
my $dbh;
my $sth;
my $d;
# connect to db
if ($dbh = Nabovarme::Db->my_connect) {
$dbh->{'mysql_auto_reconnect'} = 1;
log_info("connected to db\n");
}
else {
log_warn("cant't connect to db $!");
die $!;
}
clean_samples_cache();
while (1) {
sleep 60;
}
sub clean_samples_cache {
$sth = $dbh->prepare(qq[SELECT `serial`, `key` FROM meters WHERE `key` is not NULL]);
$sth->execute;
log_info("cleaning samples_cache for all meters");
while ($d = $sth->fetchrow_hashref) {
my $quoted_serial = $dbh->quote($d->{serial});
log_info("cleaning samples_cache for " . $d->{serial});
# send scan
$dbh->do(qq[DELETE FROM `samples_cache` WHERE SERIAL LIKE $quoted_serial AND `id` NOT IN ( \
SELECT `id` FROM (
SELECT a.`id` FROM (
SELECT * FROM nabovarme.samples_cache
WHERE `serial` LIKE $quoted_serial
AND FROM_UNIXTIME(`unix_time`) < NOW() - INTERVAL 7 DAY
ORDER BY `unix_time` DESC LIMIT 5
) a
UNION SELECT b.`id` FROM (
SELECT * FROM nabovarme.samples_cache
WHERE `serial` LIKE $quoted_serial
AND FROM_UNIXTIME(`unix_time`) >= NOW() - INTERVAL 7 DAY
ORDER BY `unix_time` DESC
) b
) save_list
)]) or log_warn($DBI::errstr);
}
}
sub sig_int_handler {
log_die("Interrupted" . $!);
}
1;
__END__