I noticed that we were having trouble with our automated appointment reminders system recently in that very occasionally the site would just crash displaying "CGI limits reached. Please try again later". Not a good thing on a live production server.
After doing a bit of checking I found that it was one of the admin scripts that runs by cron job every few minutes was using all the resources available to PHP. There are only four of these and they are responsible for sending out emails, sms messages and then checking that these are being sent.
I added some code to each of them to get the execution time and then email me with the results. Time to get a cup of coffee, sit back staring at Outlook and wait for the results to come in. First off: 0.082 secs, no problem there. The next two were 0.246 and 0.132 seconds. So far so good. The last one came in at 583.456 seconds. This one might be the problem….
I had a look at the script which checks that email messages are definately being sent. When the original email is sent it sends an email with the message ID in the subject to a admin mailbox. This script checks this mailbox by IMAP and reads the subject, updates that email in the database as being checked then deleted the email.
I decided to check the mailbox using webmail and see what was going on. It crashed the webmail application while trying to read the inbox. OK. So I set up a server to go grab all the messages by pop3 and Outlook Express. 15 minutes later it was still downloading the 3000+ emails. Hmmm, might have founf the problem.
I checked the PHP manual to try and fund out why the PHP function imap_delete wasn´t deleting them. It turns out that it marks them for detetion and you need to run the imap_expunge funtion before closing the connection to delete the messages.
So, a simple case of RTFM. I´ve left the timing code in to email me if the scripts take longer than 20 seconds to run, but hopefully I won´t have any more problems.