1 package SL::DB::BackgroundJob;
5 use DateTime::Event::Cron;
6 use English qw(-no_match_vars);
8 use SL::DB::MetaSetup::BackgroundJob;
9 use SL::DB::Manager::BackgroundJob;
11 use SL::DB::BackgroundJobHistory;
13 use SL::BackgroundJob::Test;
15 sub update_next_run_at {
18 my $cron = DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *');
19 $self->update_attributes(next_run_at => $cron->next(DateTime->now_local));
26 my $package = "SL::BackgroundJob::" . $self->package_name;
27 my $run_at = DateTime->now_local;
31 my $result = $package->new->run($self);
33 $history = SL::DB::BackgroundJobHistory
34 ->new(package_name => $self->package_name,
45 my $error = $EVAL_ERROR;
46 $history = SL::DB::BackgroundJobHistory
47 ->new(package_name => $self->package_name,
54 $::lxdebug->message(LXDebug->WARN(), "BackgroundJob ID " . $self->id . " execution error (first three lines): " . join("\n", (split(m/\n/, $error))[0..2]));
57 $self->assign_attributes(last_run_at => $run_at)->update_next_run_at;
64 return {} if !$self->data;
65 return $self->data if ref($self->{data}) eq 'HASH';
66 return YAML::Load($self->{data}) if !ref($self->{data});