in reply to Re: Restart Long Running Perl Script
in thread Restart Long Running Perl Script

Here is a prototype of the file update script. The idea is that the server does CRC xml of all files in a folder, then the client on its side compares the CRC with the local folder that must be updated. Upon difference between the two the client requires the file from server Mime encoded in XML, then updates the local copy.
use strict; use File::Find; use Digest::MD5; use MIME::Base64; use XML::Twig; my $twig= XML::Twig->new(); # XML Parser #lets get all folderz my %digest; my $cdir = "./"; my $distrodir = "/socket/.DIST/"; my $current_updates_xml; find( { wanted=>sub{ if ($_ !~ /temp|\.log/) { my $filename = $_; if (-f $File::Find::name) { open(FILE, "<$File::Find::nam +e") or die "Can't open '$File::Find::name': $!"; binmode(FILE); $digest{$cdir}{$filename} = Di +gest::MD5->new->addfile(*FILE)->hexdigest; close FILE; } elsif (-d $File::Find::name) { $cdir=$File::Find::name; $cdir =~ s/^$distrodir//; $digest{$cdir} = () unless ($ +cdir."/" eq $distrodir); } } }, chdir=>1 },$distrodir); #generate XML $current_updates_xml = qq~<?xml version="1.0" standalone="yes"?> <data +>\n~; foreach my $folder (sort {$a cmp $b} keys %digest) { $current_updates_xml .= "<folder name=\"$folder\">\n"; foreach my $file (keys %{$digest{$folder}}) { $current_updates_xml .= "<file name=\"$file\" crc=\"$d +igest{$folder}{$file}\" />\n" } $current_updates_xml .= "</folder>"; } $current_updates_xml .= "</data>\n"; #print $current_updates_xml; # client side $twig->parse($current_updates_xml); my $root = $twig->root; my %redigest; foreach my $folder ($root->children('folder')) { $redigest{$folder->att('name')} = (); foreach my $file ($folder->children('file')) { $redigest{$folder->att('name')}{$file->att('name')} = $file->a +tt('crc'); } } #TEMPORARY SECTION my $destfolder = "/socket/REDIST/"; my %tofetch; #create directory structure foreach my $folder (sort {$a cmp $b} keys %redigest) { mkdir $destfolder.$folder unless (-d $destfolder.$folder); # print "$folder\n"; foreach my $file (keys %{$redigest{$folder}}) { #compare files #print "\t $destfolder$folder/$file (CRC: $redigest{$folder}{$ +file}\n"; if (-f "$destfolder$folder/$file") { open(FILE, "$destfolder$folder +/$file") or die "Can't open $destfolder$folder/$file: $!"; binmode(FILE); $tofetch{$folder}{$file} = 1 +if (Digest::MD5->new->addfile(*FILE)->hexdigest ne $redigest{$folder} +{$file}); close FILE; } else { $tofetch{$folder}{$file} = 1 } } } #debug foreach my $folder (keys %tofetch) { #print join(", ", keys %{$tofetch{$folder}}); #print "\n"; foreach my $file (keys %{$tofetch{$folder}}) { my $requestxml; # = qq~<?xml version="1.0" standalone="yes"?> <data>\n~; $requestxml .= "<data command=\"getbinfile\" folder=\"$folder\ +" file=\"$file\" />\n"; my $filexml = server_return($requestxml); $twig->parse($filexml); my $root = $twig->root; if ($root->att('status') == 1) { my $filebindata = $root->att('binfile'); $filebindata = MIME::Base64::decode($filebindata); open(OUT, "> $destfolder$folder/$file") or die "can't open + $_[1]: $!"; syswrite OUT, $filebindata; close OUT; } } } sub server_return { my $distrodir = "/socket/.DIST/"; my $ixml = shift; $twig->parse($ixml); my $root = $twig->root; my $file = $root->att('file'); my $folder = $root->att('folder'); print $file; my $encoded; my $status; if (-f "$distrodir$folder/$file") { $encoded = MIME::Base64::encode(slurpfile("$di +strodir$folder/$file")); $status = 1; } else { $status = 0; } my $oxml = "<?xml version=\"1.0\" standalone=\ +"yes\"?> <data status=\"$status\" binfile=\"$encoded\"/>\n"; return $oxml; } sub slurpfile { open(IN, "< $_[0]") or die "can't open $_[0]: $!"; binmode (IN); seek(IN, 0, 0); sysread (IN, my $slurp, -s IN); close(IN); return $slurp; }