]> xenbits.xensource.com Git - osstest.git/commitdiff
parallel by fork: Break out into HistoryReport
authorIan Jackson <ian.jackson@eu.citrix.com>
Tue, 4 Aug 2020 17:10:25 +0000 (18:10 +0100)
committerIan Jackson <ian.jackson@eu.citrix.com>
Wed, 19 Aug 2020 10:41:18 +0000 (11:41 +0100)
Move this code from sg-report-host-history to HistoryReport, so that
it can be reused.

No significant functional change.  Some changes to debug messages.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Osstest/HistoryReport.pm
sg-report-host-history

index 6a23cfbaf60becefab2e84f30d91f9900f3ddc5e..0b206de41b8a233098699111075986479891141a 100644 (file)
@@ -19,6 +19,9 @@ package Osstest::HistoryReport;
 use strict;
 use warnings;
 
+use Osstest;
+use Osstest::Executive;
+
 BEGIN {
     use Exporter ();
     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@@ -32,6 +35,7 @@ BEGIN {
                         cacheable_fn
                         cache_write_entry
                         cache_finish
+                        parallel_by_fork
                    );
     %EXPORT_TAGS = ();
 
@@ -213,4 +217,51 @@ sub cache_finish ($$) {
     print ::DEBUG " extra=$rows_extra\n";
 }
 
+our %children;
+our $worst = 0;
+our $whoami;
+
+sub wait_for_max_children ($) {
+    my ($lim) = @_;
+    while (keys(%children) > $lim) {
+       $!=0; $?=0; my $got = wait;
+       die "$! $got $?" unless exists $children{$got};
+       my $host = $children{$got};
+       delete $children{$got};
+       $worst = $? if $? > $worst;
+       if ($?) {
+           print STDERR $whoami."[$$]: [$got] failed for $host: $?\n";
+       } else {
+           print ::DEBUG "REAPED [$got] $host\n";
+       }
+    }
+}
+
+sub parallel_by_fork ($$$$) {
+    my ($set_whoami, $maxjobs, $tasks, $fn) = @_;
+    # does   $fn->($_) foreach @$tasks
+    # but in parallal and then exits.
+    # entries in @$taskarray should be suitable for print in messages.
+    # db is reopened in each child.
+
+    $whoami = $set_whoami;
+    undef $dbh_tests;
+
+    foreach my $task (@$tasks) {
+       wait_for_max_children($maxjobs);
+
+       my $pid = fork // die $!;
+       if (!$pid) {
+           opendb_tests();
+           $fn->($task);
+           exit 0;
+       }
+       print ::DEBUG "SPAWNED [$pid] $task\n";
+       $children{$pid} = $task;
+    }
+
+    wait_for_max_children(0);
+    exit $worst;
+}
+
 1;
index 6bf14aa2fe8f7057a400fa3d8405b589fab86cb8..0a2e9904f20ba8a5e52e22b3d96170fc50f37cb0 100755 (executable)
@@ -377,43 +377,12 @@ db_retry($dbh_tests, [], sub {
     computeflightsrange();
 });
 
-undef $dbh_tests;
-
-our %children;
-our $worst = 0;
-
-sub wait_for_max_children ($) {
-    my ($lim) = @_;
-    while (keys(%children) > $lim) {
-       $!=0; $?=0; my $got = wait;
-       die "$! $got $?" unless exists $children{$got};
-       my $host = $children{$got};
-       delete $children{$got};
-       $worst = $? if $? > $worst;
-       if ($?) {
-           print STDERR "sg-report-flight[: [$got] failed for $host: $?\n";
-       } else {
-           print DEBUG "REAPED [$got] $host\n";
-       }
-    }
-}
-
-foreach my $host (sort keys %hosts) {
-    wait_for_max_children($maxjobs);
-
-    my $pid = fork // die $!;
-    if (!$pid) {
-       opendb_tests();
-       cache_read_previous("$htmlout/$host.html") if $read_previous;
-       db_retry($dbh_tests, [], sub {
-            mainquery($host);
-           reporthost $host;
-       });
-       exit(0);
-    }
-    print DEBUG "SPAWNED [$pid] $host\n";
-    $children{$pid} = $host;
-}
-
-wait_for_max_children(0);
-exit $worst;
+parallel_by_fork('sg-report-flight', $maxjobs, [ sort keys %hosts ], sub {
+    my ($host) = @_;
+    cache_read_previous("$htmlout/$host.html") if $read_previous;
+    db_retry($dbh_tests, [], sub {
+        mainquery($host);
+       reporthost $host;
+    });
+    exit(0);
+});