=================================================================== RCS file: /cvs/openbsd/OpenBSDTorrents/NewTorrents.pl,v retrieving revision 1.5 retrieving revision 1.10 diff -u -r1.5 -r1.10 --- openbsd/OpenBSDTorrents/NewTorrents.pl 2005/04/01 01:35:56 1.5 +++ openbsd/OpenBSDTorrents/NewTorrents.pl 2006/07/24 19:03:53 1.10 @@ -1,5 +1,5 @@ #!/usr/bin/perl -T -#$Id: NewTorrents.pl,v 1.5 2005/04/01 01:35:56 andrew Exp $ +#$RedRiver: NewTorrents.pl,v 1.9 2006/05/15 18:47:04 andrew Exp $ use strict; use warnings; use diagnostics; @@ -7,64 +7,92 @@ use lib 'lib'; use OpenBSDTorrents; -use POSIX 'setsid'; +use POSIX qw / setsid :sys_wait_h /; +$SIG{CHLD} = \&REAPER; +my %Kids; +my %Kid_Status; +my %Need_Update; %ENV = (); use YAML; -# *** This requires --log-format="%t [%p] %o %f %l" on the rsync command my $last_dir = ''; while (<>) { chomp; - if (my ($year, $mon, $mday, $time, $pid, $oper, $file, $size) = - m#^(\d{4})/(\d{2})/(\d{2}) (\d{2}:\d{2}:\d{2}) \[(\d+)\] (\S+) (.+) (\d+)$# ) { - #print "($year, $mon, $mday, $time, $pid, $oper, $file, $size)\n"; - my ($dir, $file) = $file =~ m#^(.*)/([^/]+)#; - #print "$dir - $file\n"; + print $_, "\n"; + if (my ($message, $file) = m#(.*)\s+\`([^']+)'#) { + next if $message eq 'Making directory'; + + my $dir = ''; + if ($file =~ m#^(.*)/([^/]+)#) { + ($dir, $file) = ($1, $2); + } + #print "$message - $dir - $file\n"; if ($last_dir && $last_dir ne $dir) { StartTorrent($last_dir); } $last_dir = $dir; - } else { - #print $_; } } + +# Regen just the new ones now +sleep(1) while (keys %Kids > 0); StartTorrent($last_dir); -sleep(300); - +# after the new ones are done, regen all, just to make sure +sleep(1) while (keys %Kids > 0); StartTorrent('skip'); +sub REAPER { + my $child; + while (($child = waitpid(-1,WNOHANG)) > 0) { + $Kid_Status{$child} = $?; + delete $Kids{$child}; + } + $SIG{CHLD} = \&REAPER; # still loathe sysV +} sub StartTorrent { my $dir = shift; return undef unless $dir; - if ($dir ne 'skip') { - $dir = "$BaseName/$dir"; + my $should_fork = 1; + + if ($dir eq 'skip') { + #$dir = ''; + %Need_Update = (); + $should_fork = 0; } else { - $dir = ''; + $dir = $OBT->{BASENAME} . "/$dir"; + $Need_Update{$dir} = 1; } - # This actually needs to be a sub that forks off - # the generation of this, and the running of the update script. + if (keys %Kids > 0) { + print "Not making torrents for $dir now, already running\n"; + return undef; + } - #defined(my $pid = fork) or die "Can't fork: $!"; + my @now_update = keys %Need_Update; + %Need_Update = (); - #return if $pid; + if ($should_fork) { + defined(my $pid = fork) or die "Can't fork: $!"; - #chdir $HomeDir or die "Can't chdir to $HomeDir: $!"; + if ($pid) { + $Kids{$pid} = 1; + return undef; + } - #setsid or die "Can't start a new session: $!"; - ##open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; - ##open STDOUT, '>/dev/null' - ## or die "Can't write /dev/null: $!"; - ##open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; + } - print "Making torrents for $dir\n"; - exec($HomeDir . '/regen.sh' . " $dir &"); - #exec($HomeDir . '/regen.sh', "$dir"); + if (@now_update) { + print "Making torrents for ", join(" ", @now_update), "\n"; + } else { + print "Remaking all torrents\n"; + push @now_update, $dir; + } + exec($OBT->{DIR_HOME} . '/regen.sh', @now_update); }