I have created a windows service using PDK. This service loops over file names and then opens a socket to a server retrieves the files contents via socket communication.

I can't seem to stop my service, it just keeps copying files nonstop. I'm not sure what to place in my code to know when the service wants to stop. I tried placing a "if (!ContinueRun())" trap in the "while" loop that reads the incoming data, but no luck there.

Any ideas?

sub Startup { while (ContinueRun($delay)) { # BACKUP CTS LOGS : Backup CTS server zipped logs that are not + backed up yet foreach my $site (@sites) { BackupLogFiles($site) if ( BetweenHoursOf($maintenance_sta +rt,$maintenance_stop) ); } } } sub BackupLogFiles { my ($site) = @_; my @list = &GetZipList( $site ); foreach my $entry (@list) { my ($file,$size) = split(/,/,$entry); &RetrieveFile($file,$size,$base_dir,$site) if ( BetweenHoursOf +($maintenance_start,$maintenance_stop) ); PerlSvc::Sleep(2); } } sub RetrieveFile { my ($file,$size,$base_dir,$site) = @_; my ($IP,$data,$buffer,$bytes_written,$bytes_needed,@ret,$percent,$ +dbh); # Get just the file name without the directory $file =~ /^.+\/(.+)$/; my $output_name = $1; $base_dir .= "/$site" . 'Pilot'; # If we already have this file, skip it if (-e "$base_dir/$output_name") { return } # Get Ip address for this server ->snip<- # Connect to server my $socket = new IO::Socket::INET(PeerAddr => $IP, PeerPort => CTS +_PORT, Proto => 'tcp', Timeout => 5); if (!$socket) { ->snip<- } $|=1; # Turn off output buffering # Create a new SessionSet to manage the socket as non-blocking my $set = SessionSet->new; if (!$set) { ->snip<- } # Create connection object to work with socket my $conn = $set->add($socket); if (!$conn) { ->snip<- } # Initiate request to CTS server my $request = sprintf('%010u%s',length("GETF1$file"),"GETF1$file") +; my $rc = $conn->write($request); my $done; if ( ! -e "$base_dir" ) { mkdir "$base_dir" } open (OUT,">$base_dir/$output_name"); binmode(OUT); while ( ! $done ) { my @ready = $set->wait(5); if ( @ready ) { if ( my $bytes = $conn->read($data,BUFFERSIZE) ) { if ($bytes > 0) { # If we haven't got the byte count of the file bei +ng # transferred, get it if (!$bytes_needed) { if (length($data) >=10) { $bytes_needed = substr($data,0,10); $data = substr($data,10); # trim off the l +ength header } } my $wrote = syswrite(OUT,$data); $bytes_written += $wrote; # If we have read what we expected, then we're don +e if ( $bytes_written == $bytes_needed ) { $conn->close; $done++; } } } else { warn "Server disconnected\n" if $command_line; $conn->close; } } else { warn "Server did not respond in 5 seconds\n" if $command_l +ine; $done++; } } close(OUT); }

janitored by ybiC: Balanced <readmore> tags around longish codeblock, as per Monastery convention


In reply to Can't Stop my PerlSvc by noslenj123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.