e8ae55bac9a394448ff16695c250b5b6716e72a5
[kivitendo-erp.git] / SL / DB / BackgroundJob.pm
1 package SL::DB::BackgroundJob;
2
3 use strict;
4
5 use DateTime::Event::Cron;
6 use English qw(-no_match_vars);
7
8 use SL::DB::MetaSetup::BackgroundJob;
9 use SL::DB::Manager::BackgroundJob;
10
11 use SL::System::Process;
12
13 __PACKAGE__->meta->initialize;
14
15 __PACKAGE__->before_save('_before_save_set_next_run_at');
16
17 sub _before_save_set_next_run_at {
18   my ($self) = @_;
19
20   $self->update_next_run_at if !$self->next_run_at;
21   return 1;
22 }
23
24 sub update_next_run_at {
25   my $self = shift;
26
27   my $cron = DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *');
28   $self->update_attributes(next_run_at => $cron->next(DateTime->now_local));
29   return $self;
30 }
31
32 sub run {
33   my $self = shift;
34
35   my $package = "SL::BackgroundJob::" . $self->package_name;
36   my $run_at  = DateTime->now_local;
37   my $history;
38
39   require SL::DB::BackgroundJobHistory;
40
41   my $ok = eval {
42     eval "require $package" or die $@;
43     my $result = $package->new->run($self);
44
45     $history = SL::DB::BackgroundJobHistory
46       ->new(package_name => $self->package_name,
47             run_at       => $run_at,
48             status       => 'success',
49             result       => $result,
50             data         => $self->data);
51     $history->save;
52
53     1;
54   };
55
56   if (!$ok) {
57     my $error = $EVAL_ERROR;
58     $history = SL::DB::BackgroundJobHistory
59       ->new(package_name => $self->package_name,
60             run_at       => $run_at,
61             status       => 'failure',
62             error_col    => $error,
63             data         => $self->data);
64     $history->save;
65
66     $::lxdebug->message(LXDebug->WARN(), "BackgroundJob ID " . $self->id . " execution error (first three lines): " . join("\n", (split(m/\n/, $error))[0..2]));
67   }
68
69   $self->assign_attributes(last_run_at => $run_at)->update_next_run_at;
70
71   return $history;
72 }
73
74 sub data_as_hash {
75   my $self = shift;
76   return {}                        if !$self->data;
77   return $self->data               if ref($self->{data}) eq 'HASH';
78   return YAML::Load($self->{data}) if !ref($self->{data});
79   return {};
80 }
81
82 sub set_data {
83   my ($self, %data) = @_;
84
85   my $data = YAML::Load($self->data);
86   $data->{$_} = $data{$_} for keys %data;
87   $self->data(YAML::Dump($data));
88
89   $self;
90 }
91
92 sub validate {
93   my ($self) = @_;
94
95   my @errors;
96
97   push @errors, $::locale->text('The execution type is invalid.') if ($self->type         || '') !~ m/^(?: once | interval )$/x;
98
99   if (   (($self->package_name || '') !~ m/^ [A-Z][A-Za-z0-9]+ $/x)
100       || ! -f (SL::System::Process::exe_dir() . "/SL/BackgroundJob/" . $self->package_name . ".pm")) {
101     push @errors, $::locale->text('The package name is invalid.');
102   }
103
104   eval {
105     DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *')->next(DateTime->now_local);
106     1;
107   } or push @errors, $::locale->text('The execution schedule is invalid.');
108
109   return @errors;
110 }
111
112 1;