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

(-)a/Koha/BackgroundJobs.pm (-45 lines)
Lines 72-122 sub filter_by_current { Link Here
72
    );
72
    );
73
}
73
}
74
74
75
=head3 purge
76
77
    my $params = { job_types => ('all') , # Arrayref of jobtypes to be purged
78
                   days      => 1,        # Age in days of jobs to be purged
79
                   confirm   => 1,        # Confirm deletion
80
                };
81
    my $count = Koha::BackgroundJobs->purge( $params );
82
83
Deletes finished background jobs. Returns the number of jobs that was / would've been deleted.
84
85
=cut
86
87
sub purge {
88
    my ( $self, $params) = @_;
89
90
    return 0 unless ( exists $params->{job_types} && scalar @{ $params->{job_types} }  >= 1 );
91
    return 0 unless ( exists $params->{days} );
92
93
    my $types = $params->{job_types};
94
    my $days  = $params->{days};
95
96
    my $confirm = exists $params->{confirm} ? $params->{confirm} : 0;
97
98
    my $rs;
99
    if ( $types->[0] eq 'all' ){
100
        $rs = $self->search(
101
            {
102
                ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] },
103
                status   => 'finished',
104
            });
105
106
    } else {
107
        $rs = $self->search(
108
            {
109
                ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] },
110
                type     => $types,
111
                status   => 'finished',
112
            });
113
    }
114
    my $count = $rs->count();
115
    $rs->delete if $confirm;
116
117
    return $count;
118
}
119
120
=head2 Internal methods
75
=head2 Internal methods
121
76
122
=head3 _type
77
=head3 _type
(-)a/misc/cronjobs/cleanup_database.pl (-20 / +31 lines)
Lines 27-34 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;
30
use constant DEFAULT_JOBS_PURGEDAYS               => 1;
31
use constant DEFAULT_BGJOBS_PURGETYPES            => qw{ update_elastic_index };
31
use constant DEFAULT_JOBS_PURGETYPES              => qw{ update_elastic_index };
32
32
33
use Koha::Script -cron;
33
use Koha::Script -cron;
34
use C4::Context;
34
use C4::Context;
Lines 110-117 Usage: $0 [-h|--help] [--confirm] [--sessions] [--sessdays DAYS] [-v|--verbose] Link Here
110
   --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.
111
   --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
112
                           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.
113
   --jobs-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
114
   --jobs-type TYPES       What type of background job to purge. Defaults to "update_elastic_index" if omitted
115
                           Specifying "all" will purge all types. Repeatable.
115
                           Specifying "all" will purge all types. Repeatable.
116
USAGE
116
USAGE
117
    exit $_[0];
117
    exit $_[0];
Lines 154-161 my $labels; Link Here
154
my $cards;
154
my $cards;
155
my @log_modules;
155
my @log_modules;
156
my @preserve_logs;
156
my @preserve_logs;
157
my $background_days;
157
my $jobs_days;
158
my @background_types;
158
my @jobs_types;
159
159
160
my $command_line_options = join(" ",@ARGV);
160
my $command_line_options = join(" ",@ARGV);
161
161
Lines 198-205 GetOptions( Link Here
198
    'labels'            => \$labels,
198
    'labels'            => \$labels,
199
    'cards'             => \$cards,
199
    'cards'             => \$cards,
200
    'return-claims'     => \$return_claims,
200
    'return-claims'     => \$return_claims,
201
    'bg-type:s'        => \@background_types,
201
    'jobs-type:s'       => \@jobs_types,
202
    'bg-days:i'         => \$background_days,
202
    'jobs-days:i'       => \$jobs_days,
203
) || usage(1);
203
) || usage(1);
204
204
205
# Use default values
205
# Use default values
Lines 212-219 $pSearchhistory = DEFAULT_SEARCHHISTORY_PURGEDAYS if defined($pSearchhis Link Here
212
$pListShareInvites = DEFAULT_SHARE_INVITATION_EXPIRY_DAYS if defined($pListShareInvites) && $pListShareInvites == 0;
212
$pListShareInvites = DEFAULT_SHARE_INVITATION_EXPIRY_DAYS if defined($pListShareInvites) && $pListShareInvites == 0;
213
$pDebarments       = DEFAULT_DEBARMENTS_PURGEDAYS         if defined($pDebarments)       && $pDebarments == 0;
213
$pDebarments       = DEFAULT_DEBARMENTS_PURGEDAYS         if defined($pDebarments)       && $pDebarments == 0;
214
$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;
215
$jobs_days         = DEFAULT_JOBS_PURGEDAYS               if defined($jobs_days)         && $jobs_days == 0;
216
@background_types  = (DEFAULT_BGJOBS_PURGETYPES)          if $background_days            && @background_types == 0;
216
@jobs_types        = (DEFAULT_JOBS_PURGETYPES)            if $jobs_days                  && @jobs_types == 0;
217
217
218
if ($help) {
218
if ($help) {
219
    usage(0);
219
    usage(0);
Lines 251-257 unless ( $sessions Link Here
251
    || $labels
251
    || $labels
252
    || $cards
252
    || $cards
253
    || $return_claims
253
    || $return_claims
254
    || $background_days
254
    || $jobs_days
255
) {
255
) {
256
    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";
257
    usage(1);
257
    usage(1);
Lines 688-704 if ($cards) { Link Here
688
    }
688
    }
689
}
689
}
690
690
691
if ($background_days) {
691
if ($jobs_days) {
692
    print "Purging background jobs more than $background_days days ago.\n" if $verbose;
692
    print "Purging background jobs more than $jobs_days days ago.\n"
693
    my $params = { job_types => \@background_types ,
693
      if $verbose;
694
                   days      => $background_days,
694
    my $jobs = Koha::BackgroundJobs->search(
695
                   confirm   => $confirm,
695
        {
696
                };
696
            status => 'finished',
697
    my $count = Koha::BackgroundJobs->purge( $params );
697
            ( $jobs_types[0] eq 'all' ? () : ( type => \@jobs_types ) )
698
        }
699
    )->filter_by_last_update(
700
        {
701
            timestamp_column_name => 'ended_on',
702
            days => $jobs_days,
703
        }
704
    );
705
    my $count = $jobs->count;
706
    $jobs->delete if $confirm;
698
    if ($verbose) {
707
    if ($verbose) {
699
        say $confirm
708
        say $confirm
700
          ? sprintf "Done with purging %d background jobs of type(s): %s added more than %d days ago.\n", $count, join(',', @background_types), $background_days
709
          ? sprintf "Done with purging %d background jobs of type(s): %s added more than %d days ago.\n",
701
          : sprintf "%d background jobs of type(s): %s added more than %d days ago would have been purged.", $count, join(',', @background_types), $background_days;
710
          $count, join( ',', @jobs_types ), $jobs_days
711
          : sprintf "%d background jobs of type(s): %s added more than %d days ago would have been purged.",
712
          $count, join( ',', @jobs_types ), $jobs_days;
702
    }
713
    }
703
}
714
}
704
715
(-)a/t/db_dependent/Koha/BackgroundJobs.t (-67 / +1 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 15;
22
use Test::More tests => 14;
23
use Test::MockModule;
23
use Test::MockModule;
24
24
25
use List::MoreUtils qw(any);
25
use List::MoreUtils qw(any);
Lines 140-207 subtest 'search_limited' => sub { Link Here
140
140
141
    $schema->storage->txn_rollback;
141
    $schema->storage->txn_rollback;
142
};
142
};
143
144
subtest 'purge' => sub {
145
    plan tests => 9;
146
    $schema->storage->txn_begin;
147
148
    my $recent_date = dt_from_string;
149
    my $old_date = dt_from_string->subtract({ days => 3 });
150
    my $job_recent_t1_new      = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'new', ended_on => $old_date, type => 'type1' } } );
151
    my $job_recent_t2_fin = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $recent_date, type => 'type2' } } );
152
    my $job_old_t1_fin    = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $old_date, type => 'type1' } } );
153
    my $job_old_t2_fin  = $builder->build_object( { class => 'Koha::BackgroundJobs', value => { status => 'finished', ended_on => $old_date, type => 'type2' } } );
154
155
    my $params = { job_types => ['type1'] , # Arrayref of jobtypes to be purged
156
                   days      => 1,        # Age in days of jobs to be purged
157
                   confirm   => 0,        # Confirm deletion
158
                };
159
    is( Koha::BackgroundJobs->purge($params), 1, 'Only the old finished type1 job would be purged' );
160
161
    $params->{'job_types'} = ['all'];
162
    is( Koha::BackgroundJobs->purge($params), 2, 'All finished old jobs would be purged with job_types = all' );
163
164
    my $rs = Koha::BackgroundJobs->search(
165
        {
166
            id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
167
        }
168
    );
169
    is( $rs->count, 4, 'All jobs still left in queue');
170
171
    $params->{'job_types'} = ['type1'];
172
    $params->{'confirm'} = 1;
173
    is( Koha::BackgroundJobs->purge($params), 1, 'Only the old finished type1 job is purged' );
174
175
    $rs = Koha::BackgroundJobs->search(
176
        {
177
            id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
178
        }
179
    );
180
    is( $rs->count, 3, '3 jobs still left in queue');
181
182
    $params->{'job_types'} = ['all'];
183
    is( Koha::BackgroundJobs->purge($params), 1, 'The remaining old finished jobs is purged' );
184
        $rs = Koha::BackgroundJobs->search(
185
        {
186
            id => [ $job_recent_t1_new->id, $job_recent_t2_fin->id, $job_old_t1_fin->id, $job_old_t2_fin->id ]
187
        }
188
    );
189
    is( $rs->count, 2, '2 jobs still left in queue');
190
191
    $rs = Koha::BackgroundJobs->search(
192
        {
193
            id => [ $job_recent_t1_new->id ]
194
        }
195
    );
196
    is( $rs->count, 1, 'Unfinished job still left in queue');
197
198
    $rs = Koha::BackgroundJobs->search(
199
        {
200
            id => [ $job_recent_t2_fin->id ]
201
        }
202
    );
203
    is( $rs->count, 1, 'Recent finished job still left in queue');
204
205
    $schema->storage->txn_rollback;
206
207
};
208
- 

Return to bug 31969