Loading message instead of timeout while executing a heavy query

偶尔善良 提交于 2019-12-14 00:03:58

问题


I'm running Zend server and ZF1 with db2 database. For some export, the queries take a very long time (>50 seconds) this causes a 408 timeout error. I'm looking for a solution to show a loading message on the client while running the SQL query. Any help is appreciated.


回答1:


You need to increase the maximum execution time allowed for php so that it can finish and not be terminated with 408 return code. Either locally for that script only: http://php.net/manual/en/function.set-time-limit.php or globally: http://php.net/manual/en/info.configuration.php#ini.max-execution-time

The simplest way to show loading progress is to have the long job executed with an AJAX request. Your main page should show the loading progress and fire a request to the export script with Javascript. When that export script finishes, the main page can show a success message.

If the export script creates something like a CSV file that needs to be downloaded, rather than send that CSV as the response body you can output it to a file on the server and then send a url of that file in the response. Your Javascript in the main page can show that url and prompt the user to download it in the success message.

Another way to do it is to build a job queue. This can be done using a queuing system or just using a table of jobs in the database. When the user requests an export, you create a new job. You need a process that runs on the server, as a cron job for example, that checks the table for all new jobs, and when there is one it runs the export and updates the status of the job. In your page you need to periodically, with Javascript or simply by reloading the page, check the status of your job and show a success message when it's completed. Again, if the export creates a file, the server process needs to write it and you include a link to download in the success message.



来源:https://stackoverflow.com/questions/30775947/loading-message-instead-of-timeout-while-executing-a-heavy-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!