Forum Moderators: coopster

Message Too Old, No Replies

500 Internal Server Error / Script Times out

         

SeanF

1:57 am on Aug 9, 2022 (gmt 0)

5+ Year Member Top Contributors Of The Month



I have a PHP report which iterates through hundreds of records and for each record, executes a number of other MySQL queries. It's a complicated report but I haven't been able to streamline the logic any further. It doesn't have to be executed frequently so waiting a while, up to 20 minutes minutes, is acceptable.

The problem is, I am getting a "500 Internal Server Error" and the script times out after 10 minutes (almost exactly)
- I have a hosting plan with a dedicated server at DreamHost.com
- Dreamhost uses a "phprc" file in lieu of "php.ini"
- I have the following line in the phprc file: max_execution_time = 1500
- Checking the php parameters Phpinfo(), I see: max_execution_time2400

How can I ensure that the script will run to completion?
Could the problem be that the browser is timing out because it's not getting anything from the server?

Related, does anyone know of a javascript "server is working" sort of script so that the user sees that the process is still running?

Thanks

phranque

3:03 am on Aug 9, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



have you considered running this report in the background, updated regularly as a cron job, and caching the results to be reported by the web app?

SeanF

11:18 am on Aug 9, 2022 (gmt 0)

5+ Year Member Top Contributors Of The Month



Yes... the only problem with that is that the data might be "outdated when the user runs the report. For an example a user (or another user) might enter / edit a lot of data and the go to run the report to see the results and depending on the when the cron job ran last, the resulting data might not be accurate.

SeanF

11:19 am on Aug 9, 2022 (gmt 0)

5+ Year Member Top Contributors Of The Month



I meant to add: I have the following in my PHP script:

ignore_user_abort(true);
@ini_set("output_buffering", "Off");
@ini_set('implicit_flush', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('max_execution_time',2400);

Dimitri

12:15 pm on Aug 9, 2022 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



set_time_limit
[php.net...]

if you (your host) are using PHP FPM, you should also check :
process_control_timeout
[php.net...]

Your web server software, (apache, nginx, etc...) can also have time limit for child process to complete.

Not exactly related, but a web browser can decide to reset the connection, if something lasts too long.

csdude55

8:01 pm on Aug 10, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Many many years ago, I had a similar script that I ran via the browser. And yes, the browser would time out.

My solution was to put it in a finite loop (something like for ($i=0; $i < 10000; $i++) { ... }), and then in MySQL I added LIMIT 10000 OFFSET $_GET['limit']. Then I would run it in segments using:

example.com/script.php?limit=10000
example.com/script.php?limit=20000

and so on. Please forgive any typos there, that's just from memory to give you an idea.

If you don't NEED to run it in your browser then running it from SSH might bypass the browser timing out, of course.

phranque

9:28 pm on Aug 10, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



another solution is to take an email address and send a link to the updated results when completed.