kivitendo/SL/DB/BackgroundJob.pm @ 79b7fc43
295e585a | Moritz Bunkus | package SL::DB::BackgroundJob;
|
||
use strict;
|
||||
782fd788 | Moritz Bunkus | use DateTime::Event::Cron;
|
||
ee71ba33 | Moritz Bunkus | use English qw(-no_match_vars);
|
||
782fd788 | Moritz Bunkus | |||
2c597c2d | Sven Schöling | use Rose::DB::Object::Helpers qw(as_tree);
|
||
7c60c242 | Sven Schöling | use SL::DB::MetaSetup::BackgroundJob;
|
||
use SL::DB::Manager::BackgroundJob;
|
||||
ee71ba33 | Moritz Bunkus | |||
3be85e17 | Moritz Bunkus | use SL::System::Process;
|
||
ee71ba33 | Moritz Bunkus | |||
2d7e4203 | Sven Schöling | __PACKAGE__->meta->initialize;
|
||
572adb38 | Moritz Bunkus | __PACKAGE__->before_save('_before_save_set_next_run_at');
|
||
sub _before_save_set_next_run_at {
|
||||
my ($self) = @_;
|
||||
$self->update_next_run_at if !$self->next_run_at;
|
||||
return 1;
|
||||
}
|
||||
782fd788 | Moritz Bunkus | sub update_next_run_at {
|
||
my $self = shift;
|
||||
295e585a | Moritz Bunkus | |||
782fd788 | Moritz Bunkus | my $cron = DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *');
|
||
ee71ba33 | Moritz Bunkus | $self->update_attributes(next_run_at => $cron->next(DateTime->now_local));
|
||
782fd788 | Moritz Bunkus | return $self;
|
||
}
|
||||
295e585a | Moritz Bunkus | |||
ee71ba33 | Moritz Bunkus | sub run {
|
||
my $self = shift;
|
||||
my $package = "SL::BackgroundJob::" . $self->package_name;
|
||||
my $run_at = DateTime->now_local;
|
||||
my $history;
|
||||
7c60c242 | Sven Schöling | require SL::DB::BackgroundJobHistory;
|
||
2772592d | Moritz Bunkus | my $ok = eval {
|
||
074c3e6e | Moritz Bunkus | eval "require $package" or die $@;
|
||
ee71ba33 | Moritz Bunkus | my $result = $package->new->run($self);
|
||
$history = SL::DB::BackgroundJobHistory
|
||||
->new(package_name => $self->package_name,
|
||||
run_at => $run_at,
|
||||
8e115155 | Moritz Bunkus | status => SL::DB::BackgroundJobHistory::SUCCESS(),
|
||
ee71ba33 | Moritz Bunkus | result => $result,
|
||
data => $self->data);
|
||||
$history->save;
|
||||
1;
|
||||
};
|
||||
2772592d | Moritz Bunkus | if (!$ok) {
|
||
fafc4b3f | Moritz Bunkus | my $error = $EVAL_ERROR;
|
||
ee71ba33 | Moritz Bunkus | $history = SL::DB::BackgroundJobHistory
|
||
->new(package_name => $self->package_name,
|
||||
run_at => $run_at,
|
||||
8e115155 | Moritz Bunkus | status => SL::DB::BackgroundJobHistory::FAILURE(),
|
||
fafc4b3f | Moritz Bunkus | error_col => $error,
|
||
ee71ba33 | Moritz Bunkus | data => $self->data);
|
||
$history->save;
|
||||
fafc4b3f | Moritz Bunkus | |||
$::lxdebug->message(LXDebug->WARN(), "BackgroundJob ID " . $self->id . " execution error (first three lines): " . join("\n", (split(m/\n/, $error))[0..2]));
|
||||
ee71ba33 | Moritz Bunkus | }
|
||
$self->assign_attributes(last_run_at => $run_at)->update_next_run_at;
|
||||
return $history;
|
||||
}
|
||||
sub data_as_hash {
|
||||
my $self = shift;
|
||||
e4e46bf6 | Moritz Bunkus | |||
$self->data(YAML::Dump($_[0])) if @_;
|
||||
ee71ba33 | Moritz Bunkus | return {} if !$self->data;
|
||
return $self->data if ref($self->{data}) eq 'HASH';
|
||||
return YAML::Load($self->{data}) if !ref($self->{data});
|
||||
return {};
|
||||
}
|
||||
aba17eb1 | Sven Schöling | sub set_data {
|
||
my ($self, %data) = @_;
|
||||
9e4a9fec | Moritz Bunkus | $self->data(YAML::Dump({
|
||
%{ $self->data_as_hash },
|
||||
%data,
|
||||
}));
|
||||
98f37c10 | Sven Schöling | |||
$self;
|
||||
aba17eb1 | Sven Schöling | }
|
||
3be85e17 | Moritz Bunkus | sub validate {
|
||
my ($self) = @_;
|
||||
my @errors;
|
||||
push @errors, $::locale->text('The execution type is invalid.') if ($self->type || '') !~ m/^(?: once | interval )$/x;
|
||||
if ( (($self->package_name || '') !~ m/^ [A-Z][A-Za-z0-9]+ $/x)
|
||||
|| ! -f (SL::System::Process::exe_dir() . "/SL/BackgroundJob/" . $self->package_name . ".pm")) {
|
||||
push @errors, $::locale->text('The package name is invalid.');
|
||||
}
|
||||
eval {
|
||||
e5f571cf | Moritz Bunkus | DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *')->next(DateTime->now_local);
|
||
3be85e17 | Moritz Bunkus | 1;
|
||
} or push @errors, $::locale->text('The execution schedule is invalid.');
|
||||
return @errors;
|
||||
}
|
||||
295e585a | Moritz Bunkus | 1;
|