문제

I need to write my own email dispatcher in php/mysql. I'm using Amazon's SES to send emails. I have records of who and what needs to be emailed in a "Queue" MySQL table. Each record gets deleted upon a successful email send. I would like to send these emails out as fast as possible but i need to keep Amazon's quota in mind. They restrict your allowed emails-sent/second.

How can i arrange this to not exceed the limit? I know i can write a php cron script that will only pull X number of records from the Queue and send those, but if my cron only runs once per minute, there is a lot of wasted time. (X being the quota limit/second)

On the other hand, if my email quota gets bumped and i can send 1000/per second(or something ridiculous), i'm probably going to run into problems if my script takes a long time to execute. The cron could potentially fire again and pull the same records from the Queue table.

도움이 되었습니까?

해결책

You should create a lock file when you execute the script and remove it when the execution is complete. Then you can alert yourself if the script is being run again without the previous execution being complete.

$filename = 'sending_emails.txt';
if (file_exists($filename)) {
    mail('your@email.com', 'Execution overlap', 'Adjust script parameters!');
    exit;
}
file_put_contents('sending_emails.txt', '1');
// Send e-mail logic here
unlink($filename);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top