From a05833b773d9f78502d8a74eb89195187239f428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nyl=C3=A9n?= Date: Wed, 26 Oct 2022 14:45:23 +0200 Subject: [PATCH 1/2] Bug 31969: (follow-up) Move deletion code to Koha::BackgroundJobs->purge for reuse. --- Koha/BackgroundJobs.pm | 45 +++++++++++++++++++++++++++++++ misc/cronjobs/cleanup_database.pl | 30 ++++----------------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/Koha/BackgroundJobs.pm b/Koha/BackgroundJobs.pm index 48afbf68fa..8d7ae64e0e 100644 --- a/Koha/BackgroundJobs.pm +++ b/Koha/BackgroundJobs.pm @@ -72,6 +72,51 @@ sub filter_by_current { ); } +=head3 purge + + my $params = { job_types => ('all') , # Arrayref of jobtypes to be purged + days => 1, # Age in days of jobs to be purged + confirm => 1, # Confirm deletion + }; + my $count = Koha::BackgroundJobs->purge( $params ); + +Deletes finished background jobs. Returns the number of jobs that was / would've been deleted. + +=cut + +sub purge { + my ( $self, $params) = @_; + + return 0 unless ( exists $params->{job_types} && scalar @{ $params->{job_types} } >= 1 ); + return 0 unless ( exists $params->{days} ); + + my $types = $params->{job_types}; + my $days = $params->{days}; + + my $confirm = exists $params->{confirm} ? $params->{confirm} : 0; + + my $rs; + if ( $types->[0] eq 'all' ){ + $rs = $self->search( + { + ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] }, + status => 'finished', + }); + + } else { + $rs = $self->search( + { + ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] }, + type => $types, + status => 'finished', + }); + } + my $count = $rs->count(); + $rs->delete if $confirm; + + return $count; +} + =head2 Internal methods =head3 _type diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 612ae3639b..aff16f987b 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -690,7 +690,11 @@ 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); + my $params = { job_types => \@background_types , + days => $background_days, + confirm => $confirm, + }; + my $count = Koha::BackgroundJobs->purge( $params ); 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 @@ -836,27 +840,3 @@ sub DeleteSpecialHolidays { 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.34.1