]> git.proxmox.com Git - pve-common.git/commitdiff
add upid_wait method
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 27 Jan 2016 12:15:05 +0000 (13:15 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 27 Jan 2016 15:54:50 +0000 (16:54 +0100)
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 or timing
out the wait.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/ProcFSTools.pm

index 99f767a172b7e1f0bc687d44c043a8123fd0d347..516c0e8b939791a64771df41c36df362e65c7423 100644 (file)
@@ -352,4 +352,24 @@ sub read_proc_net_ipv6_route {
     return $res;
 }
 
     return $res;
 }
 
+sub upid_wait {
+    my ($upid, $waitfunc, $sleep_intervall) = @_;
+
+    my $task = PVE::Tools::upid_decode($upid);
+
+    $sleep_intervall = $sleep_intervall ? $sleep_intervall : 1;
+
+    my $next_time = time + $sleep_intervall;
+
+    while (check_process_running($task->{pid}, $task->{pstart})) {
+
+       if (time >= $next_time && $waitfunc && ref($waitfunc) eq 'CODE'){
+           &$waitfunc($task);
+           $next_time = time + $sleep_intervall;
+       }
+
+       CORE::sleep(1);
+    }
+}
+
 1;
 1;