Make expire_nodes and expire_nodes_archive behave correctly

This commit is contained in:
Oliver Gorwits
2018-01-04 20:17:47 +00:00
parent 1c877ed4b3
commit 94f80f3c70
2 changed files with 33 additions and 26 deletions

View File

@@ -112,39 +112,38 @@ sub delete {
foreach my $set (qw/
NodeMonitor
NodeWireless
NodeNbt
/) {
$schema->resultset($set)->search(
{ mac => { '-in' => $nodes->as_query }},
)->delete;
}
# for node_ip only delete if there are no longer
# any nodes referencing the IP.
my $except = $nodes->search(undef, { '+columns' => 'port' })->as_query;
my @except_all = @{$$except};
my $except_query = shift @except_all;
my $deleted = $schema->storage->dbh_do(sub {
my ($storage, $dbh, @extra) = @_;
local $dbh->{TraceLevel} = '1|SQL';
$dbh->do(<<SQL
WITH nodes_restrict AS ($except_query)
DELETE FROM node_ip WHERE mac IN (
SELECT mac FROM node_ip
LEFT OUTER JOIN (
SELECT mac, port FROM node
EXCEPT (SELECT * FROM nodes_restrict)) exceptions
USING (mac)
WHERE node_ip.mac IN (SELECT mac FROM nodes_restrict)
AND exceptions.port IS NULL)
SQL
, undef, (map {$_->[1]} @except_all));
});
# now let DBIC do its thing
my @retval = ($self->next::method());
# for node_ip and node_nbt *only* delete if there are no longer
# any active nodes referencing the IP or NBT (hence 2nd IN clause).
foreach my $set (qw/
NodeIp
NodeNbt
/) {
$schema->resultset($set)->search({
'me.mac' => { '-in' => $schema->resultset($set)->search({
'-and' => [
-bool => 'nodes.active',
'me.mac' => { '-in' => $nodes->as_query }
]
},
{
columns => 'mac',
join => 'nodes',
group_by => 'me.mac',
having => \[ 'count(nodes.mac) = 0' ],
})->as_query,
},
})->delete;
}
return (wantarray ? @retval : $retval[0]);
return $self->next::method();
}
}

View File

@@ -40,6 +40,14 @@ register_worker({ phase => 'main' }, sub {
});
}
# also have to clean up node_ip that have no correspoding node
schema('netdisco')->resultset('NodeIp')->search({
mac => { -in => schema('netdisco')->resultset('NodeIp')->search(
{ port => undef },
{ join => 'nodes', select => [{ distinct => 'me.mac' }], }
)->as_query },
})->delete;
if (setting('expire_jobs') and setting('expire_jobs') > 0) {
schema('netdisco')->txn_do_locked('admin', 'EXCLUSIVE', sub {
schema('netdisco')->resultset('Admin')->search({