[pve-devel] [PATCH pve-guest-common 3/5] PVE::ReplicationState - new helpers record_job_start/record_job_end

Dietmar Maurer dietmar at proxmox.com
Tue Jun 20 10:50:55 CEST 2017


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 PVE/Replication.pm      | 24 ++++--------------------
 PVE/ReplicationState.pm | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/PVE/Replication.pm b/PVE/Replication.pm
index 1762f2d..7686222 100644
--- a/PVE/Replication.pm
+++ b/PVE/Replication.pm
@@ -309,16 +309,9 @@ my $run_replication_nolock = sub {
     eval {
 	my $state = PVE::ReplicationState::read_job_state($jobcfg);
 
-	my $t0 = [gettimeofday];
-
-	$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} //= [];
+	PVE::ReplicationState::record_job_start($jobcfg, $state, $start_time, $iteration);
 
-	PVE::ReplicationState::write_job_state($jobcfg, $state);
+	my $t0 = [gettimeofday];
 
 	mkdir $PVE::ReplicationState::replicate_logdir;
 	my $logfile = PVE::ReplicationState::job_logfile_name($jobid);
@@ -340,24 +333,15 @@ my $run_replication_nolock = sub {
 	};
 	my $err = $@;
 
-	$state->{duration} = tv_interval($t0);
-	delete $state->{pid};
-	delete $state->{ptime};
-
 	if ($err) {
 	    chomp $err;
-	    $state->{fail_count}++;
-	    $state->{error} = "$err";
-	    PVE::ReplicationState::write_job_state($jobcfg,  $state);
 	    $logfunc_wrapper->("end replication job with error: $err");
 	} else {
 	    $logfunc_wrapper->("end replication job");
-	    $state->{last_sync} = $start_time;
-	    $state->{fail_count} = 0;
-	    delete $state->{error};
-	    PVE::ReplicationState::write_job_state($jobcfg,  $state);
 	}
 
+	PVE::ReplicationState::record_job_end($jobcfg, $state, $start_time, tv_interval($t0), $err);
+
 	close($logfd);
     };
     if (my $err = $@) {
diff --git a/PVE/ReplicationState.pm b/PVE/ReplicationState.pm
index ae4deaf..3ccc26c 100644
--- a/PVE/ReplicationState.pm
+++ b/PVE/ReplicationState.pm
@@ -5,6 +5,7 @@ use strict;
 use JSON;
 
 use PVE::INotify;
+use PVE::ProcFSTools;
 use PVE::Tools;
 use PVE::CalendarEvent;
 use PVE::Cluster;
@@ -114,6 +115,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) = @_;
 
-- 
2.11.0




More information about the pve-devel mailing list