From 0ab04c2810268dbcf5c2376470b1758cb2436bda Mon Sep 17 00:00:00 2001 From: Henrik Andersen Date: Wed, 8 May 2024 08:08:42 +0200 Subject: [PATCH] Reimplementation of previous repair, but without performance hit on large amount of jobs --- lib/Minion/Backend/Pg.pm | 11 ++++++++--- t/pg.t | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Minion/Backend/Pg.pm b/lib/Minion/Backend/Pg.pm index 9ac18c3..cdc830b 100644 --- a/lib/Minion/Backend/Pg.pm +++ b/lib/Minion/Backend/Pg.pm @@ -178,9 +178,14 @@ sub repair { my $minion = $self->minion; $db->query("DELETE FROM minion_workers WHERE notified < NOW() - INTERVAL '1 second' * ?", $minion->missing_after); - # Old jobs - $db->query("DELETE FROM minion_jobs WHERE state = 'finished' AND finished <= NOW() - INTERVAL '1 second' * ?", - $minion->remove_after); + # Old jobs with no unresolved dependencies + $db->query( + "DELETE FROM minion_jobs + WHERE id IN ( + SELECT id FROM minion_jobs WHERE state = 'finished' AND finished <= NOW() - INTERVAL '1 second' * ? + EXCEPT SELECT unnest(parents) AS id FROM minion_jobs WHERE state != 'finished' + )", $minion->remove_after + ); # Expired jobs $db->query("DELETE FROM minion_jobs WHERE state = 'inactive' AND expires <= NOW()"); diff --git a/t/pg.t b/t/pg.t index 68ac4d8..2653769 100644 --- a/t/pg.t +++ b/t/pg.t @@ -1116,9 +1116,9 @@ subtest 'Job dependencies' => sub { is_deeply $job->info->{children}, [], 'right children'; is_deeply $job->info->{parents}, [$id, $id2], 'right parents'; is $minion->stats->{finished_jobs}, 2, 'two finished jobs'; - is $minion->repair->stats->{finished_jobs}, 0, 'no finished jobs'; + is $minion->repair->stats->{finished_jobs}, 2, 'still two finished jobs'; ok $job->finish, 'job finished'; - is $minion->stats->{finished_jobs}, 1, 'one finished job'; + is $minion->stats->{finished_jobs}, 3, 'three finished job'; is $minion->repair->remove_after(172800)->stats->{finished_jobs}, 0, 'no finished jobs'; $id = $minion->enqueue(test => [] => {parents => [-1]}); ok $job = $worker->dequeue(0), 'job dequeued';