[pve-devel] [PATCH manager 1/2] task index: optionally include active tasks

Fabian Grünbichler f.gruenbichler at proxmox.com
Wed Dec 19 09:37:20 CET 2018


otherwise there is no way to find out about those tasks over the API if
the UPID was not recorded when the initial API call happened.

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---

Notes:
    this is a nice addition for external monitoring/dashboard/UI
    applications, e.g. see #1997

 PVE/API2/Tasks.pm | 47 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 10 deletions(-)

diff --git a/PVE/API2/Tasks.pm b/PVE/API2/Tasks.pm
index a5acb118..95a13c56 100644
--- a/PVE/API2/Tasks.pm
+++ b/PVE/API2/Tasks.pm
@@ -58,6 +58,12 @@ __PACKAGE__->register_method({
 		default => 0,
 		optional => 1,
 	    },
+	    active => {
+		type => 'boolean',
+		default => 0,
+		optional => 1,
+		description => 'Include currently active tasks.',
+	    },
 	},
     },
     returns => {
@@ -94,36 +100,57 @@ __PACKAGE__->register_method({
 	my $limit = $param->{limit} // 50;
 	my $userfilter = $param->{userfilter};
 	my $errors = $param->{errors} // 0;
+	my $include_active = $param->{active} // 0;
 
 	my $count = 0;
 	my $line;
 
 	my $auditor = $rpcenv->check($user, "/nodes/$node", [ 'Sys.Audit' ], 1);
 
+	my $filter_task = sub {
+	    my $task = shift;
+
+	    return 1 if $userfilter && $task->{user} !~ m/\Q$userfilter\E/i;
+	    return 1 if !($auditor || $user eq $task->{user});
+
+	    return 1 if $errors && $task->{status} && $task->{status} eq 'OK';
+	    return 1 if $param->{vmid} && (!$task->{id} || $task->{id} ne $param->{vmid});
+
+	    return 1 if $count++ < $start;
+	    return 1 if $limit <= 0;
+
+	    return 0;
+	};
+
 	my $parse_line = sub {
 	    if ($line =~ m/^(\S+)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
 		my $upid = $1;
 		my $endtime = $3;
 		my $status = $5;
 		if ((my $task = PVE::Tools::upid_decode($upid, 1))) {
-		    return if $userfilter && $task->{user} !~ m/\Q$userfilter\E/i;
-		    return if !($auditor || $user eq $task->{user});
-
-		    return if $errors && $status && $status eq 'OK';
-
-		    return if $param->{vmid} && (!$task->{id} || $task->{id} ne $param->{vmid});
-
-		    return if $count++ < $start;
-		    return if $limit <= 0;
 
 		    $task->{upid} = $upid;
 		    $task->{endtime} = hex($endtime) if $endtime;
 		    $task->{status} = $status if $status;
+
+		    if (!$filter_task->($task)) {
+			push @$res, $task;
+			$limit--;
+		    }
+		}
+	    }
+	};
+
+	if ($include_active) {
+	    my $recent_tasks = PVE::INotify::read_file('active');
+	    for my $task (@$recent_tasks) {
+		next if delete $task->{saved}; # already in index(.1)
+		if (!$filter_task->($task)) {
 		    push @$res, $task;
 		    $limit--;
 		}
 	    }
-	};
+	}
 
 	if (my $bw = File::ReadBackwards->new($filename)) {
 	    while (defined ($line = $bw->readline)) {
-- 
2.19.2





More information about the pve-devel mailing list