BackgroundJob-Hilfsfunktionen
authorMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 29 Oct 2010 12:14:10 +0000 (14:14 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Wed, 12 Jan 2011 10:13:42 +0000 (11:13 +0100)
SL/DB/BackgroundJob.pm
SL/DB/Manager/BackgroundJob.pm [new file with mode: 0644]

index e6b099f..f4edf2d 100644 (file)
@@ -1,13 +1,18 @@
-# This file has been auto-generated only because it didn't exist.
-# Feel free to modify it at will; it will not be overwritten automatically.
-
 package SL::DB::BackgroundJob;
 
 use strict;
 
+use DateTime::Event::Cron;
+
 use SL::DB::MetaSetup::BackgroundJob;
+use SL::DB::Manager::BackgroundJob;
+
+sub update_next_run_at {
+  my $self = shift;
 
-# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
-__PACKAGE__->meta->make_manager_class;
+  my $cron = DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *');
+  $self->update_attributes(next_run_at => $cron->next->set_time_zone($::locale->get_local_time_zone));
+  return $self;
+}
 
 1;
diff --git a/SL/DB/Manager/BackgroundJob.pm b/SL/DB/Manager/BackgroundJob.pm
new file mode 100644 (file)
index 0000000..178f9d3
--- /dev/null
@@ -0,0 +1,22 @@
+package SL::DB::Manager::BackgroundJob;
+
+use strict;
+
+use SL::DB::Helpers::Manager;
+use base qw(SL::DB::Helpers::Manager);
+
+sub object_class { 'SL::DB::BackgroundJob' }
+
+__PACKAGE__->make_manager_methods;
+
+sub cleanup {
+  my $class = shift;
+  $class->delete_all(where => [ and => [ type => 'once', last_run_at => { lt => DateTime->now_local->subtract(days => '1') } ] ]);
+}
+
+sub get_all_need_to_run {
+  my $class = shift;
+  return $class->get_all(where => [ and => [ active => 1, next_run_at => { le => DateTime->now_local } ] ]);
+}
+
+1;