[pve-devel] [PATCH pve-manager 04/18] PVE/Replication.pm: save pid/ptime to running job state

Dietmar Maurer dietmar at proxmox.com
Tue May 23 09:08:43 CEST 2017


So that we can check which job is running.

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 PVE/API2/Replication.pm |  6 ++++++
 PVE/CLI/pvesr.pm        |  4 +++-
 PVE/Replication.pm      | 17 +++++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Replication.pm b/PVE/API2/Replication.pm
index 3c631e44..c400a93c 100644
--- a/PVE/API2/Replication.pm
+++ b/PVE/API2/Replication.pm
@@ -5,6 +5,7 @@ use strict;
 
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RPCEnvironment;
+use PVE::ProcFSTools;
 use PVE::ReplicationConfig;
 use PVE::Replication;
 
@@ -84,6 +85,11 @@ __PACKAGE__->register_method ({
 	    foreach my $k (qw(last_sync fail_count error duration)) {
 		$d->{$k} = $state->{$k} if defined($state->{$k});
 	    }
+	    if ($state->{pid} && $state->{ptime}) {
+		if (PVE::ProcFSTools::check_process_running($state->{pid}, $state->{ptime})) {
+		    $d->{pid} = $state->{pid};
+		}
+	    }
 	    push @$res, $d;
 	}
 
diff --git a/PVE/CLI/pvesr.pm b/PVE/CLI/pvesr.pm
index 1dc24635..38116f7b 100644
--- a/PVE/CLI/pvesr.pm
+++ b/PVE/CLI/pvesr.pm
@@ -125,9 +125,11 @@ my $print_job_status = sub {
 	my $timestr = $job->{last_sync} ?
 	    strftime("%Y-%m-%d_%H:%M:%S", localtime($job->{last_sync})) : '-';
 
+	my $state = $job->{pid} ? "SYNCING" : $job->{error} // 'OK';
+
 	printf($format, $job->{id}, $job->{guest}, $tid,
 	       $timestr, $job->{duration} // '-',
-	       $job->{fail_count}, , $job->{error} // 'OK');
+	       $job->{fail_count}, $state);
     }
 };
 
diff --git a/PVE/Replication.pm b/PVE/Replication.pm
index 6ddc0ce2..ff4bbeb4 100644
--- a/PVE/Replication.pm
+++ b/PVE/Replication.pm
@@ -7,6 +7,7 @@ use JSON;
 use Time::HiRes qw(gettimeofday tv_interval);
 
 use PVE::INotify;
+use PVE::ProcFSTools;
 use PVE::Tools;
 use PVE::Cluster;
 use PVE::QemuConfig;
@@ -162,10 +163,26 @@ my $run_replication = sub {
 
     my $t0 = [gettimeofday];
 
+    # cleanup stale pid/ptime state
+    foreach my $vmid (keys %$stateobj) {
+	foreach my $tid (keys %{$stateobj->{$vmid}}) {
+	    my $state = $stateobj->{$vmid}->{$tid};
+	    delete $state->{pid};
+	    delete $state->{ptime};
+	}
+    }
+
+    $state->{pid} = $$;
+    $state->{ptime} = PVE::ProcFSTools::read_proc_starttime($state->{pid});
+
+    $update_job_state->($stateobj, $jobcfg,  $state);
+
     eval { replicate($jobcfg, $start_time); };
     my $err = $@;
 
     $state->{duration} = tv_interval($t0);
+    delete $state->{pid};
+    delete $state->{ptime};
 
     if ($err) {
 	$state->{fail_count}++;
-- 
2.11.0




More information about the pve-devel mailing list