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