http://www.perlmonks.org?node_id=370630

rlb3 has asked for the wisdom of the Perl Monks concerning the following question:

I have a question that involves POE. I trying to write a script that grabs a picture from a web cam every 30 second and every 30 minutes copies it to create an archive of pictures. To further complete things, I have two servers that this script needs to run on but the script only runs on one server depending on the status it reads from another server. The problem I am having is the subroutine that run every 30 minutes (move_pic) never runs. I will include the code that I have so far, but if any one can suggest a better way I would be happy to hear it.



Times shortened for testing...

#!/usr/bin/perl use strict; use warnings; use POE; use LWP::Simple; use File::Copy; use Net::SMTP; use POSIX qw(strftime); $|++; my $smtp = Net::SMTP->new('localhost'); my $host = qx { hostname --short }; my $lb; if ($host eq 'www0') { $lb = '10.0.0.1'; } else { $lb = '10.0.1.1'; } POE::Session->create( inline_states => { _start => \&start, check => \&check, get_pic => \&get_pic, move_pic => \&move_pic, mail => \&mail, _stop => \&stop, }, ); $poe_kernel->run; exit 0; sub start { my $kernel = $_[KERNEL]; $kernel->yield('check'); } sub check { my $kernel = $_[KERNEL]; my $servermon = qx { nc -w 2 $lb 5000}; if ($servermon eq $host) {; print "Check\n"; $kernel->delay(move_pic => 30); $kernel->delay(get_pic => 10); } else { $kernel->delay(check => 120); } } sub get_pic { my ($kernel, $heap) = @_[KERNEL, HEAP]; print "get_pic\n"; my $image_link = 'http://webcam/hugesize.jpg'; my $temp_filename = '30sec.jpg'; my $filename = 'current.jpg'; unless (mirror($image_link,$temp_filename)) { print "Sending mail\n"; $kernel->yield('mail'); } move($temp_filename,$filename); $kernel->yield('check'); } sub move_pic { my ($kernel, $heap) = @_[KERNEL, HEAP]; print "move_pic\n"; my $old_filename = 'current.jpg'; my $new_filename = time.'.jpg'; if (-f $old_filename) { copy($old_filename,$new_filename); } $kernel->yield('check'); } sub mail { my ($kernel, $heap) = @_[KERNEL, HEAP]; my $failTime = strftime "%H:%M:%S", localtime; my $todayDate = strftime "%m/%d/%y", localtime; $smtp->mail('wwwlb\@localhost'); $smtp->to('me\@locahost'); $smtp->data(); $smtp->datasend("To: me\@localhost\n"); $smtp->datasend("From: www0\@localhost\n"); $smtp->datasend("Subject: WebCam Died..."); $smtp->datasend("\n\n"); $smtp->datasend("Webcam failed will retry in one minute."); $smtp->datasend("\n\nDate: ".$todayDate."\nTime: ".$failTime." +\n\n"); $smtp->dataend(); $smtp->quit; $kernel->yield(get_pic => 60); } sub stop { print "That's All\n"; }
Thanks,
rlb3