1

Wanted to know if there was a was to prevent a php timeout of occurring if a part of the code has started being process.

Let me explain:

i have a script that is executed that take way too long to even use

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

the code is built to allow it to timeout and restart where it was but I have 2 line of code that need to be executed together for that to happen

$email->save();
$this->_setDoneWebsiteId($user['id'], $websiteId);

is there a way in php to tell it it has to finish executing them both even if the timeout is called?

Got an idea as I'm writing this, i could use a time_out of 120 sec and start a timer and if there is less then 20 sec left before timeout to stop, i just wanted to know if i was missing something.

Thank you for your inputs.

4 Answers 4

1

If your code is not synchronous and some task takes more than 100 seconds - you'll not be able to check the execution time.

I see only one truly HACK (be careful, test it with php -f in console for be able to kill the processes):

<?php
// Any preparations here
register_shutdown_function(function(){
    if (error_get_last()) { // There was timeout exceeded error
        // Call the rest of your system
        // But note: you have no stack, no context, no valid previous frame - nothing!
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

One thing you could do is use the DATE time features to monitor your average execution time. (kind of builds up with each execution assuming you have a loop).

If the average then time is then longer than how ever much time you have left (you would be counting how much time has been taken already against your maximum execution time), you would trigger a restart and let it pick up from where it left off.

How ever if you are experiencing time outs, you might want to look at ways to make your code more efficient.

Comments

0

No, you can't abort timeout handler, but i'd say that 20 seconds is quite a big time if you're not parsing something huge. However, you can do the following:

  • Get time of the execution start ($start = $_SERVER['REQUEST_TIME'] or just $start = microtime(true); in the beginning of your controller).
  • Asset that execution time is lesser than 100 seconds before running $email->save() or halt/skip code if necessary. This is as easy as if (microtime(true) - $start < 100) { $email->save()... }. You would like more abstraction on this, however.
  • (if necessary) check execution time after both methods have ran and halt execution if it has timed out.

This will require time_limit set to big value or even turned off and tedious work to prevent too long execution. In most cases timeout is your friend that just tells you're work is taking too much time and you should rethink your architecture and inner processes; if you're out of 120 seconds, you'll probably want to put that work on a daemon.

Comments

0

Thank you for your input, as I thought the timer solution is the best way.

what I ended up doing was the following, this is not the actual code as its too long to make a good answer but just the general idea.

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

$startTime = date('U'); //give me the current timestamps

while(true){
//gather all the data i need for the email

    //I know its overkill to break the loop with 1 min remaining 
    //but i realy dont want to take chances
    if(date('U') < ($startTime+120)){
        $email->save();
        $this->_setDoneWebsiteId($user['id'], $websiteId);
    }else{
        return false
    }
} 

I could not use the idea of measuring the average time of each cycle as it vary too much.

I could have made the code more efficient but it number of cycle is based on the number of users and websites in the framework. It should grow big enough to need multiple run to be completed anyway.

Il have to make some research to understand register_shutdown_function, but I will look into it.

Again Thank you!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.