]> git.proxmox.com Git - pve-guest-common.git/blobdiff - PVE/ReplicationState.pm
PVE::ReplicationState::extract_vmid_tranfer_state - new helper
[pve-guest-common.git] / PVE / ReplicationState.pm
index ae4deaf0f500b99a4ca758f447fda9aae12e79be..6a743a3e5ec358ff69e1bed4d7d96860d71c6f08 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 use JSON;
 
 use PVE::INotify;
+use PVE::ProcFSTools;
 use PVE::Tools;
 use PVE::CalendarEvent;
 use PVE::Cluster;
@@ -61,6 +62,20 @@ sub extract_job_state {
     return $state;
 }
 
+sub extract_vmid_tranfer_state {
+    my ($stateobj, $vmid, $old_target, $new_target) = @_;
+
+    my $oldid = PVE::ReplicationConfig::Cluster->get_unique_target_id({ target => $old_target });
+    my $newid = PVE::ReplicationConfig::Cluster->get_unique_target_id({ target => $new_target });
+
+    if (defined(my $vmstate = $stateobj->{$vmid})) {
+       $vmstate->{$newid} = delete($vmstate->{$oldid}) if defined($vmstate->{$oldid});
+       return $vmstate;
+    }
+
+    return {};
+}
+
 sub read_job_state {
     my ($jobcfg) = @_;
 
@@ -114,6 +129,38 @@ sub write_vmid_job_states {
     PVE::GuestHelpers::guest_migration_lock($vmid, undef, $code);
 }
 
+sub record_job_start {
+    my ($jobcfg, $state, $start_time, $iteration) = @_;
+
+    $state->{pid} = $$;
+    $state->{ptime} = PVE::ProcFSTools::read_proc_starttime($state->{pid});
+    $state->{last_node} = PVE::INotify::nodename();
+    $state->{last_try} = $start_time;
+    $state->{last_iteration} = $iteration;
+    $state->{storeid_list} //= [];
+
+    write_job_state($jobcfg, $state);
+}
+
+sub record_job_end {
+    my ($jobcfg, $state, $start_time, $duration, $err) = @_;
+
+    $state->{duration} = $duration;
+    delete $state->{pid};
+    delete $state->{ptime};
+
+    if ($err) {
+       chomp $err;
+       $state->{fail_count}++;
+       $state->{error} = "$err";
+    } else {
+       $state->{last_sync} = $start_time;
+       $state->{fail_count} = 0;
+       delete $state->{error};
+    }
+    write_job_state($jobcfg,  $state);
+}
+
 sub replication_snapshot_name {
     my ($jobid, $last_sync) = @_;