Maximum Execution time doesn’t work?

Asked

Viewed 137 times

1

I need my application to stop, accuse error, "Maximum Execution time of 5 Seconds exceeded" would be ideal as long as the time is over 5 seconds for example. Researching right here in the community I found some solutions, but it is not working. Is there any other setting that can block this?

Follows the code:

<?php
namespace MyApp\Domain\Eventos;

ini_set('max_execution_time', 5);
set_time_limit(5);

In this case, I am processing a file and it processes in about 20 seconds, however, even with the limited set there, it does not give timeout.

NOTE: I don’t have access to php.ini

Thanks in advance!

  • Where are you putting this ini_set?

  • In the class I am running, in this case it is the class that processes the file.

  • tries with php’s Sleep function => Sleep(5);

  • Right, but with Sleep, it will stop for 5 seconds and then back, I need to really error the application if it passes the informed time. In my case, if it takes more than five seconds, it’s because there’s something very wrong, you understand?

  • It depends on where in this class you are applying, because if you only use one method of it she will not even see this ini_set. Either you add to the constructor or in the method body

  • Yes, I call a method straight out, in case I do not add the constructor no. In this case, there is another alternative p/ setar this guy?

  • Get it, see if this link helps you ! https://laracasts.com/discuss/channels/general-discussion/maximum-execution-time-of-30-seconds-exceeded?page=1

  • Okay, I will try, but here the company blocks the registration. Anyway thanks. I will continue searching too.

  • I don’t know exactly what the final reason you want to reach, but if possible you could use Symfony Process (https://symfony.com/doc/current/components/process.html) to run the script as a separate process and set a timeout in it.

  • Peter, it would be ideal, but the "deadline" does not allow, the quickest way I found was this. However, for now without success.. :(

Show 5 more comments

1 answer

2

The ini_set, according to its own documentation

Sets a new value for the specified configuration option. The configuration option will keep the new value during script execution and will be restored at the end of script execution.

That is, when executing a script, if the directive is defined ini_set, it will alter temporarily the value set and once the execution is finished it will return to the initial value. then, you need to use it when starting the execution of a script.

Suppose the following route on the Laravel

Route::get('/', 'HomeController@index')->name('home');

In the controller I must define the ini_set thus

<?php
    namespace App\Http\Controllers;

    use App\Http\Controllers\Controller;

    class HomeController extends Controller
    {
        public function index() {
            ini_set('max_execution_time', 5);
            /* ... */
        } 
    }

when calling the method index the value of max_execution_time will be amended. Or change this value through construtor

<?php
    namespace App\Http\Controllers;

    use App\Http\Controllers\Controller;

    class HomeController extends Controller
    {

        public function __construct() {
            ini_set('max_execution_time', 5);
        }

        public function index() {
            /* ... */
        } 
    }

thus the call of any method of HomeController will change the value of max_execution_time.

  • I did exactly this way (the constructor) and the script continues longer.. I’m still investigating.

Browser other questions tagged

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