[pve-devel] r5726 - pve-access-control/trunk/PVE

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Mar 18 11:20:36 CET 2011


Author: dietmar
Date: 2011-03-18 11:20:36 +0100 (Fri, 18 Mar 2011)
New Revision: 5726

Modified:
   pve-access-control/trunk/PVE/RPCEnvironment.pm
Log:
archive finished tasks


Modified: pve-access-control/trunk/PVE/RPCEnvironment.pm
===================================================================
--- pve-access-control/trunk/PVE/RPCEnvironment.pm	2011-03-18 10:19:09 UTC (rev 5725)
+++ pve-access-control/trunk/PVE/RPCEnvironment.pm	2011-03-18 10:20:36 UTC (rev 5726)
@@ -38,7 +38,7 @@
 	$pri = 'err';
     }
     PVE::Cluster::log_msg($pri, $user, "end task $upid $msg");
-    PVE::Cluster::broadcast_tasklist(active_workers());
+    PVE::Cluster::broadcast_tasklist(active_workers($upid));
 };
 
 my $worker_reaper = sub {
@@ -302,52 +302,86 @@
 
 	my $tasklist = PVE::INotify::read_file('active');
 
-	if ($new_upid) {
-	    my $task =  PVE::Tools::upid_decode($new_upid);
-	    $task->{upid} = $new_upid;
-	    push @$tasklist, $task;
-	}
-
 	my @ta;
 	my $tlist = [];
 	my $thash = {}; # only list task once
 
-	foreach my $task (@$tasklist) {
-	    my $upid = $task->{upid};
-	    next if $thash->{$upid};
-	    $thash->{$upid} = 1;
+	my $check_task = sub {
+	    my ($task) = @_;
 
-	    if (-d "/proc/$task->{pid}") {
-		my $pstart = PVE::ProcFSTools::read_proc_starttime($task->{pid});
-		if ($pstart && ($pstart == $task->{pstart})) {
-		    push @$tlist, $task;
-		} else {
-		    delete $task->{pid};
-		    push @ta, $task;
-		}
+	    my $pstart = PVE::ProcFSTools::read_proc_starttime($task->{pid});
+	    if ($pstart && ($pstart == $task->{pstart})) {
+		print "RUNNING $task->{upid}\n";
+		push @$tlist, $task;
 	    } else {
 		delete $task->{pid};
 		push @ta, $task;
 	    }
 	    delete $task->{pstart};
+	};
+
+	foreach my $task (@$tasklist) {
+	    my $upid = $task->{upid};
+	    next if $thash->{$upid};
+	    $thash->{$upid} = $task;
+	    &$check_task($task);
 	}
 
+	if ($new_upid) {
+	    my $task = $thash->{$new_upid};
+	    if (!$task) {
+		$task = PVE::Tools::upid_decode($new_upid);
+		$task->{upid} = $new_upid;
+		&$check_task($task);
+	    }
+	}
+
 	@ta = sort { $b->{starttime} cmp $a->{starttime} } @ta;
 
+	my $save = defined($new_upid);
+
+	foreach my $task (@ta) {
+	    next if $task->{endtime};
+	    $task->{endtime} = time();
+	    $task->{status} = PVE::Tools::upid_read_status($task->{upid});
+	    $save = 1;
+	}
+
 	# we try to reduce the amount of data
 	# only list running tasks and task not older than 5 minutes
 	# try to limit to 25 tasks
 	my $ctime = time();
 	my $max = 25 - scalar(@$tlist);
-	for (my $i = 0; $i < $max; $i++) {
-	    my $task = $ta[$i];
-	    last if !$task;
-	    next if ($ctime - $task->{starttime}) > 60*5;
-	    push @$tlist, $task;
+
+	my $archive = '';
+	foreach my $task (@ta) {
+	    if (($max <= 0) || (($ctime - $task->{starttime}) > 60*5)) {
+		$archive .= sprintf("$task->{upid} %08X $task->{status}\n", 
+				    $task->{endtime});
+		$save = 1;
+	    } else {
+		push @$tlist, $task;
+		$max--;
+	    }
 	}
-	
-	PVE::INotify::write_file('active', $tlist) if $new_upid;
 
+	if ($archive) {
+	    eval {
+		my $filename = "/var/log/pve/tasks/index";
+		my $fh = IO::File->new($filename, '>>', 0644) ||
+		    die "unable to open file '$filename' - $!\n";
+		PVE::Tools::safe_print($filename, $fh, $archive);
+		close($fh) ||
+		    die "unable to close file '$filename' - $!\n";
+	    };
+	    my $err = $@;
+	    if ($err) {
+		syslog('err', $err);
+		$save = 0; # don't write changes
+	    }
+	}
+	PVE::INotify::write_file('active', $tlist) if $save;
+
 	return $tlist;
     };
 
@@ -377,9 +411,6 @@
 	local $SIG{PIPE} = 
 	local $SIG{TERM} = 'IGNORE';
 
-    my $wwwid = getpwnam('www-data') ||
-	die "getpwnam failed";
-
     my $starttime = time ();
 
     my @psync = POSIX::pipe();
@@ -434,7 +465,7 @@
 	    die "unable to redirect STDIN - $!" 
 		if !open(STDIN, "</dev/null");
 
-	    $outfh = PVE::Tools::upid_open($upid, $wwwid) if !$sync;
+	    $outfh = PVE::Tools::upid_open($upid) if !$sync;
 
 	    # redirect STDOUT
 	    $fd = fileno(STDOUT);
@@ -520,7 +551,7 @@
 	}
 
 	if ($sync) {
-	    $outfh = PVE::Tools::upid_open($upid, $wwwid);
+	    $outfh = PVE::Tools::upid_open($upid);
 	}
     };
     my $err = $@;
@@ -593,6 +624,8 @@
 	    kill (9, $cpid); # make sure it gets killed
 	}
 
+	close($outfh);
+
 	waitpid ($cpid, 0);
 	$res = $?;
 	&$log_task_result($upid, $user, $res);




More information about the pve-devel mailing list