View | Details | Raw Unified | Return to bug 31969
Collapse All | Expand All

(-)a/misc/cronjobs/cleanup_database.pl (-2 / +49 lines)
Lines 27-32 use constant DEFAULT_MESSAGES_PURGEDAYS => 365; Link Here
27
use constant DEFAULT_SEARCHHISTORY_PURGEDAYS      => 30;
27
use constant DEFAULT_SEARCHHISTORY_PURGEDAYS      => 30;
28
use constant DEFAULT_SHARE_INVITATION_EXPIRY_DAYS => 14;
28
use constant DEFAULT_SHARE_INVITATION_EXPIRY_DAYS => 14;
29
use constant DEFAULT_DEBARMENTS_PURGEDAYS         => 30;
29
use constant DEFAULT_DEBARMENTS_PURGEDAYS         => 30;
30
use constant DEFAULT_BGJOBS_PURGEDAYS             => 1;
31
use constant DEFAULT_BGJOBS_PURGETYPES            => qw{ update_elastic_index };
30
32
31
use Koha::Script -cron;
33
use Koha::Script -cron;
32
use C4::Context;
34
use C4::Context;
Lines 36-41 use Getopt::Long qw( GetOptions ); Link Here
36
use C4::Log qw( cronlogaction );
38
use C4::Log qw( cronlogaction );
37
use C4::Accounts qw( purge_zero_balance_fees );
39
use C4::Accounts qw( purge_zero_balance_fees );
38
use Koha::UploadedFiles;
40
use Koha::UploadedFiles;
41
use Koha::BackgroundJobs;
39
use Koha::Old::Biblios;
42
use Koha::Old::Biblios;
40
use Koha::Old::Items;
43
use Koha::Old::Items;
41
use Koha::Old::Biblioitems;
44
use Koha::Old::Biblioitems;
Lines 49-55 use Koha::Patron::Debarments qw( DelDebarment ); Link Here
49
52
50
sub usage {
53
sub usage {
51
    print STDERR <<USAGE;
54
    print STDERR <<USAGE;
52
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]
55
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] ]
53
56
54
   -h --help          prints this help message, and exits, ignoring all
57
   -h --help          prints this help message, and exits, ignoring all
55
                      other options
58
                      other options
Lines 107-112 Usage: $0 [-h|--help] [--confirm] [--sessions] [--sessdays DAYS] [-v|--verbose] Link Here
107
   --cards DAY             Purge card creator batches last added to more than DAYS days ago.
110
   --cards DAY             Purge card creator batches last added to more than DAYS days ago.
108
   --return-claims         Purge all resolved return claims older than the number of days specified in
111
   --return-claims         Purge all resolved return claims older than the number of days specified in
109
                           the system preference CleanUpDatabaseReturnClaims.
112
                           the system preference CleanUpDatabaseReturnClaims.
113
   --bg-days DAYS          Purge all finished background jobs this many days old. Defaults to 1 if no DAYS provided.
114
   --bg-type TYPES         What type of background job to purge. Defaults to "update_elastic_index" if omitted
115
                           Specifying "all" will purge all types. Repeatable.
110
USAGE
116
USAGE
111
    exit $_[0];
117
    exit $_[0];
112
}
118
}
Lines 148-153 my $labels; Link Here
148
my $cards;
154
my $cards;
149
my @log_modules;
155
my @log_modules;
150
my @preserve_logs;
156
my @preserve_logs;
157
my $background_days;
158
my @background_types;
151
159
152
my $command_line_options = join(" ",@ARGV);
160
my $command_line_options = join(" ",@ARGV);
153
161
Lines 190-195 GetOptions( Link Here
190
    'labels'            => \$labels,
198
    'labels'            => \$labels,
191
    'cards'             => \$cards,
199
    'cards'             => \$cards,
192
    'return-claims'     => \$return_claims,
200
    'return-claims'     => \$return_claims,
201
    'bg-type:s'        => \@background_types,
202
    'bg-jobs:i'         => \$background_days,
193
) || usage(1);
203
) || usage(1);
194
204
195
# Use default values
205
# Use default values
Lines 202-207 $pSearchhistory = DEFAULT_SEARCHHISTORY_PURGEDAYS if defined($pSearchhis Link Here
202
$pListShareInvites = DEFAULT_SHARE_INVITATION_EXPIRY_DAYS if defined($pListShareInvites) && $pListShareInvites == 0;
212
$pListShareInvites = DEFAULT_SHARE_INVITATION_EXPIRY_DAYS if defined($pListShareInvites) && $pListShareInvites == 0;
203
$pDebarments       = DEFAULT_DEBARMENTS_PURGEDAYS         if defined($pDebarments)       && $pDebarments == 0;
213
$pDebarments       = DEFAULT_DEBARMENTS_PURGEDAYS         if defined($pDebarments)       && $pDebarments == 0;
204
$pMessages         = DEFAULT_MESSAGES_PURGEDAYS           if defined($pMessages)         && $pMessages == 0;
214
$pMessages         = DEFAULT_MESSAGES_PURGEDAYS           if defined($pMessages)         && $pMessages == 0;
215
$background_days   = DEFAULT_BGJOBS_PURGEDAYS             if defined($background_days)   && $background_days == 0;
216
@background_types  = (DEFAULT_BGJOBS_PURGETYPES)          if $background_days            && @background_types == 0;
205
217
206
if ($help) {
218
if ($help) {
207
    usage(0);
219
    usage(0);
Lines 239-244 unless ( $sessions Link Here
239
    || $labels
251
    || $labels
240
    || $cards
252
    || $cards
241
    || $return_claims
253
    || $return_claims
254
    || $background_days
242
) {
255
) {
243
    print "You did not specify any cleanup work for the script to do.\n\n";
256
    print "You did not specify any cleanup work for the script to do.\n\n";
244
    usage(1);
257
    usage(1);
Lines 675-680 if ($cards) { Link Here
675
    }
688
    }
676
}
689
}
677
690
691
if ($background_days) {
692
    print "Purging background jobs more than $background_days days ago.\n" if $verbose;
693
    my $count = PurgeBackgroundJobs($background_days, \@background_types, $confirm);
694
    if ($verbose) {
695
        say $confirm
696
          ? sprintf "Done with purging %d background jobs of type(s): %s added more than %d days ago.\n", $count, join(',', @background_types), $background_days
697
          : sprintf "%d background jobs of type(s): %s added more than %d days ago would have been purged.", $count, join(',', @background_types), $background_days;
698
    }
699
}
700
678
cronlogaction({ action => 'End', info => "COMPLETED" });
701
cronlogaction({ action => 'End', info => "COMPLETED" });
679
702
680
exit(0);
703
exit(0);
Lines 812-814 sub DeleteSpecialHolidays { Link Here
812
    my $count = $sth->execute( $days ) + 0;
835
    my $count = $sth->execute( $days ) + 0;
813
    print "Removed $count unique holidays\n" if $verbose;
836
    print "Removed $count unique holidays\n" if $verbose;
814
}
837
}
815
- 
838
839
sub PurgeBackgroundJobs {
840
    my ( $days, $types, $confirm ) = @_;
841
842
    my $rs;
843
    if ( $types->[0] eq 'all' ){
844
        $rs = Koha::BackgroundJobs->search(
845
            {
846
                ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] },
847
                status   => 'finished',
848
            });
849
850
    } else {
851
        $rs = Koha::BackgroundJobs->search(
852
            {
853
                ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] },
854
                type     => \@{$types},
855
                status   => 'finished',
856
            });
857
    }
858
    my $count = $rs->count();
859
    $rs->delete if $confirm;
860
861
    return $count;
862
}

Return to bug 31969