Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / DB / BackgroundJob.pm
index f5c4b21..1fc8d99 100644 (file)
@@ -5,10 +5,15 @@ use strict;
 use DateTime::Event::Cron;
 use English qw(-no_match_vars);
 
+use Rose::DB::Object::Helpers qw(as_tree);
+
 use SL::DB::MetaSetup::BackgroundJob;
 use SL::DB::Manager::BackgroundJob;
 
 use SL::System::Process;
+use SL::YAML;
+
+__PACKAGE__->meta->initialize;
 
 __PACKAGE__->before_save('_before_save_set_next_run_at');
 
@@ -43,7 +48,7 @@ sub run {
     $history = SL::DB::BackgroundJobHistory
       ->new(package_name => $self->package_name,
             run_at       => $run_at,
-            status       => 'success',
+            status       => SL::DB::BackgroundJobHistory::SUCCESS(),
             result       => $result,
             data         => $self->data);
     $history->save;
@@ -56,7 +61,7 @@ sub run {
     $history = SL::DB::BackgroundJobHistory
       ->new(package_name => $self->package_name,
             run_at       => $run_at,
-            status       => 'failure',
+            status       => SL::DB::BackgroundJobHistory::FAILURE(),
             error_col    => $error,
             data         => $self->data);
     $history->save;
@@ -71,18 +76,22 @@ sub run {
 
 sub data_as_hash {
   my $self = shift;
+
+  $self->data(SL::YAML::Dump($_[0])) if @_;
+
   return {}                        if !$self->data;
   return $self->data               if ref($self->{data}) eq 'HASH';
-  return YAML::Load($self->{data}) if !ref($self->{data});
+  return SL::YAML::Load($self->{data}) if !ref($self->{data});
   return {};
 }
 
 sub set_data {
   my ($self, %data) = @_;
 
-  my $data = YAML::Load($self->data);
-  $data->{$_} = $data{$_} for keys %data;
-  $self->data(YAML::Dump($data));
+  $self->data(SL::YAML::Dump({
+    %{ $self->data_as_hash },
+    %data,
+  }));
 
   $self;
 }