From e3759c7c55054ccbbb43a8bf6745b4e20132019b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Demians?= Date: Tue, 25 Oct 2022 13:09:14 +0200 Subject: [PATCH] Bug 31970 - background_jobs_purge.pl to purge background jobs queue With this script, you can see the state of the background jobs queue, finished jobs only, and delete entries, globally, or by job type. Koha::BackgroundJob is extended with a purge() method which could be used from Koha WUI. TO TEST : - On a Koha instance with a populated background_jobs table. - Go in misc/cronjobs directory. - ./background_jobs_purge.pl Display script usage. List of job's types is displayed. - ./background_jobs_purge.pl all 2 --verbose Display a list of jobs (all types) to be deleted. - ./background_jobs_purge.pl all 2 --verbose --confirm Display a list of jobs (all types) that have been deleted. - ./background_jobs_purge.pl all 2 --verbose --confirm Display a message: No job to purge - ./background_jobs_purge.pl update_elastic_index 0 -v Display a list of all update_elastic_index job - ./background_jobs_purge.pl update_elastic_index 0 -v -c Display a list of all update_elastic_index job that have been delete. This could be run several time each day... --- Koha/BackgroundJob.pm | 30 ++++++++++ misc/cronjobs/background_jobs_purge.pl | 82 ++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100755 misc/cronjobs/background_jobs_purge.pl diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index cb0f9adcea..4806e88048 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -510,6 +510,36 @@ sub to_api_mapping { }; } +=head3 purge + + Koha::BackgrounJob::purge(10, 1) + +Purge all jobs older than 10 days. If called from the base class, jobs of +all types will be purges, otherwise only the jobs from the specific type. + +=cut + +sub purge { + my ($self, $days, $doit) = @_; + my $purge_all = ref($self) eq 'Koha::BackgroundJob'; + my $duration = DateTime::Duration->new( days => $days ); + my $ended_on = DateTime->now - $duration; + $ended_on = $ended_on->ymd ." 23:59:59"; + my $query = { + status => 'finished', + ended_on => { '<=', $ended_on }, + }; + $query->{type} = $self->job_type if ref($self) ne 'Koha::BackgroundJob'; + my $jobs = Koha::BackgroundJobs->search($query, + { order_by => { -asc => 'enqueued_on' } } ); + my $result = []; + while ( my $job = $jobs->next ) { + push @$result, $job; + $job->delete if $doit; + } + return $result; +} + =head3 _type =cut diff --git a/misc/cronjobs/background_jobs_purge.pl b/misc/cronjobs/background_jobs_purge.pl new file mode 100755 index 0000000000..fffa860d6b --- /dev/null +++ b/misc/cronjobs/background_jobs_purge.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +# Copyright 2022 Tamil s.a.r.l. +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Getopt::Long qw( GetOptions ); +use Class::Load ':all'; +use Koha::BackgroundJob; +use YAML; + +use constant DEFAULT_DAYS => 30; + +my $type_to_class = Koha::BackgroundJob::core_types_to_classes; + +sub usage { + my $types = join("\n", map { " ► $_"; } sort keys %$type_to_class); + print STDERR < \$confirm, + 'verbose|v' => \$verbose, +); + +usage() unless @ARGV; + +for my $class (values %$type_to_class) { + load_class($class) +} + +while (@ARGV) { + my $type = shift @ARGV; + my $class = + $type =~ /^all$/i ? 'Koha::BackgroundJob' : $type_to_class->{$type}; + unless ($class) { + say "Invalid job type: $type"; + exit; + } + my $days = @ARGV && $ARGV[0] =~ /^[0-9]+$/ ? shift @ARGV : DEFAULT_DAYS; + $class = $class->new(); + my $jobs = $class->purge($days, $confirm); + next unless $verbose; + say "** TEST MODE **" unless $confirm; + my $count = @$jobs; + if ($count) { + say "Purge $count jobs of type '$type' older than $days days\n", + "ID TYPE FINISHED\n", + join("\n", map { + sprintf("%-8d %-35s %s", $_->id, $_->type, $_->ended_on); + } @$jobs); + } + else { + say "No job to purge"; + } +} -- 2.34.1