From: Martin Mares Date: Sat, 29 Dec 2012 20:32:57 +0000 (+0100) Subject: Gallery2: Implemented parallel execution on multiple galleries X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=532e85eb884e73a65122f7b8f5906c4dfece68f5;p=gallery.git Gallery2: Implemented parallel execution on multiple galleries --- diff --git a/gal2/gal b/gal2/gal index d8dd694..f4f1889 100755 --- 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 < General options: -(none yet) +--all Run on all galleries in subdirectories +--threads= Run in 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 \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);