开发者

Preventing a 504 Gateway Timeout with huge PHP script

开发者 https://www.devze.com 2023-01-20 13:22 出处:网络
I\'m currently running an Apache server (2.2) on my local machine (Windows) which I\'m using to run some PHP scripts to take care of some tedious work. One of the scripts involves a ton of moving, res

I'm currently running an Apache server (2.2) on my local machine (Windows) which I'm using to run some PHP scripts to take care of some tedious work. One of the scripts involves a ton of moving, resizing, and download / uploading files to another server. I wou开发者_运维技巧ld very much like the script to run constantly so that I don't have to baby the script by starting it up again every time it times out.

set_time_limit(0);
ignore_user_abort(1);

Both are set in my script, but after about 30mins to an hour the script stops and I get the 504 Gateway Time-out message in my browser. Is there something I missing in Apache or PHP to prevent the timeout? Or should I be running the script a different way?


Or should I be running the script a different way?

Definitely. You should run your script from command line (CLI)


if i should implement something like this i would you 2 different scripts:

  • A. process_controller.php
  • B. process.php

The workflow should be:

  1. the user call the script A by using a browser
  2. the script A start the script B by using a system() or exec() and pass to it a "process token" via command line.
  3. the script B write the execution status into a shared space: a file named as the token, a database table. in general something that can be read also by the script A by using the token as reference
  4. the script A contains an AJAX call, in polling, that ask to the script A the status of the process for a given token

Ajax polling:

<script>  
 var $myToken;  
 function ajaxPolling()
 {
   $.get('process_controller.php?action=getStatus&token='+$myToken, function(data) {
    $('.result').html(data);
 });

 }
 setInterval("ajaxPolling()",60*1000); //Every minute
</script>

there are some considerations about the communication between the 2 processes, depending on how many instances of the script B you would be able to run in parallel

  1. Just one: you don't need a random/unique token
  2. One per user: session_start(); $token = session_id();
  3. More than one per user: session_start(); $token = session_id().microtime();


If you need to run it form your browser, You should make sure that there is not php execution limit in the php.ini file, but also that there is not limit set in mod_php (or what ever you are using) under apache.


Use php's system() to call a shell script which starts a service/background task.

0

精彩评论

暂无评论...
验证码 换一张
取 消