]> wagnertech.de Git - mfinanz.git/blobdiff - SL/BackgroundJob/CleanBackgroundJobHistory.pm
Hintergrundjobs: DB-Model und allgemeine Modelimplementation mit ersten Test-Jobs
[mfinanz.git] / SL / BackgroundJob / CleanBackgroundJobHistory.pm
diff --git a/SL/BackgroundJob/CleanBackgroundJobHistory.pm b/SL/BackgroundJob/CleanBackgroundJobHistory.pm
new file mode 100644 (file)
index 0000000..117a4d4
--- /dev/null
@@ -0,0 +1,29 @@
+package SL::BackgroundJob::CleanBackgroundJobHistory;
+
+use parent qw(SL::BackgroundJob::Base);
+
+use SL::DB::BackgroundJobHistory;
+
+sub create_job {
+  $_[0]->create_standard_job('0 3 * * *'); # daily at 3:00 am
+}
+
+sub run {
+  my $self    = shift;
+  my $db_obj  = shift;
+
+  my $options = $db_obj->data_as_hash;
+  $options->{retention_success} ||= 14;
+  $options->{retention_failure} ||= 3 * 30;
+
+  my $today = DateTime->today_local;
+
+  for my $status (qw(success failure)) {
+    SL::DB::Manager::BackgroundJobHistory->delete_all(where =>  [ status => $status,
+                                                                  run_at => { lt => $today->clone->subtract(days => $options->{"retention_${status}"}) } ]);
+  }
+
+  return 1;
+}
+
+1;