php - How to set maximum run time using interval on each loop?

Asked

Viewed 283 times

0

Good morning! I need to send some email reminders according to some conditions. I built a script that queries the information in the database (Mysql) and triggers the emails. The script is working very well, however, from what I saw in some forums over the internet, it is interesting to set an interval between sending an email and another. I put interval of 20 seconds between after each upload, but before that I set a maximum execution time of the script, and for what I saw when I use the function set_time_limit() the function sleep() is ignored.

Follow the logic of my script below:

// faço a consulta na base de dados (MySQL)
if ($stmt->rowCount() > 0) {
   $result = $stmt->fetchAll();
} else {
   exit;
}

// controlar tempo máximo de execução do script
$qtd_email =  $stmt->rowCount();
$time_email = 20;
$segundos = ($qtd_email * $time_email) + 20; // adiciono 20 seg de folga
set_time_limit($segundos);

foreach ($result as $rs):

   // mensagem do e-mail

   // envia o e-mail

   // Ao terminar 1° loop, aguarda 20 segundos para o próximo
   sleep($time_email);

endforeach;

In that case the function sleep() is being ignored. The execution time of the script on the server that will host the application is at most 60 seconds, so I need to use set_time_limit(). In case anyone knows a way around it, I’m very grateful!

If there is another way to do it, I am also open to suggestions. Thank you from now!

  • I do not know if I understand very well, do you want to limit the time of each loop? And if there is no time does what? If there are several records does what?

  • That, limit the time of each loop. Example: finished one loop, waits 20sec and starts the next one. It will give yes time, note that I set the execution time of the script according to the amount of records in the $qtd_email variable. My problem is that PHP ignores the Sleep() function when I use set_time_limit().

  • Are you running from the command line? There are several posts here that deal with the subject, most refer to automate via crontab, I’ve suggested using Sleep but this type of task should never go through a page server.

  • I will use the Cron task scheduler to run a PHP file.

  • Pay attention to the observations there, have some cool tips, but qq doubt warn here

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.