Bug 31969: (follow-up) Move deletion code to Koha::BackgroundJobs->purge for reuse.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Björn Nylén 2022-10-26 14:45:23 +02:00 committed by Tomas Cohen Arazi
parent 6565328092
commit 78219e72f6
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
2 changed files with 50 additions and 25 deletions

View File

@ -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

View File

@ -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;
}