?
Solved

Never-ending While Loop

Posted on 2010-11-22
3
Medium Priority
?
641 Views
Last Modified: 2013-12-12
So I am writing a mail-import script that will run every minute and check for new email messages from an internal IMAP/POP server... The script will run in multiple instances, one per installation and essentially never be 'done' fetching... at any given time there could be 6-7 installations running at one time (maybe more)

I have part of the script already done, accessing IMAP folders and pulling out vars/header info from mailbox was super easy. Only if a new mID exists (unread) does it proceed with any additional functions; making the majority of the seeks to EXIM fairly light weight. I would really like to stay in PHP (vs Perl) because I have a lot of functions ready-to-go from other post-processes I can call and take advantage of easily.

Here is my question....

a) The below article suggests using sleep() in a never end while() loop and running it as a background process. This was my first thought, essentially I can 'never close' the servers IMAP connection and just keep requesting changes from the mailbox, essentially forcing the connection to stay open in while( ....dothis.. sleep(60) ) and just kick the process once for each installation. I've been reading up on background processing PHP in this situation (never-ending); i've done it with perl/bash but I'm not sure how it would work in PHP; if the PHP timeout limit is reached the script will end, correct? or is this just a function of http PHP processing?

(The suggestion to set PHP limits to '0' unlimited timeout is just not something I'm willing to do, to risky) does that blow this concept for me?

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_22132759.html

b) I could set a cron to go off every minute open/close the IMAP connection each time, set it on a cron (per installation) and kick it every minute but that will run the risk of some processes not ending, before the next begins and essentially run the risk of crashing the server all together, if they piled up.

I'd rather go with theory A but i'm a little unsure and dont want to start one direction only to end in defeat..... So?

Can you while(true){ ..dothis.. sleep() } from shell, without modifying the PHP timeouts. essentially forcing sysadmin to kill the PID to stop the process? Would this have any additional effect server side that would make either or both of these inefficient?
0
Comment
Question by:edjent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Author Comment

by:edjent
ID: 34192983
Oh Yeah! Thanks for any help that gets me moving down the road!
0
 
LVL 8

Accepted Solution

by:
ropenner earned 2000 total points
ID: 34195327
for B)  you can avoid crashes if you check within your PHP script first whether the previous one finished ... by looking through the current processes 'ps' or another method of detecting that it finished up previously.  Thus avoiding opening up IMAP folders if still in use.  If the previous one didn't finish then exit the script and wait for the next minute to come around.

A)  I've just tried this and it looks like it works with sleeps without having to touch the timeout.  Writing a log file would be advisable so that you can see what is happening as it sleeps and wakes.

The code I just tried sets the timeout to 2 seconds and then sleeps every second for 10 seconds ... the process is not interrupted from the commandline

<?PHP
set_time_limit(2);

$loop =0;
while(1) {
      $loop++;
      sleep(1);
      print "yo  ";
      if ($loop > 10) {
            exit(1);
      }
}
?>
0
 
LVL 6

Expert Comment

by:rubene
ID: 34274248
Maybe you could run your php script as a daemon, because php has wrappers around the common C functions related to daemon processes.

So you could fork child processes from your main php script process and control the child processes from that main process. See: http://php.net/manual/en/function.pcntl-fork.php for info on the pcntl_fork function.Just a suggestion.

If you end up going this way don't forget to use the 'normal' daemon stuff, such as fclosing or redirecting stdin, stdout en stderr to /dev/null.

HTH.
Ruben.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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