1 package SL::DB::BackgroundJob;
 
   5 use DateTime::Event::Cron;
 
   6 use English qw(-no_match_vars);
 
   8 use Rose::DB::Object::Helpers qw(as_tree);
 
  10 use SL::DB::MetaSetup::BackgroundJob;
 
  11 use SL::DB::Manager::BackgroundJob;
 
  13 use SL::System::Process;
 
  16 __PACKAGE__->meta->initialize;
 
  18 __PACKAGE__->before_save('_before_save_set_next_run_at');
 
  20 sub _before_save_set_next_run_at {
 
  23   $self->update_next_run_at if !$self->next_run_at;
 
  27 sub update_next_run_at {
 
  30   my $cron = DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *');
 
  31   $self->update_attributes(next_run_at => $cron->next(DateTime->now_local));
 
  38   my $package = "SL::BackgroundJob::" . $self->package_name;
 
  39   my $run_at  = DateTime->now_local;
 
  42   require SL::DB::BackgroundJobHistory;
 
  45     eval "require $package" or die $@;
 
  46     my $result = $package->new->run($self);
 
  48     $history = SL::DB::BackgroundJobHistory
 
  49       ->new(package_name => $self->package_name,
 
  51             status       => SL::DB::BackgroundJobHistory::SUCCESS(),
 
  60     my $error = $EVAL_ERROR;
 
  61     $history = SL::DB::BackgroundJobHistory
 
  62       ->new(package_name => $self->package_name,
 
  64             status       => SL::DB::BackgroundJobHistory::FAILURE(),
 
  69     $::lxdebug->message(LXDebug->WARN(), "BackgroundJob ID " . $self->id . " execution error (first three lines): " . join("\n", (split(m/\n/, $error))[0..2]));
 
  72   $self->assign_attributes(last_run_at => $run_at)->update_next_run_at;
 
  80   $self->data(SL::YAML::Dump($_[0])) if @_;
 
  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});
 
  89   my ($self, %data) = @_;
 
  91   $self->data(SL::YAML::Dump({
 
  92     %{ $self->data_as_hash },
 
 104   push @errors, $::locale->text('The execution type is invalid.') if ($self->type         || '') !~ m/^(?: once | interval )$/x;
 
 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.');
 
 112     DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *')->next(DateTime->now_local);
 
 114   } or push @errors, $::locale->text('The execution schedule is invalid.');