set_data Methode
[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 require SL::DB::MetaSetup::BackgroundJob;
9 require SL::DB::Manager::BackgroundJob;
10
11 require SL::DB::BackgroundJobHistory;
12
13 use SL::System::Process;
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   my $ok = eval {
40     eval "require $package" or die $@;
41     my $result = $package->new->run($self);
42
43     $history = SL::DB::BackgroundJobHistory
44       ->new(package_name => $self->package_name,
45             run_at       => $run_at,
46             status       => 'success',
47             result       => $result,
48             data         => $self->data);
49     $history->save;
50
51     1;
52   };
53
54   if (!$ok) {
55     my $error = $EVAL_ERROR;
56     $history = SL::DB::BackgroundJobHistory
57       ->new(package_name => $self->package_name,
58             run_at       => $run_at,
59             status       => 'failure',
60             error_col    => $error,
61             data         => $self->data);
62     $history->save;
63
64     $::lxdebug->message(LXDebug->WARN(), "BackgroundJob ID " . $self->id . " execution error (first three lines): " . join("\n", (split(m/\n/, $error))[0..2]));
65   }
66
67   $self->assign_attributes(last_run_at => $run_at)->update_next_run_at;
68
69   return $history;
70 }
71
72 sub data_as_hash {
73   my $self = shift;
74   return {}                        if !$self->data;
75   return $self->data               if ref($self->{data}) eq 'HASH';
76   return YAML::Load($self->{data}) if !ref($self->{data});
77   return {};
78 }
79
80 sub set_data {
81   my ($self, %data) = @_;
82
83   my $data = YAML::Load($self->data);
84   $data->{$_} = $data{$_} for keys %data;
85   $self->data(YAML::Dump($data));
86 }
87
88 sub validate {
89   my ($self) = @_;
90
91   my @errors;
92
93   push @errors, $::locale->text('The execution type is invalid.') if ($self->type         || '') !~ m/^(?: once | interval )$/x;
94
95   if (   (($self->package_name || '') !~ m/^ [A-Z][A-Za-z0-9]+ $/x)
96       || ! -f (SL::System::Process::exe_dir() . "/SL/BackgroundJob/" . $self->package_name . ".pm")) {
97     push @errors, $::locale->text('The package name is invalid.');
98   }
99
100   eval {
101     DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *')->next(DateTime->now_local);
102     1;
103   } or push @errors, $::locale->text('The execution schedule is invalid.');
104
105   return @errors;
106 }
107
108 1;