added child reaper
[cloudstore.git] / rsync-piper.pl
index bbb51e5..767ab92 100755 (executable)
@@ -56,6 +56,7 @@ if ( $ENV{SQL} ) {
 
 mkdir "$dir/var" if ! -e "$dir/var";
 
+unlink $log_fifo if -f $log_fifo;
 mkfifo $log_fifo, 0700 unless -p $log_fifo;
 
 my $rsync_config = qq{
@@ -119,17 +120,37 @@ if ( -e $pid_file ) {
        my $pid = read_file $pid_file;
        chomp($pid);
        if ( kill 0, $pid ) {
-               warn "found rsync pid $pid";
+               warn "found rsync pid $pid\n";
                kill 2, $pid;
                while ( -e $pid_file ) {
-                       warn "waiting for rsync to die...";
+                       warn "waiting for rsync to die...\n";
                        sleep 1;
                }
+               
+               open(my $fifo, '<', $log_fifo);
+               while ( kill 0, $pid ) {
+                       my $line = <$fifo>;
+                       warn "<<< $line\n";
+               }
+
+               kill 0, $pid && die "can't kill it!";
        } else {
                unlink $pid_file;
        }
 }
 
+use POSIX ":sys_wait_h";
+sub REAPER {
+       my $child;
+       while ((my $waitedpid = waitpid(-1,WNOHANG)) > 0) {
+               warn "reaped $waitedpid" . ($? ? " with exit $?" : '');
+       }
+       $SIG{CHLD} = \&REAPER;  # loathe SysV
+}
+
+$SIG{CHLD} = \&REAPER;
+
+
 if ( ! -e $pid_file ) {
        my $exec = "$rsync --daemon --config $cfg_file --no-detach --port=$port";
        warn "START $exec\n";
@@ -146,9 +167,11 @@ if ( ! -e $pid_file ) {
        }
 }
 
+=for gearman
 use Gearman::Client;
 my $gearman = Gearman::Client->new;
 $gearman->job_servers('127.0.0.1:4730');
+=cut
 
 while(1) {
        warn "# reading log output from $log_fifo\n";
@@ -183,7 +206,9 @@ while(1) {
 
                        my $json = CloudStore::JSON::rsync_transfer \%data;
 
+=for gearman
                        $gearman->dispatch_background( 'rsync_transfer' => $json );
+=cut
 
                }
        }