[pve-devel] [PATCH common v3] add upid_wait method
Thomas Lamprecht
t.lamprecht at proxmox.com
Mon Jan 25 12:56:03 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 or timing
out the wait.
---
changes v3:
* do it in a sane way without double waiting
src/PVE/Tools.pm | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 64a57e9..58d943c 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -940,6 +940,26 @@ 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;
+
+ my $next_time = time + $sleep_intervall;
+
+ while (PVE::ProcFSTools::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);
+ }
+}
+
# useful functions to store comments in config files
sub encode_text {
my ($text) = @_;
--
2.1.4
More information about the pve-devel
mailing list