From c1073fdc483bad57e0d71999f83b6797f294cf47 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Wed, 27 Jan 2016 13:15:05 +0100 Subject: [PATCH] add upid_wait method 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 --- src/PVE/ProcFSTools.pm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm index 99f767a..516c0e8 100644 --- a/src/PVE/ProcFSTools.pm +++ b/src/PVE/ProcFSTools.pm @@ -352,4 +352,24 @@ sub read_proc_net_ipv6_route { 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; -- 2.39.2