can't get out of infinite while loop



Need your help please.

This is what it does:
---------------------------
This script takes files out of one directory. Moves the files out of
the source_dir, puts them into a bridge_dir, adds extension tmp while
there, then moves the files into the destination_dir removing the tmp
extension. It checks the source_dir for new files every 10 seconds.

To get into the infinite loop, I write to a file the "on" status
(1).

If it finds the "on" status (1), gets into the loop until that
condition changes.
It reads the file every time it gets into the loop to see if the
condition changed to "off" (0). When you send the --red switch, it
will write to a file the "off" status (0);

However, the second time it tries to read the file, it complains
saying that the file does not exist. What the hell? it just read a
second ago? Why is it saying that the file isn't there? the file is
there.

If you have a better way to get out of the loop please let me know.

I need wisdom and enlightenment.


Output
--------------

$ perl trafficLight.pl --green
writing switch on
switch on written
reading status file
condition equals 1
getting into loop
inside of loop pass. Reading status file
ready to sleep for 10 seconds
inside of loop pass. Reading status file
Uncaught exception from user code:
No such file or directory at trafficLight2007817.pl line 145.
main::do_Green() called at trafficLight2007817.pl line 60



<code>
#!/usr/bin/perl

use warnings;
use strict;
use File::Copy;
use diagnostics;
use Getopt::Long;
use Time::localtime;


my $status_file = "trafficLight.status";

my $switch_on = "1";
my $switch_off = "0";



my $source_dir_path = "/home/monk/source_dir";
my $destination_dir_path = "/home/monk/destination_dir";
my $bridge_path = "/home/monk/bridge";


my $PICKUP_INTERVAL = 10; #seconds expressed in integers
my $extension = ".tmp"; #extension added to files while on the
bridge. It can be *.tmp

#define parameters for the help menu
my $help = "";
my $green = "";
my $red = "";

GetOptions( 'help' => \$help, 'green' => \$green, 'red' => \$red );

if ($help) {
printHelp();
exit;
}

elsif ($green) {
do_Green();
exit;
}
elsif ($red) {
do_Red();
exit;
}
else {
printHelp();
exit;
}

sub printHelp {
my $help = q{

Description:
Processes files from one directory to another.


Parameters:
--help [ -h ] [ --h ] : prints this help
--green [ -g ] [ --green ] : process the files
--red [ -r ] [ --red ] : stop processing files killing
the process

Usage: perl traffic_light.pl [--help\-h\--h] [--green\-g\--g] [--red\-r
\--r]


};

print $help;

}

#write status ON to a file for green light
print "writing switch on\n";
sleep 7;
open( SWITCH, "> $status_file" ) or die "Problem writing to
$status_file file...$!";
print SWITCH $switch_on;
close(SWITCH);

print "switch on written\n";
sleep 7;
my $condition;




sub do_Green {


#flush my buffers
#$| = 1;

print "reading status file\n";
sleep 5;
open( SWITCH, $status_file ) or die "Process has been exited already
$!";

while (<SWITCH>) {
chomp;
$condition = $_;
}
close SWITCH;
print "condition equals $condition\n";
print "getting into loop\n";
sleep 7;

while ( $condition == 1 ) {
print "inside of loop pass. Reading status file\n";
sleep 5;
open( SWITCHAGAIN, $status_file ) or die $!;
while (<SWITCHAGAIN>) {
chomp;
$condition = $_;
}
close (SWITCHAGAIN);

#set up dirhandles
opendir( SOURCEDIR, $source_dir_path )
or die "Problem accessing the source directory...$!";

#get in there
chdir $source_dir_path;

#put files in an array skipping the . and .. files
my @xfiles = grep !/^\./, readdir(SOURCEDIR);

#we close the dirhandle
closedir(SOURCEDIR);

foreach (@xfiles) {
move( $_, $bridge_path ) or die "Nothing really to worry about.
Files are moving.";
}

###################
#add tmp extension

opendir( BRIDGEDIR, $bridge_path )
or die "Problem passing files to the bridge directory...$!";
chdir $bridge_path;

my @bridge_files = grep !/^\./, readdir(BRIDGEDIR);

my $old_filename;

#we rename each file individually
foreach (@bridge_files) {

#work only on the files ending in tmp
if ( $_ !~ m/$extension$/ ) {

$old_filename = $_;
rename( $old_filename, $_ . $extension ) or die $!;
}
}

#closing to avoid contamination on next process
closedir BRIDGEDIR;

######################
#move files from the bridge to the destination directory
#NOTE that they go into destination directory with the tmp extension

opendir( BRIDGEDIR, $bridge_path )
or die "Problem getting files off the bridge directory...$!";
chdir $bridge_path;

my @target_files = grep !/^\./, readdir(BRIDGEDIR);

#we move each file individually
foreach (@target_files) {
move( $_, $destination_dir_path ) or die $!;
}
closedir BRIDGEDIR;

#########################
#now let us remove the tmp extension.
#on the destination directory

opendir( DESTINATIONDIR, $destination_dir_path )
or die "Problem accessing files on the destination directory...
$!";
chdir $destination_dir_path;

my @destination_files = grep !/^\./, readdir(DESTINATIONDIR);

#we rename each file individually
foreach my $file (@destination_files) {

#work only on the files ending in tmp
if ( $file =~ m/$extension$/ ) {

#just remove the artificial tmp extension [incl dot] added while
on the bridge

my $extension_length = length($extension);
#my $new_filename = substr( $file, 0, -4 );

my $new_filename = substr( $file, 0, -$extension_length );
rename( $file, $new_filename ) or die $!;
}
}
closedir DESTINATIONDIR;


print "ready to sleep for $PICKUP_INTERVAL seconds\n";

#picks up files every x seconds
sleep $PICKUP_INTERVAL;

}

print "got out of the loop exiting now";
exit(0);
}


sub do_Red {

#if currently program executing
if (-e $status_file){
#write process id to a file to kill it later when needed.
print "writting status off";
sleep 5;

open( SWITCH, "> $status_file" ) or die "Problem writing to
$status_file file...$!";
print SWITCH $switch_off;
close(SWITCH);

print "status off written";
sleep 5;

print "removing remnant files if still there (possibly move up)\n";
unlink $status_file or die "$! File removed already";

}


print "leave in peace\n";

exit(0);

}

</code>

.



Relevant Pages

  • Re: question about thread scheduling
    ... I doubt you will need to mess with the system tick to get what ... I will try what you suggested, the reason that I didn't use the sleep ... If the NN run in a different thread as the control loop ... Sleepputs your thread to sleep for 3 timer ticks and ...
    (microsoft.public.windowsce.platbuilder)
  • Re: question about thread scheduling
    ... I will try what you suggested, the reason that I didn't use the sleep method ... If the NN run in a different thread as the control loop ... Sleepputs your thread to sleep for 3 timer ticks and ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Is it possible to bridge three NIC on a Windows 2000 Server
    ... They could be referring specifically to a bridge type switch ... One uses Router methods (routing tables ... So if you join to broadcast domains with a Bridge, ...
    (microsoft.public.win2000.networking)
  • Re: Capital Beltway in a dozen years
    ... changes have there been to the I-495 Beltway? ... IL=Inner Loop, OL=Outer LOOP ... -- Added pedestrian/bicycle bridge near Old Georgetown Rd., ... The Exit 24 semi-directional flyover or the Bucky Trotter ...
    (misc.transport.road)
  • Re: Event loop and sleep()
    ... The purpose of sleep() was to wake up at some interval, ... > if there is anything to process from sockets, ... Removing the sleepfrom the loop causes ... As someone suggested, the message queue ...
    (comp.lang.cpp)