Re: Cron php & refresh

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2008-04-20 at 13:32 -0500, Larry Garfield wrote:
> On Sunday 20 April 2008, Jeffrey wrote:
> > I'm working on an application that includes e-mail notifications of
> > certain events. Because the application will have hundreds or thousands
> > of users, I've designed it so that e-mail notifications are saved to a
> > MySQL table. Then a regular cron job runs a php page to select the data
> > from the table, put it into a mail() command and mail.
> >
> > My original vision was to have the script do about 50 mails, then pause
> > for a 5 seconds, do a meta refresh and run through another 50 and so on
> > - with experimentation in numbers as necessary. This process was set up
> > because I recall reading somewhere - perhaps the manual - that it is not
> > a good thing to run too many php mail()s in succession. Also, I worry
> > about php time-outs.
> >
> > If you know more about PHP and 'nix than I do, you already realise the
> > fatal flaw here: meta refresh doesn't work on a cron job.
> >
> > Question: is there an alternative? I appreciate I could use sleep()
> > after every 50 mails - but there would still be the time-out problem.
> >
> > I've searched the manual and via Google - but haven't found anything -
> > possibly I am searching on the wrong terms.
> >
> > Your help will be much appreciated.
> 
> Why not just have the cron task itself run every 5 minutes?  Every 5  minutes, 
> hit a PHP script that will pull the next 50 messages to send and send them, 
> then exit.  In 5 minutes minus the time that takes, cron will fire your 
> script again.  Problem solved.  

If you take this approach, just make sure you build a proper queuing
system or mark the mails you're processing as being processed in the DB.
Otherwise you could end up with a situation where the system takes
longer than 5 minutes to process the mails (it shouldn't, but you never
know - maybe there's a rogue process or the mail queue is backed up or
something) and then the PHP script starts at the beginning of those 50
mails again. It's an obvious error, but I've done it in an app where I
thought "There's no way the process won't finish before the next cron
job starts", and then it happened and I ended up sending a couple
thousand SMS's out twice at cost to my client. Oops.

J


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux