epic-ts
[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::Paginated;
9 use SL::DB::Helper::Sorted;
10
11 sub object_class { 'SL::DB::BackgroundJob' }
12
13 __PACKAGE__->make_manager_methods;
14
15 sub _sort_spec {
16   return ( default => [ 'next_run_at', 1 ],
17            columns => { SIMPLE => 'ALL' } );
18 }
19
20 sub cleanup {
21   my $class = shift;
22   $class->delete_all(where => [ and => [ type => 'once', last_run_at => { lt => DateTime->now_local->subtract(days => '1') } ] ]);
23 }
24
25 sub get_all_need_to_run {
26   my $class         = shift;
27
28   my $now           = DateTime->now_local;
29   my @interval_args = (and => [ type        => 'interval',
30                                 active      => 1,
31                                 next_run_at => { le => $now } ]);
32   my @once_args     = (and => [ type        => 'once',
33                                 active      => 1,
34                                 last_run_at => undef,
35                                 or          => [ cron_spec   => undef,
36                                                  cron_spec   => '',
37                                                  next_run_at => undef,
38                                                  next_run_at => { le => $now } ] ]);
39
40   return $class->get_all(query => [ or => [ @interval_args, @once_args ] ]);
41 }
42
43 1;