0

This question has been asked a few times and I have not found a satisfying answer to my specific scenario. I am executing a long running php script (from a symfony2 controller) that accesses a web service gets data and pushes it into a database. Sometimes, at random, I get these fatal error exceptions irrespective of what execution time limits I set -

 Fatal Error: Maximum execution time of 120 seconds exceeded 

Code

 public function webCallAction()
 {
       set_time_limit(120);
       $count = 0;
       while($count < 100) {          
            $count = $count + 1;
            try{           
            $webAPIUtil = new WebAPIHelper();
            //fatal timeout error here on random occassions
            $dataList = $webAPIUtil ->getData();
            }
             catch(\FatalErrorException $e1)
             {
                //Not working
              }
            foreach ($dataList as $dataItem) {
                //Insert into database using doctrine
            }
       }


  }

These seem to be temporary errors such as curl_timeouts etc (from within third party libraries). I would like to be able to retry the call by handling the fatal error exception. Is there a right way to do this in Symfony2?

An answer to one of the questions was -

PHP doesn't provide conventional means for catching and recovering from fatal errors. This is because processing should not typically be recovered after a fatal error. String matching an output buffer (as suggested by the original post the technique described on PHP.net) is definitely ill-advised.

But again, this seems to be a fatalerror thrown because of timeout in connection. Surely we need a retry mechanism when we catch the fatal error timeout exception.

Also, would it make sense if I put the code to be run from a command line as a script - described here - http://symfony.com/doc/current/cookbook/console/console_command.html

user275157
  • 1,332
  • 4
  • 23
  • 45
  • 1
    Can you post the code in question? It will be very difficult for anyone to give you meaningful advice otherwise. – Darragh Enright Dec 09 '15 at 20:00
  • Lots of options possibly dupe http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error and http://stackoverflow.com/questions/4410632/handle-fatal-errors-in-php-using-register-shutdown-function – Dipen Shah Dec 09 '15 at 20:18
  • @Darragh - Added the code in question – user275157 Dec 10 '15 at 04:07
  • @DipenShah - Looked at those questions and I felt this is not a duplicate since my code involves the Symfony framework. I am new to PHP and Symfony in general. Please feel free to point me in the right direction (if possible an example in Symfony2) if I am mistaken. – user275157 Dec 10 '15 at 04:16
  • 1
    Hi @user275157 - I think it would be a good idea to implement this action as a console command and execute it on the command-line (as a cron or manually). One advantage of this is that PHP's `max_execution_time` on the command-line defaults to `0`, which means it does not time out. This does not address the possibility that your process could hang forever in the event that the webservice does not return data. – Darragh Enright Dec 10 '15 at 14:17
  • Darragh...thank you...I could take that as an accepted answer. – user275157 Dec 10 '15 at 16:10

0 Answers0