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

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Jan 22 16:01:32 CET 2016


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

waitfunc gets called every wait cycle, can be used for outputting/
logging something.

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

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/Tools.pm | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 64a57e9..aaaeedb 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -940,6 +940,22 @@ 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';
+
+	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