1

I've a script to send packages of 300-500 e-mails hour. That means that this script will be fired once a hour using cron or other feature.

The server has a max execution limit of 30secs and it's not configurable.

I've been thinking if the pseudo-code below should work:

$time=time();
$count=0;
while(condition){
    $count++;
    send(email);
    $now=time();
    if($now-$time>=29){break;} //1sec margin
}
echo "$count e-mails sent";

Opinions?

Paulo Bueno
  • 2,499
  • 6
  • 42
  • 68
  • 1
    So I'm assuming you can't utilize `ini_set('max_execution_time', 300)`? – Corey Ballou Dec 18 '09 at 13:43
  • 3
    I don't want to start a useless discussion. And if those are your parameters then so be it.... but sending 10k mails/day plus being limited to such a restrictive php environment sounds a bit like a mismatch to me ;-) – VolkerK Dec 18 '09 at 13:59

3 Answers3

5

if your script is launched with cron it means that you're using PHP-CLI "PHP Command Line Interface".

As mentioned in the PHP documentation, your have no time limit while using CLI.

So you don't have to worry about that : max_execution_time is set to unlimited.

Arno
  • 745
  • 4
  • 17
1

Just to double-check that you can't set the execution time, here are two suggestions.


You could simply call set_time_limit() before sending an e-mail. According to the PHP docs:

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

For instance:

foreach ($emails as $email) {
    set_time_limit(30);
    send($email, ...);
}

Another option is via the cron. Since you are running PHP from a cron job, you can specify your own php.ini. You could execute your script as follows:

php -c /custom/directory/my_php.ini my_script.php

Where my_php.ini may specify:

max_execution_time = 0     ; (unlimited)
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
0

Break up the task in smaller chunks. Use the database to keep "state" of the actual job execution.

This approach has the advantage of being scalable: you probably will end-up having to send more emails as you grow, won't you?

jldupont
  • 93,734
  • 56
  • 203
  • 318
  • Well. The question is "How small should it be?". My subscribe list has almost 50.000 entries. And the e-mails/hour limit is set by my host. What i'm guessing is if php is able to fire at least 300 per execution. – Paulo Bueno Dec 18 '09 at 13:50
  • @Paulo: ok, I understand better the situation you are facing. Anyhow, you'll need a strategy to deal with exceptions: that's when keeping a context in a permanent store kicks in. – jldupont Dec 18 '09 at 14:04