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;
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;
sub usage {
print STDERR <<USAGE;
-Usage: $0 [-h|--help] [--confirm] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions] [--fees DAYS] [--temp-uploads] [--temp-uploads-days DAYS] [--uploads-missing 0|1 ] [--statistics DAYS] [--deleted-catalog DAYS] [--deleted-patrons DAYS] [--old-issues DAYS] [--old-reserves DAYS] [--transfers DAYS] [--labels DAYS] [--cards DAYS]
+Usage: $0 [-h|--help] [--confirm] [--sessions] [--sessdays DAYS] [-v|--verbose] [--zebraqueue DAYS] [-m|--mail] [--merged] [--import DAYS] [--logs DAYS] [--searchhistory DAYS] [--restrictions DAYS] [--all-restrictions] [--fees DAYS] [--temp-uploads] [--temp-uploads-days DAYS] [--uploads-missing 0|1 ] [--statistics DAYS] [--deleted-catalog DAYS] [--deleted-patrons DAYS] [--old-issues DAYS] [--old-reserves DAYS] [--transfers DAYS] [--labels DAYS] [--cards DAYS] [--bg-jobs DAYS [--bg-type TYPE] ]
-h --help prints this help message, and exits, ignoring all
other options
In the case of --fees, DAYS must be greater than
or equal to 1.
--log-modules Specify which action log modules to trim. Repeatable.
- --preserve-logs Specify which action logs to exclude. Repeatable.
+ --preserve-log Specify which action logs to exclude. Repeatable.
--logs DAYS purge entries from action_logs older than DAYS days.
Defaults to 180 days if no days specified.
--searchhistory DAYS purge entries from search_history older than DAYS days.
--cards DAY Purge card creator batches last added to more than DAYS days ago.
--return-claims Purge all resolved return claims older than the number of days specified in
the system preference CleanUpDatabaseReturnClaims.
+ --bg-days DAYS Purge all finished background jobs this many days old. Defaults to 1 if no DAYS provided.
+ --bg-type TYPES What type of background job to purge. Defaults to "update_elastic_index" if omitted
+ Specifying "all" will purge all types. Repeatable.
USAGE
exit $_[0];
}
my $cards;
my @log_modules;
my @preserve_logs;
+my $background_days;
+my @background_types;
+
+my $command_line_options = join(" ",@ARGV);
GetOptions(
'h|help' => \$help,
'restrictions:i' => \$pDebarments,
'all-restrictions' => \$allDebarments,
'del-exp-selfreg' => \$pExpSelfReg,
- 'del-unv-selfreg' => \$pUnvSelfReg,
+ 'del-unv-selfreg:i' => \$pUnvSelfReg,
'unique-holidays:i' => \$special_holidays_days,
'temp-uploads' => \$temp_uploads,
'temp-uploads-days:i' => \$temp_uploads_days,
'labels' => \$labels,
'cards' => \$cards,
'return-claims' => \$return_claims,
+ 'bg-type:s' => \@background_types,
+ 'bg-jobs:i' => \$background_days,
) || usage(1);
# Use default values
$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);
|| $labels
|| $cards
|| $return_claims
+ || $background_days
) {
print "You did not specify any cleanup work for the script to do.\n\n";
usage(1);
say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
-cronlogaction() unless $confirm;
+cronlogaction({ info => $command_line_options });
my $dbh = C4::Context->dbh();
my $sth;
print "Purging old reserves older than $pOldReserves days.\n" if $verbose;
my $old_reserves = Koha::Old::Holds->filter_by_last_update( { days => $pOldReserves } );
my $count = $old_reserves->count;
- $old_reserves->delete if $verbose;
+ $old_reserves->delete if $confirm;
if ($verbose) {
say $confirm
? sprintf "Done with purging %d old reserves.", $count
}
);
my $count = $transfers->count;
- $transfers->delete if $verbose;
+ $transfers->delete if $confirm;
if ($verbose) {
say $confirm
? sprintf "Done with purging %d transfers.", $count
}
}
+if ($background_days) {
+ print "Purging background jobs more than $background_days days ago.\n" if $verbose;
+ 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
+ : 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);
sub RemoveOldSessions {
my $count = $sth->execute( $days ) + 0;
print "Removed $count unique holidays\n" if $verbose;
}
+