]> mj.ucw.cz Git - gallery.git/commitdiff
Gallery2: Implemented parallel execution on multiple galleries
authorMartin Mares <mj@ucw.cz>
Sat, 29 Dec 2012 20:32:57 +0000 (21:32 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 8 Feb 2015 20:14:16 +0000 (21:14 +0100)
gal2/gal

index d8dd6943ebde1b0b1303dddcc4d8201fb033bdf2..f4f18894bbb75718d03609b9faec4c476293db59 100755 (executable)
--- a/gal2/gal
+++ b/gal2/gal
@@ -9,12 +9,16 @@ use Getopt::Long;
 use FindBin;
 my $gallery_root = $FindBin::Bin;
 
+my $all;
+my $threads = 4;
+
 sub show_help() {
        print <<AMEN ;
 Usage: gal <general-options> <command> <command-options> <args>
 
 General options:
-(none yet)
+--all          Run on all galleries in subdirectories
+--threads=<n>  Run in <n> threads (default=4)
 
 Common commands:
 scan           Scan directory and obtain originals
@@ -35,6 +39,8 @@ GetOptions(
        "version" => sub {
                        print "UCW Gallery 2.0 (c) 2004-2012 Martin Mares <mj\@ucw.cz>\n";
                },
+       "all!" => \$all,
+       "threads=i" => \$threads,
 ) or die "Try `gal help' for more information.\n";
 Getopt::Long::Configure('default');
 
@@ -49,5 +55,51 @@ my $sub_path = "$gallery_root/bin/gal-$sub";
 
 $ENV{"GALLERY_ROOT"} = $gallery_root;
 $ENV{"PERL5LIB"} = join(":", $gallery_root, $ENV{"PERL5LIB"} // ());
-exec $sub_path, @ARGV;
-die "Cannot execute $sub_path: $!\n";
+
+if (!$all) {
+       exec $sub_path, @ARGV;
+       die "Cannot execute $sub_path: $!\n";
+}
+
+### Parallel execution ###
+
+my @dirs = sort map { chomp; s{^\./}{}; s{\/gallery.cf}{}; $_; } `find . -mindepth 2 -name gallery.cf`;
+my $done = 0;
+my $need = @dirs;
+
+my $running = 0;
+my $errors = 0;
+my %pid_to_dir = ();
+
+while ($running || @dirs) {
+       if ($running == $threads || !@dirs) {
+               # Wait for children
+               my $pid = wait; die if $pid < 0;
+               if ($?) {
+                       print "!! ", $pid_to_dir{$pid}, " FAILED";
+                       $errors++;
+               } else {
+                       print "++ ", $pid_to_dir{$pid};
+               }
+               delete $pid_to_dir{$pid};
+               $running--;
+               $done++;
+               print " (done $done/$need)\n";
+       } else {
+               my $dir = shift @dirs;
+               print "<< $dir\n";
+               my $pid = fork;
+               if (!$pid) {
+                       close STDOUT;
+                       open STDOUT, '>', "$dir/gallery.log" or die;
+                       chdir $dir or die "Cannot chdir to $dir: $!\n";
+                       exec $sub_path, @ARGV;
+                       die "Cannot execute $sub_path: $!\n";
+               }
+               $pid_to_dir{$pid} = $dir;
+               $running++;
+       }
+}
+
+print "$done jobs, $errors errors. See gallery.log in subdirectories.\n";
+exit ($errors > 0);