Solved

setting a timeout

Posted on 1998-09-09
6
225 Views
Last Modified: 2010-03-05
Here is a copy of a admin at my hosting copany "I am thinking because it involves uploading the program is just not
executing but yet hanging around and keeping httpd childs open until we kill
them off.  Could you set a timeout or somethiong on this script?"  How would I do this?  Here is the script.  Thanks for any suggestions/help.
#!/usr/local/bin/perl
    use CGI;
      require '/home/dank/public_html/cgi-local/chat/locksubs.pl';

    $cgi = new CGI;

print "Content-type: text/html\n\n";


    if ($cgi->param('file') eq '') {
      &no_pic;
}
      $username = $cgi->param('USERNAME');
      $pics_dir = '/home/dank/public_html/cgi-local/pics';
    $filename = "/home/dank/public_html/cgi-local/pics/$username.gif";
    open(OUT,"> $filename");
    while($bytesread = read($cgi->param('file'), $buffer, 1024)) {
        print OUT $buffer;
    }
    close(OUT);
      &lock("waiting-count.db","$pics_dir",'3');
      open(COUNT,"<$pics_dir/waiting-count.db");
      $count = <COUNT>;
      close(COUNT);
      $count++;
      open(COUNT,">$pics_dir/waiting-count.db");
      print COUNT "$count";
      close(COUNT);
      &unlock("waiting-count.db","$pics_dir");      
      &lock("waiting.db","$pics_dir",'3');
      open(WAIT,">>$pics_dir/waiting.db");
      print WAIT "$username--$count\n";
      close(WAIT);
      &unlock("waiting.db","$pics_dir");

print "done. username = $username\n";

sub no_pic {
      print "sorry please give a pic to process.\n";
      exit;
}
0
Comment
Question by:idadan
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204782
What is &lock
0
 

Author Comment

by:idadan
ID: 1204783
A file locking routine, from the cgi-perl cookbook (matt wright)...
0
 

Accepted Solution

by:
arhnold earned 100 total points
ID: 1204784
Hello,
      One method I use when I want to have a timeout on some part of a script is to use SIGALRM, with the signal handler set to a subroutine to handle the case.  Here is an example hacked from a program of mine...

..yada yada yada...
$timeout = 60;
$SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
alarm($timeout)  # Starts the alarm timer ticking down
   # Main reading loop.
   while ($go_on) {
      &Go_Do_Something(); # This has $timeout seconds to return
                          # else the ALRM will pop  
      # We returned in time, restart the timer.              
      alarm($timeout);
      ... yada yada yada...


## In this case here is what the signal handler would do...
sub Wait_For_Timeout {
   #####################################
   # Signal Handler for Wait_For (ALRM).
   #####################################
   &dprint("\n\t** Script Timeout.  Moving on...\n");
   $go_on=0;  # Breaks the while() loop above
}

      Hope this helps.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:idadan
ID: 1204785
Ok... in this line
    $SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
is that legal? It looks funny to me.. and also shouldn't you define $go_on as something before calling it or won't it just read that as 0?  I need this code to work as is, if there is anything wrong, could you just look it over and check it?  thanks.
0
 

Expert Comment

by:arhnold
ID: 1204786
Yes this is legal.  If you have the Programming Perl v2 book look at p340-1 for an example.  The only difference is that they use
$SIG{'x'} = \&foo, but its the same thing in the end.  

      The $go_on variable was defined a couple hundred lines previous in the script.  It was declared as a 'my' and was visible to all code in this file.

      You could keep your code pretty much the same.  Write a new subroutine to cleanup the locks on a timeout and print an error message.  Then wrap the area of the code you wish to guard with the timer stuff. e.g

$SIG{'ALRM'} = \&My_Timeout;

alarm(60);
&lock("waiting.db","$pics_dir",'3');
   open(WAIT,">>$pics_dir/waiting.db");
   print WAIT "$username--$count\n";
   close(WAIT);
   &unlock("waiting.db","$pics_dir");
## We completed this part in under 60 seconds, cancel the timer
alarm(0);

$SIG{'ALRM'} = 'DEFAULT'; # Return to normal response
0
 

Author Comment

by:idadan
ID: 1204787
Thanks for the help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTTP Error 502.2 - Bad Gateway 3 217
perl split by | 2 100
add a syntax to a csv file 8 99
Regex rule to match two different url 5 73
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question