Dokumentation
[kivitendo-erp.git] / SL / BackgroundJob / CleanBackgroundJobHistory.pm
1 package SL::BackgroundJob::CleanBackgroundJobHistory;
2
3 use parent qw(SL::BackgroundJob::Base);
4
5 use SL::DB::BackgroundJobHistory;
6
7 sub create_job {
8   $_[0]->create_standard_job('0 3 * * *'); # daily at 3:00 am
9 }
10
11 sub run {
12   my $self    = shift;
13   my $db_obj  = shift;
14
15   my $options = $db_obj->data_as_hash;
16   $options->{retention_success} ||= 14;
17   $options->{retention_failure} ||= 3 * 30;
18
19   my $today = DateTime->today_local;
20
21   for my $status (qw(success failure)) {
22     SL::DB::Manager::BackgroundJobHistory->delete_all(where =>  [ status => $status,
23                                                                   run_at => { lt => $today->clone->subtract(days => $options->{"retention_${status}"}) } ]);
24   }
25
26   return 1;
27 }
28
29 1;
30
31 __END__
32
33 =encoding utf8
34
35 =head1 NAME
36
37 SL::BackgroundJob::CleanBackgroundJobHistory - Background job for
38 cleaning the history table of all executed jobs
39
40 =head1 SYNOPSIS
41
42 This background job deletes old entries from the table
43 C<background_job_histories>. Each time a job is run an entry is
44 created in that table.
45
46 The associated C<SL::DB::BackgroundJob> instance's C<data> may be a
47 hash containing the retention periods for successful and failed
48 jobs. Both are the number of days a history entry is to be kept.  C<<
49 $data->{retention_success} >> defaults to 14.  C<<
50 $data->{retention_failure} >> defaults to 90.
51
52 The job is supposed to run once a day.
53
54 =head1 BUGS
55
56 Nothing here yet.
57
58 =head1 AUTHOR
59
60 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
61
62 =cut