blib/lib/App/Netdisco/Worker/Plugin/Expire.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 175 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Worker::Plugin::Expire;
2  
3 use Dancer ':syntax';
4 use App::Netdisco::Worker::Plugin;
5 use aliased 'App::Netdisco::Worker::Status';
6  
7 use Dancer::Plugin::DBIC 'schema';
8 use App::Netdisco::JobQueue 'jq_insert';
9 use App::Netdisco::Util::Statistics 'update_stats';
10 use App::Netdisco::Util::DNS 'ipv4_from_hostname';
11 use App::Netdisco::DB::ExplicitLocking ':modes';
12 use App::Netdisco::Util::Permission 'acl_matches_only';
13  
14 register_worker({ phase => 'main' }, sub {
15 my ($job, $workerconf) = @_;
16  
17 if (setting('expire_devices') and ref {} eq ref setting('expire_devices')) {
18 foreach my $acl (keys %{ setting('expire_devices') }) {
19 my $days = setting('expire_devices')->{$acl};
20  
21 schema('netdisco')->txn_do(sub {
22 my @hostlist = schema('netdisco')->resultset('Device')->search({
23 -not_bool => 'is_pseudo',
24 last_discover => \[q/< (LOCALTIMESTAMP - ?::interval)/,
25 ($days * 86400)],
26 })->get_column('ip')->all;
27  
28 foreach my $ip (@hostlist) {
29 next unless acl_matches_only($ip, $acl);
30  
31 jq_insert([{
32 device => $ip,
33 action => 'delete',
34 }]);
35  
36 schema('netdisco')->resultset('UserLog')->create({
37 username => ($ENV{USER} || 'scheduled'),
38 userip => ipv4_from_hostname($job->backend || setting('workers')->{'BACKEND'}),
39 event => 'expire_devices',
40 details => $ip,
41 });
42 }
43 });
44 }
45 }
46  
47 if (setting('expire_nodes') and setting('expire_nodes') > 0) {
48 schema('netdisco')->txn_do(sub {
49 my $freshness = ((defined setting('expire_nodeip_freshness'))
50 ? setting('expire_nodeip_freshness') : setting('expire_nodes'));
51 if ($freshness) {
52 schema('netdisco')->resultset('NodeIp')->search({
53 time_last => \[q/< (LOCALTIMESTAMP - ?::interval)/, ($freshness * 86400)],
54 })->delete();
55 }
56  
57 schema('netdisco')->resultset('Node')->search({
58 time_last => \[q/< (LOCALTIMESTAMP - ?::interval)/,
59 (setting('expire_nodes') * 86400)],
60 })->delete();
61 });
62 }
63  
64 if (setting('expire_nodes_archive') and setting('expire_nodes_archive') > 0) {
65 schema('netdisco')->txn_do(sub {
66 my $freshness = ((defined setting('expire_nodeip_freshness'))
67 ? setting('expire_nodeip_freshness') : setting('expire_nodes_archive'));
68 if ($freshness) {
69 schema('netdisco')->resultset('NodeIp')->search({
70 time_last => \[q/< (LOCALTIMESTAMP - ?::interval)/, ($freshness * 86400)],
71 })->delete();
72 }
73  
74 schema('netdisco')->resultset('Node')->search({
75 -not_bool => 'active',
76 time_last => \[q/< (LOCALTIMESTAMP - ?::interval)/,
77 (setting('expire_nodes_archive') * 86400)],
78 })->delete();
79 });
80 }
81  
82 # node_wireless is otherwise only cleared when a node row for the same mac
83 # is deleted, and a client still seen anywhere keeps its node row fresh, so
84 # every (mac, ssid) pair it ever held - an SSID it roamed away from, or an
85 # "unknown" row from before a gap in the SSID walk was skipped - would stay
86 # for good. Macsuck refreshes time_last on each pair it sees, so age alone
87 # tells a pair that is no longer reported. This deliberately does not pick
88 # "unknown" out by name: on classes with no cd11_ssid at all it is still the
89 # only row a client has, and is refreshed like any other.
90 #
91 # Unset, it follows node expiry, falling back to the archive age so that a
92 # site which keeps active nodes for good but expires archived ones still
93 # ages node_wireless out. A NULL time_last is a pair not seen since the
94 # column gained its default, as every macsuck sets it, so it goes too.
95 my $wireless_age = ((defined setting('expire_node_wireless'))
96 ? setting('expire_node_wireless')
97 : (setting('expire_nodes') || setting('expire_nodes_archive')));
98 if ($wireless_age and $wireless_age > 0) {
99 schema('netdisco')->txn_do(sub {
100 schema('netdisco')->resultset('NodeWireless')->search({
101 -or => [
102 { time_last => undef },
103 { time_last => \[q/< (LOCALTIMESTAMP - ?::interval)/,
104 ($wireless_age * 86400)] },
105 ],
106 })->delete();
107 });
108 }
109  
110 # also clean up node_ip entries that have no corresponding node
111 if (setting('expire_nodeip_orphans')) {
112 schema('netdisco')->resultset('NodeIp')->search({
113 mac => { -in => schema('netdisco')->resultset('NodeIp')->search(
114 { port => undef },
115 { join => 'nodes', select => [{ distinct => 'me.mac' }], }
116 )->as_query },
117 })->delete;
118 }
119  
120 if (setting('expire_jobs') and setting('expire_jobs') > 0) {
121 schema('netdisco')->txn_do_locked('admin', EXCLUSIVE, sub {
122 schema('netdisco')->resultset('Admin')->search({
123 entered => \[q/< (LOCALTIMESTAMP - ?::interval)/,
124 (setting('expire_jobs') * 86400)],
125 })->delete();
126 });
127 }
128  
129 if (setting('expire_userlog') and setting('expire_userlog') > 0) {
130 schema('netdisco')->txn_do_locked('admin', EXCLUSIVE, sub {
131 schema('netdisco')->resultset('UserLog')->search({
132 creation => \[q/< (LOCALTIMESTAMP - ?::interval)/,
133 (setting('expire_userlog') * 86400)],
134 })->delete();
135 });
136 }
137  
138 # now update stats
139 update_stats();
140  
141 return Status->done('Checked expiry and updated stats');
142 });
143  
144 true;