[pve-devel] [PATCH common v2] add upid_wait method

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Jan 25 12:08:50 CET 2016


Waits for a process identified by a UPID to end by busy waiting
and is intended for long running workers.

waitfunc gets called every wait cycle after min $sleep_interval
seconds and can be used for outputting/logging something.

$sleep_intervall denotes how long the wait periods between two
checks are.
---

Fixed in v2:
 * always wait for at least 'sleep_intervall' seconds

Regarding interrupts we could wait almost for 2*sleep_interval
seconds between two check_process_running cycles, for the cases
where I'd use this functions (e.g. long running workers) this is
fine (imo) as:
 1) interrupts won't happen that often there in the first place
 2) long running means that we probably do not care if we wait
    half a second longer with a low probability

 src/PVE/Tools.pm | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 64a57e9..57b1d44 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -940,6 +940,24 @@ sub upid_read_status {
     return "unable to read tail (got $br bytes)";
 }
 
+sub upid_wait {
+    my ($upid, $waitfunc, $sleep_intervall) = @_;
+
+    my $task = upid_decode($upid);
+
+    $sleep_intervall = $sleep_intervall ? $sleep_intervall : 1;
+
+    while (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart})) {
+
+	&$waitfunc($task) if $waitfunc && ref($waitfunc) eq 'CODE';
+
+	my $next_time = time + $sleep_intervall;
+	while (time < $next_time) {
+	    CORE::sleep($sleep_intervall);
+	}
+    }
+}
+
 # useful functions to store comments in config files 
 sub encode_text {
     my ($text) = @_;
-- 
2.1.4





More information about the pve-devel mailing list