javascript - Terminate PHP script on server through AJAX call - Stack Overflow

I have written a PHP script to import large amount of data. The import process is triggered through Aja

I have written a PHP script to import large amount of data. The import process is triggered through Ajax call and the ajax request keep on waiting for the server response. As I am working on a dedicated server so there is no issue of timeout.

The problem is that we require a feature by which we can terminate the import process. For example a stop button on client-side. We thought that if we had killed the waiting ajax call then the process on the server will also stop as there is no request to serve. But unfortunately that is not the case, the script keeps on executing on server side while the Ajax Request is already killed from client.

Secondly, we use PHP session in this project. Let say if the cancel button requires an Ajax call to another script on the server to stop the process then How could that request will reach server if there is already a waiting ajax request. Php/Apache will hold the second request until the first request cycle is pleted.

Note: As per our project architecture we require session_start() on every page. It will be good if anyone can guide on these issues.

I have written a PHP script to import large amount of data. The import process is triggered through Ajax call and the ajax request keep on waiting for the server response. As I am working on a dedicated server so there is no issue of timeout.

The problem is that we require a feature by which we can terminate the import process. For example a stop button on client-side. We thought that if we had killed the waiting ajax call then the process on the server will also stop as there is no request to serve. But unfortunately that is not the case, the script keeps on executing on server side while the Ajax Request is already killed from client.

Secondly, we use PHP session in this project. Let say if the cancel button requires an Ajax call to another script on the server to stop the process then How could that request will reach server if there is already a waiting ajax request. Php/Apache will hold the second request until the first request cycle is pleted.

Note: As per our project architecture we require session_start() on every page. It will be good if anyone can guide on these issues.

Share Improve this question edited May 13, 2013 at 17:26 Charles 51.5k13 gold badges106 silver badges144 bronze badges asked May 13, 2013 at 12:09 asim-ishaqasim-ishaq 2,2306 gold badges33 silver badges58 bronze badges 3
  • You didn't ask a question. – hobbs Commented May 13, 2013 at 12:16
  • @hobbs read the last line "It will be good if anyone can guide on these issues" – asim-ishaq Commented May 13, 2013 at 13:44
  • 3 second point: PHP locks the session file while it's actively used by a script. Unless you session_write_close() to release the lock, only one session-using PHP script can be in-flight at any one time. – Marc B Commented May 13, 2013 at 14:02
Add a ment  | 

6 Answers 6

Reset to default 1

You can write the Process ID at the start of running the script to a file, the db or a cache. You can get the Process ID with http://php/manual/en/function.getmypid.php. This assumes each script has its own Process ID.

The kill script (not using the locked session) could read that Process ID and try to kill it.

Be careful while doing long running processes in PHP as PHP's zend engine & GC is not suited for long running processes.

So, I strongly suggest using a proper job manager like gearman. gearman does have a php extensions. Using a job manager will give you full control over each process. you can start/stop processes & taks.

Another option is to use a queue, like amqp, to handle these tasks more cleanly. Which one is more suitable for your use case, I'll let you decide.

How about setting a time limit slightly higher than your AJAX timeout on your PHP script? This should kill the PHP script if it runs over time. Something similar to this:

<?php

    set_time_limit(20);

    while ($i<=10)
    {
        echo "i=$i ";
        sleep(100);
        $i++;
    }

?>

Source: http://php/manual/en/function.set-time-limit.php

Once a script runs it can only be stopped by ending the php process working on the script. One possibility would be to use the session to store a "continue" condition when another script is called.

For example:

Script 1 is the worker (importer)
Script 2 is a function called repeatedly by ajax as long as the importer shall work.
Script 1 and 2 share let's say $_SESSION['lastPing'].

so

Script 2 sets $_SESSION['lastPing'] = time(); on each call.
Script 1 has a condition if($_SESSION['lastPing'] - 30 > time()){ die(); }

You might be able to handle this using the proc_ functions. A possible outline of the steps:

  1. Create a unique token and pass it to the server along with the order to begin the import.
  2. When order to import is received (via AJAX):
    a) Store a record showing that this token is being processed (via file, db, memcached).
    b) Run the import script using proc_open.
    c) Begin a polling loop, checking the record from (2a) to see that this token is still in processing status. If not, call proc_terminate and exit the loop.
  3. If the order to stop import is given (in a separate AJAX call), update the persisted record to indicate that the particular token should be stopped, which will be picked up in (2c)

Goto the following link for the exact solution to your problem:

PHP auto-kill a script if the HTTP request is cancelled/closed

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745114232a4612033.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信