0

I have a CakePHP controller action which can take up to 15 minutes to run.

I have set PHP's time_limit to accomodate that and the script runs OK.

The problem my client is facing is that the script does not provide any return, as it seems that the BROWSER (Firefox on most cases) times out the request, since the script won't provide any output before it finishes. I can confirm the script ran properly, because it encompasses a major INSERT at the end of it, and I can verify this from MySQL, but it fails to render the result page.

What can I do in these cases? Does CakePHP provide a way to flush partial responses to avoid browser timeout (assuming this is case...)?

Any other hints of what may be happening?

Thanks!

Pbal
  • 185
  • 14
  • 1
    Is it critical for you to show the result on the webpage after the process is done, or would you be applicable to alter it so that the php-process runs in background, and sends an email confirming the result at the end instead? – Ole Haugset Sep 18 '14 at 12:39
  • The client "likes to wait" for the result page. We are considering creating a PHP process in background and link it to a front-end ajax-based script which would poll the process as it runs, but as it stands today, we need a quick fix before moving ahead... :( – Pbal Sep 18 '14 at 14:14
  • http://cakeresque.kamisama.me/ – Reactgular Sep 18 '14 at 15:26
  • See: http://www.binarytides.com/php-output-content-browser-realtime-buffering/ & https://stackoverflow.com/questions/10579116/how-to-flush-data-to-browser-but-continue-executing & http://ckon.wordpress.com/2008/05/02/tricks-for-long-or-large-php-scripts/ – Costa Sep 20 '14 at 23:57

1 Answers1

0

There is a flush() function available in PHP (http://php.net/manual/en/function.flush.php), which is supposed to signify to PHP to flush its write buffers.

Please note: that this is based on something I've done before - I'm sure there must be a better/more elegant way of doing it.

If you don't mind whether there is output to the browser or not, you can simply output a blank string of spaces every 15 seconds or so.

As an example:

# Every 15 seconds:
echo str_repeat(' ', 100);
flush();
garbetjie
  • 579
  • 3
  • 10
  • I am aware of `flush`, but am not sure whether this will work with an MVC framework such as CakePHP. Will test and provide feedback! Thanks for the idea! – Pbal Sep 18 '14 at 14:10
  • I don't see why it wouldn't. It's a PHP function after all - the framework doesn't control whether/how it works :) – garbetjie Sep 18 '14 at 17:58
  • Not quite sure why, but it doesn't work after all. Also tried `ob_flush()` to no avail as well. While I am not 100% versed in Cake's "innards", conceptually speaking, you shouldn't output partial responses when working with an MVC framework. I guess Cake must create some sort of protection against this. Please see my response as well. Thanks for the idea though! – Pbal Sep 18 '14 at 23:23