From 067074418de167d88d2176e54499cfd30f98957e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nyl=C3=A9n?= Date: Mon, 24 Oct 2022 15:12:03 +0200 Subject: [PATCH] Bug 31969: Add background queue options to cleanup_database This patch adds background queue options to cleanup_database.pl to allow for purging completed background jobs. --bg-jobs DAYS Purge all finished background jobs this many days old. Defaults to 1 if no DAYS provided. --bg-type TYPE What type of background job to purge. Defaults to "update_elastic_index" if omitted Specifying "all" will purge all types. Repeatable. To test: 1 - Enable elastic search in Koha 2 - perl misc/maintenance/touch_all_items.pl 3 - Generate an number of diffrent types of bg-jobs (eg batch_hold_cancel, batch_biblio_record_deletion, batch_item_record_deletion) 4 - Check db and note there are a bunch of diffrent jobs 5 - Update to make them old UPDATE background_jobs SET ended_on = '2022-10-01 00:00:00', status='finished' 6 - perl misc/cronjobs/cleanup_database.pl 7 - Note bg-jobs entry shows in help 8 - perl misc/cronjobs/cleanup_database.pl --bg-jobs 1 -v 9 - Note that elasticqueue would have been cleared 10 - perl misc/cronjobs/cleanup_database.pl --bg-jobs 1 -v --confirm 11 - Note that number of entries deleted is reported 12 - Attempt to clear other job types, including "all" eg perl misc/cronjobs/cleanup_database.pl --bg-jobs 1 --bg-type batch_item_record_deletion -v --confirm perl misc/cronjobs/cleanup_database.pl --bg-jobs 1 --bg-type all -v --confirm 13 - Confirm in staff interface that jobs are gone: http://localhost:8081/cgi-bin/koha/admin/background_jobs.pl (Uncheck 'Current jobs only') Signed-off-by: Nick Clemens --- misc/cronjobs/cleanup_database.pl | 50 ++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index dc62ef8aea..612ae3639b 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -27,6 +27,8 @@ use constant DEFAULT_MESSAGES_PURGEDAYS => 365; use constant DEFAULT_SEARCHHISTORY_PURGEDAYS => 30; use constant DEFAULT_SHARE_INVITATION_EXPIRY_DAYS => 14; use constant DEFAULT_DEBARMENTS_PURGEDAYS => 30; +use constant DEFAULT_BGJOBS_PURGEDAYS => 1; +use constant DEFAULT_BGJOBS_PURGETYPES => qw{ update_elastic_index }; use Koha::Script -cron; use C4::Context; @@ -36,6 +38,7 @@ use Getopt::Long qw( GetOptions ); use C4::Log qw( cronlogaction ); use C4::Accounts qw( purge_zero_balance_fees ); use Koha::UploadedFiles; +use Koha::BackgroundJobs; use Koha::Old::Biblios; use Koha::Old::Items; use Koha::Old::Biblioitems; @@ -49,7 +52,7 @@ use Koha::Patron::Debarments qw( DelDebarment ); sub usage { print STDERR < \$labels, 'cards' => \$cards, 'return-claims' => \$return_claims, + 'bg-type:s' => \@background_types, + 'bg-jobs:i' => \$background_days, ) || usage(1); # Use default values @@ -202,6 +212,8 @@ $pSearchhistory = DEFAULT_SEARCHHISTORY_PURGEDAYS if defined($pSearchhis $pListShareInvites = DEFAULT_SHARE_INVITATION_EXPIRY_DAYS if defined($pListShareInvites) && $pListShareInvites == 0; $pDebarments = DEFAULT_DEBARMENTS_PURGEDAYS if defined($pDebarments) && $pDebarments == 0; $pMessages = DEFAULT_MESSAGES_PURGEDAYS if defined($pMessages) && $pMessages == 0; +$background_days = DEFAULT_BGJOBS_PURGEDAYS if defined($background_days) && $background_days == 0; +@background_types = (DEFAULT_BGJOBS_PURGETYPES) if $background_days && @background_types == 0; if ($help) { usage(0); @@ -239,6 +251,7 @@ unless ( $sessions || $labels || $cards || $return_claims + || $background_days ) { print "You did not specify any cleanup work for the script to do.\n\n"; usage(1); @@ -675,6 +688,16 @@ if ($cards) { } } +if ($background_days) { + print "Purging background jobs more than $background_days days ago.\n" if $verbose; + my $count = PurgeBackgroundJobs($background_days, \@background_types, $confirm); + if ($verbose) { + say $confirm + ? sprintf "Done with purging %d background jobs of type(s): %s added more than %d days ago.\n", $count, join(',', @background_types), $background_days + : sprintf "%d background jobs of type(s): %s added more than %d days ago would have been purged.", $count, join(',', @background_types), $background_days; + } +} + cronlogaction({ action => 'End', info => "COMPLETED" }); exit(0); @@ -812,3 +835,28 @@ sub DeleteSpecialHolidays { my $count = $sth->execute( $days ) + 0; print "Removed $count unique holidays\n" if $verbose; } + +sub PurgeBackgroundJobs { + my ( $days, $types, $confirm ) = @_; + + my $rs; + if ( $types->[0] eq 'all' ){ + $rs = Koha::BackgroundJobs->search( + { + ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] }, + status => 'finished', + }); + + } else { + $rs = Koha::BackgroundJobs->search( + { + ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] }, + type => \@{$types}, + status => 'finished', + }); + } + my $count = $rs->count(); + $rs->delete if $confirm; + + return $count; +} -- 2.30.2