BackgroundJob-Manager sortierbar machen
[kivitendo-erp.git] / SL / DB / Manager / BackgroundJob.pm
1 package SL::DB::Manager::BackgroundJob;
2
3 use strict;
4
5 use SL::DB::Helper::Manager;
6 use base qw(SL::DB::Helper::Manager);
7
8 use SL::DB::Helper::Sorted;
9
10 sub object_class { 'SL::DB::BackgroundJob' }
11
12 __PACKAGE__->make_manager_methods;
13
14 sub _sort_spec {
15   return ( default => [ 'next_run_at', 1 ],
16            columns => { SIMPLE => 'ALL' } );
17 }
18
19 sub cleanup {
20   my $class = shift;
21   $class->delete_all(where => [ and => [ type => 'once', last_run_at => { lt => DateTime->now_local->subtract(days => '1') } ] ]);
22 }
23
24 sub get_all_need_to_run {
25   my $class         = shift;
26
27   my $now           = DateTime->now_local;
28   my @interval_args = (and => [ type        => 'interval',
29                                 active      => 1,
30                                 next_run_at => { le => $now } ]);
31   my @once_args     = (and => [ type        => 'once',
32                                 active      => 1,
33                                 last_run_at => undef,
34                                 or          => [ cron_spec   => undef,
35                                                  cron_spec   => '',
36                                                  next_run_at => undef,
37                                                  next_run_at => { le => $now } ] ]);
38
39   return $class->get_all(query => [ or => [ @interval_args, @once_args ] ]);
40 }
41
42 1;