I ran into this problem today. I was writing a function to run cron job to generate pdf of orders, save it in the database and send mail as an attachment. I fetched all orders from the database, and run it in a loop to generate, store and send mail for each. The job was fine on local probably because of less data but after deploy, we got the max_execution_time of 30 seconds exceeded error. The error was pointing to a file in library we are using to generate pdf(fpdf). Since it is not possible to get the data in live for test, I ran the pdf generating function inside a loop. As,
for ($i = 0; $i < 300; ++$i) {
$start = microtime(true);
$pdf = $page->createNewPdf();
echo $i;
echo '<br />';
echo base64_encode($pdf);
$end = microtime(true);
echo 'Diff '.($end - $start);
}
Here the difference is Diff 0.24374079704285 on average. After generating 126 sometimes 127 pdf files I am getting the max_execution_time exceeded error. Just to test, I called the function to send the email from within the loop as
for ($i = 0; $i < 200; ++$i) {
$start = microtime(true);
$pdf = $page->createNewPdf();
$page->sendMail(100, $pdf);
echo $i;
echo '<br />';
echo base64_encode($pdf);
echo '<br/>';
$end = microtime(true);
echo 'Diff '.($end - $start);
}
Here the difference is 6.122..... and the point is I am not getting the max_execution_time exceeded error after 30 seconds but after about 7 to 8 minutes and I am wondering Why? The php version is 7.1.33 and server is Aache.
If max_execution_time is set to 30 does is mean a request to the server should send the response within the 30 seconds limit or does it resets the execution time each time a function is called within the request, createNewPdf() = 30s, sendMail() = 30s. I am still getting the error about 7-8 minutes. Why?
Error without sendMail() called Fatal error: Maximum execution time of 30 seconds exceeded in ...../fpdf/tfpdf.php on line 1564 Error with senMail() called Fatal error: Maximum execution time of 30 seconds exceeded in ...../lib/fpdf/tfpdf.php on line 1564 How do I resolve this problem? I have tried increasing the execution_time but the error occurs after the time limit. But the main thing I wanted to understand is why did the code run for 7-8minutes and showed the execution time exceeded error.
The max_execution_time is counted from request start to end. It does not reset when calling a new function.
However, reading the documentation over at https://www.php.net/manual/en/info.configuration.php#ini.max-execution-time will tell you
The maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function for more details.
So I guess your calls out to the mailing service don't count towards the total execution time, hence your script can run for 7-8 minutes before finally hitting the PHP runtime limitation.
So, to circumwent your problem with the script running into timeouts when sending emails,
you could either increase the max_execution_time if you have access to the php.ini file or if your PHP Configuration allows ini_set, you can increase the limit manually for this specific script.
Maybe you could even use set_time_limit to increase your maximum runtime every time you've successfully sent out one email.
Another, more complex but also more robust, solution would be to implement some kind of email queue, which is then perpetually processed by a cli script via cron. This way, you don't have to worry about exceeding the script runtime. You could put all information about the mails you need to send into a file, or database table, and your cron script could read those jobs and execute them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With