Hintergrundjobs: DB-Model und allgemeine Modelimplementation mit ersten Test-Jobs
[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;