]> wagnertech.de Git - mfinanz.git/blob - SL/DB/BackgroundJob.pm
restart apache2 in postinst
[mfinanz.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             description  => $self->description,
51             run_at       => $run_at,
52             status       => SL::DB::BackgroundJobHistory::SUCCESS(),
53             result       => $result,
54             data         => $self->data);
55     $history->save;
56
57     1;
58   };
59
60   if (!$ok) {
61     my $error = $EVAL_ERROR;
62     $history = SL::DB::BackgroundJobHistory
63       ->new(package_name => $self->package_name,
64             run_at       => $run_at,
65             status       => SL::DB::BackgroundJobHistory::FAILURE(),
66             error_col    => '' . $error,
67             data         => $self->data);
68     $history->save;
69
70     $::lxdebug->message(LXDebug->WARN(), "BackgroundJob ID " . $self->id . " execution error (first three lines): " . join("\n", (split(m/\n/, $error))[0..2]));
71   }
72
73   $self->assign_attributes(last_run_at => $run_at)->update_next_run_at;
74
75   return $history;
76 }
77
78 sub data_as_hash {
79   my $self = shift;
80
81   $self->data(SL::YAML::Dump($_[0])) if @_;
82
83   return {}                        if !$self->data;
84   return $self->data               if ref($self->{data}) eq 'HASH';
85   return SL::YAML::Load($self->{data}) if !ref($self->{data});
86   return {};
87 }
88
89 sub set_data {
90   my ($self, %data) = @_;
91
92   $self->data(SL::YAML::Dump({
93     %{ $self->data_as_hash },
94     %data,
95   }));
96
97   $self;
98 }
99
100 sub validate {
101   my ($self) = @_;
102
103   my @errors;
104
105   push @errors, $::locale->text('The execution type is invalid.') if ($self->type         || '') !~ m/^(?: once | interval )$/x;
106
107   if (   (($self->package_name || '') !~ m/^ [A-Z][A-Za-z0-9]+ $/x)
108       || ! -f (SL::System::Process::exe_dir() . "/SL/BackgroundJob/" . $self->package_name . ".pm")) {
109     push @errors, $::locale->text('The package name is invalid.');
110   }
111
112   eval {
113     DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *')->next(DateTime->now_local);
114     1;
115   } or push @errors, $::locale->text('The execution schedule is invalid.');
116
117   return @errors;
118 }
119
120 1;