Jump to content

Throttling a script


DanRz

Recommended Posts

Hey!

I am trying to write a script that will send emails to users using Amazon SES, I have a send limit of 1 per second...

Currently I have 1500 records... how would I throttle the script to only send 1 email per second?

Currently, I just use a foreach loop to go through an array and send the email using that...

$allActiveMembers = $this->getAllMembers('1');
foreach($allActiveMembers as $member){
	// If email blank... skip.
	if($member['memberEmail'] == NULL){
		continue;
	}

    // Email Valid.
	if($this->helper->isValidEmail($member['memberEmail']) == false){
    	continue;
    }

    // Send Email
    $this->SendMail($member['memberEmail']);

}

Thanks

Dan

Link to comment
Share on other sites

at one per second, it will take 25 minutes to send all the emails. php scripts are not intended to run continuously. you would need to call set_time_limit(...) inside the loop to keep the script from fatally timing out and halting, but not using set_time_limit(0) so as to not prevent the script from stopping due to a programming mistake.

this is normally done by queuing, in a database table, all the mail addresses to be sent to. having a cron job/scheduled task that runs at some reasonable interval, such as once a minute. the code that the cron job triggers checks if the queue has any unsent messages in it, loops, with a sleep() call inside the loop, to send fewer emails than the time to the next execution of the cron job, to prevent overlap, and marks each email in the queue as having been sent. e.g. with the datetime when it was sent.

doing this, you would send ~50 email per minute, in 30 executions of the cron job.

Link to comment
Share on other sites

3 hours ago, DanRz said:

I am trying to write a script that will send emails to users using Amazon SES, I have a send limit of 1 per second...

1 message per second is what they give you in the sandbox. If you're using this for real messages then you should not be in the sandbox...

Link to comment
Share on other sites

23 hours ago, Barand said:

sleep() , perchance to dream.

Thanks, I was thinking sleep but I've had problems with that in the past...

20 hours ago, mac_gyver said:

at one per second, it will take 25 minutes to send all the emails. php scripts are not intended to run continuously. you would need to call set_time_limit(...) inside the loop to keep the script from fatally timing out and halting, but not using set_time_limit(0) so as to not prevent the script from stopping due to a programming mistake.

this is normally done by queuing, in a database table, all the mail addresses to be sent to. having a cron job/scheduled task that runs at some reasonable interval, such as once a minute. the code that the cron job triggers checks if the queue has any unsent messages in it, loops, with a sleep() call inside the loop, to send fewer emails than the time to the next execution of the cron job, to prevent overlap, and marks each email in the queue as having been sent. e.g. with the datetime when it was sent.

doing this, you would send ~50 email per minute, in 30 executions of the cron job.

Hmmm I think this might be the way forward... I shall do it like this :)

19 hours ago, requinix said:

1 message per second is what they give you in the sandbox. If you're using this for real messages then you should not be in the sandbox...

Ahh yes, they gave me something like 150,000 emails per day, however, my access keys were leaked and used to send 50,000 odd emails.... so I asked them to move my limit down to 5000 per day as I would never send over that so if it ever happened again I would get stung...

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.