8b2e99aa8f1c965a4d45315e7a25d2e062fe0dc4
[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 use SL::System::TaskServer;
12
13 sub object_class { 'SL::DB::BackgroundJob' }
14
15 __PACKAGE__->make_manager_methods;
16
17 sub _sort_spec {
18   return ( default => [ 'next_run_at', 1 ],
19            columns => { SIMPLE => 'ALL' } );
20 }
21
22 sub cleanup {
23   my $class = shift;
24   $class->delete_all(where => [ and => [ type => 'once', last_run_at => { lt => DateTime->now_local->subtract(days => '1') } ] ]);
25 }
26
27 sub get_all_need_to_run {
28   my $class         = shift;
29
30   my $now           = DateTime->now_local;
31   my @interval_args = (and => [ type        => 'interval',
32                                 active      => 1,
33                                 next_run_at => { le => $now } ]);
34   my @once_args     = (and => [ type        => 'once',
35                                 active      => 1,
36                                 last_run_at => undef,
37                                 or          => [ cron_spec   => undef,
38                                                  cron_spec   => '',
39                                                  next_run_at => undef,
40                                                  next_run_at => { le => $now } ] ]);
41   my @node_filter;
42
43   my $node_id = SL::System::TaskServer->node_id;
44   if ($::lx_office_conf{task_server}->{only_run_tasks_for_this_node}) {
45     @node_filter = (node_id => $node_id);
46   } else {
47     @node_filter = (
48       or => [
49         node_id => undef,
50         node_id => '',
51         node_id => $node_id,
52       ]);
53   }
54
55   return $class->get_all(query => [ or => [ @interval_args, @once_args ], @node_filter ]);
56 }
57
58 1;