
BrentDavidson
New User
Jul 16, 2008, 8:17 AM
Post #1 of 1
(2739 views)
|
|
Win32 File Copy Watchdog
|
Can't Post
|
|
I have built a perl program for our branch offices to help automate copying scanned documents to our file server. Every now and then the internet will drop off at the main office causing the transfer program to hang. I'm using File::Copy::Recursive's fcopy function to copy the files. If the network drops off in the middle of a transfer, the fcopy function apparently never returns. To get around this, I've tried to implement a watchdog timer by writing a temp file, forking off the fcopy. The forked process does the fcopy then removes the temp file and exits. The main program loops through a sleep 1 command until either a set number of iterations has been reached or the temp file is removed. If the temp file still exists when the time runs out, the parent program kills off the child. Sounds all well and good in theory, but in practice it's not working so well. I'm probably complicating matters by using Perl2EXE but I have too many systems to maintain to keep Perl installed on all of them. It's far easier to implement an exe file. What appears to be happening is that once the forked process exits, the entire program crashes and I get one of the "An error has been encountered, do you want to send to microsoft" dialogs. Is there a better way to do what I'm trying to do? Here's my WatchDogCopy subroutine:
########## Sub wdcopy ########## # wdcopy is an interface to # File::Copy that uses a watchdog # timer to abort the copy process # if it appears to be hung (due # to network timeout or some # such problem sub wdcopy { my $src = shift @_; # Get source filename from call my $dest = shift @_; # Get Destination filename from call open (WDFILE,">".$wdfile) || errmsg("Can't create watchdog temp file"); # Open our temp file or open error # Error dialog then die. print WDFILE "\n"; # Send a /n to the the temp file for good measure. close (WDFILE); # Close out watchdog temp file my $watchdog = fork(); # For the Fcopy process if (! $watchdog) { # If we're not the parent process fcopy ($src,$dest); # Copy the source to the destination unlink ($wdfile); # Remove the temp file exit 0; # exit } else { # Otherwise, we are the parent so while ((-e $wdfile) && ($wdtimeout)) { # Loop until the temp file disappears or wdtimeout reaches 0 sleep 1; # Sleep for 1 second $wdtimeout--; # Decrement $wdtimeout } # We're now out of the loop so if (-e $wdfile) { # Check to see if the temp file still exists kill (1,$watchdog); # If so, we kill the forked process unlink ($wdfile); # remove the temp file and return 0; # return to the main program with an error } else { # Otherwise everything went smoothly so return 1; # return to main program with success. } } } ########## END Sub wdcopy ##########
|