views:

93

answers:

2

I've got two reasons to use a sleep function: first, to automatically send a confirmation email to a client 20 minutes after they contact us. I don't want to use cron jobs because I want it to be exactly 20 minutes (and I'm sick of my web server sending me emails telling me they initiated a cron job.....a new email every 20 minutes!)

Second reason: I've heard of people sending out mass emails using the sleep function. Since my server will only allow 100 emails an hour, I want to use the sleep function to have the script sleep for an hour then continue where it picked up.

My question is this: does it use server resources? Will it slow things down? Are there any other problems with using the sleep function? Thanks in advance!

+3  A: 

While a process is sleeping it will not consume CPU time, but the process' working set still requires physical memory and/or pagefile to support that process. In other words, the PHP interpreter process needs to keep running. So long as your server has enough RAM this shouldn't be a problem though.

Billy ONeal
So if my hosting service is reputable (e.g., godaddy), will I likely not have to worry about RAM issues? Thanks for your help!
Dustin
It is not a good idea to use sleep function within your PHP serverside script. You are wasting a webserver thread while your are sleeping. Also, if you sleep for too long either server will kill your process as stuck or user's browser will time out and give up.The proper way to do it, is either with the cronjob or with a stand-alone process.
Vlad
You'd be opening yourself up wide open to a trivial DoS attack, too. Some jerk could register a couple hundred accounts over the course of a minute, and sleeping apache threads would eat up all of your memory.
Frank Farmer
I was assuming this wouldn't be something the OP would allow to run on more than a few processes at a time in any case. If you've got 10 min execution times -- even sleeping -- your server won't be handling load very well. The OP's description of email routing, however, seems like it could be scheduled on a few processes at most without issue.
Billy ONeal
A: 

Email delivery times are pretty variable, so you're not going to get an email to someone's inbox in exactly 20 minutes, no matter what you do.

I use a long-running background script -- launched from CLI, instead of apache -- to handle email sending. My application dumps emails into a queue table, which the mailer script polls every 15 seconds. It sleep()s between polls. This way, I only have one script trying to connect to the SMTP server, and sleeping.

That portion of the app has been running successfully with no major issues for the last 2 years. The only annoyance is keeping the script running -- if it goes down for any reason, mail doesn't go out til you bring it back up. But worst case, you could just restart it via cron periodically, e.g. daily.

If I were tackling your problem, I'd simply put a "Send time" column on the queue table, and date it 20 minutes out for these emails. The mailer would then SELECT * FROM mail_queue WHERE send_time <= NOW()

Alternately, you could use a real jobqueue like beanstalkd. I chose the queue table solely to keep my application stack simple.

Frank Farmer