2

I'm creating a PHP script that will allow a user to log into a website and execute database queries and do other actions that could take some time to complete. If the PHP script runs these actions and they take too long, the browser page times out on the user end and the action never completes on the server end. If I redirect the user to another page and then attempt to run the action in the PHP script, will the server run it even though the user is not on the page? Could the action still time out?

Jack Humphries
  • 13,056
  • 14
  • 84
  • 125

4 Answers4

3

In the event of long-running server-side actions in a web application like this, a good approach is to separate the queueing of the actions (which should be handled by the web application) from the running of the actions (which should be handled by a different server-side application).

In this case it could be as simple as the web application inserting a record into a database table which indicates that User X has requested Action Y to be processed at Time Z. A back-end process (always-running daemon, scheduled script, whatever you prefer) would be constantly polling that database table to look for new entries. ("New" might be denoted by something like an "IsComplete" column in that table.) It could poll every minute, every few minutes, every hour... whatever is a comfortable balance between server performance and the responsiveness of an action beginning when it's requested.

Once the action is complete, the server-side application that ran the action would mark it as complete in the database and would store the results wherever you need them to be stored. (Another database table or set of tables? A file? etc.) The web application can check for these results whenever you need it to (such as on each page load, maybe there could be some sort of "current status" of queued actions on each page so the user can see when it's ready).

The reason for all of this is simply to keep the user-facing web application responsive. Even if you do things like increase timeouts, users' browsers may still give up. Or the users themselves may give up after staring at a blank page and a spinning cursor for too long. The user interface should always respond back to the user quickly.

David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    Good explanation of how and why to use a message queue! – ficuscr Jun 25 '13 at 17:37
  • @ficuscr: What I've described is sort of a "poor man's message queue" (in that it doesn't do as much guaranteeing as a more robust message queue would) but it's effective in most cases. It's possible this may even be overkill, though. I'm not familiar enough with PHP, but does it have some way of forking off a task into a separate thread and handling the response from that task later? Much in the same way that JavaScript callbacks work, or asynchronous C# methods? If it does, that might be a simpler approach. – David Jun 25 '13 at 17:42
  • 1
    Naw, threading with PHP is rarely the solution. Really for long running things as described either live with intermittent failed completions or take the time to use a MQ. Gearman is again an easy option with PHP. – ficuscr Jun 25 '13 at 17:45
  • @ficuscr FWIW, threading is rarely a good solution in any language when high-volume internet scalability is needed. –  Jun 26 '13 at 00:45
1

You could look at using something like ignore_user_abort but that is still not ideal in my opinion. I would look at deferring these actions and running them through a message queue. PHP comes with Gearman - that is one option. Using a message queue scales well and does a better job ensuring the request actions actually get completed.

Lots on SO on the subject... Asynchronous processing or message queues in PHP (CakePHP) ...but don't use Cake :)

Community
  • 1
  • 1
ficuscr
  • 6,975
  • 2
  • 32
  • 52
0

set_time_limit() is your friend.

php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
0

If it were me, I would put a loading icon animation in the user interface telling them to wait. Then I would execute the "long process" using an asynchronous AJAX call that would then return an answer, positive or negative, that you would pass to the user through JavaScript.

Just like when you upload pictures to Facebook, you can tell the user what is going on. Very clean!

Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
NaN
  • 8,596
  • 20
  • 79
  • 153