Ajax/PHP - should I use one long running script or polling?
Asked Answered
E

4

4

I have a PHP script that is kicked off via ajax. This PHP script uses exec() to run a separate PHP script via the shell.

The script that is called via exec() may take 30 seconds or so to complete. I need to update the UI once it is finished.

Which of these options is preferred?

a) Leave the HTTP connection open for the 30 seconds and wait for it to finish.
b) Have exec() run the PHP script in the background and then use ajax polling to check for completion (every 5 seconds or so).
c) Something else that I haven't thought of.

Thank you, Brian

Evildoer answered 2/6, 2010 at 18:34 Comment(0)
D
2

Poll the server for updates every few seconds. When you leave connections open for that long a period of time there's always the possibility that they may be dropped by the server or their browser (browsers timeout if an HTTP request takes too long).

Doloritas answered 2/6, 2010 at 18:59 Comment(2)
Is it possible to get around this by using PHP's set_time_limit() function and Apache's KeepAliveTimeout setting?Evildoer
You can but these are not good options for the problem that you posted. The solution I gave you is your best option. Been there, done that.Doloritas
K
1

Option b) feels a little too stateful to me. Does the server need to receive a request once the 30 seconds are done, else it gets into a bad state? (like it doesn't relinquish resources or something of the like) If so, definitely go with a) methinks.

As for c), maybe you'll find something on the AJAX Pattern's Web Site under Browser-Server Diaglog.

Katheykathi answered 2/6, 2010 at 19:1 Comment(1)
A response is needed just to update the UI, no server resources. One of my big concerns is also which option would be better as the number of concurrent users increases? Thanks.Evildoer
G
1

The AJAX option seems good to me. One alternative is Comet (Ajax Push) style to minimize required traffic: Server sends signal to client(browser) when it has to say something (update UI).

Garek answered 2/6, 2010 at 20:31 Comment(0)
G
1

a) could have problems with timeout and locks server requests (usually you set limit server accepts connections). you could lock the server if many users add requests on the server. on http environment i would only keep open connections as long as necessary.

b) if it is 30 seconds long i would poll not so often like each second. i would increase the polling time. is the execution time always 30 seconds? example polling style (payload is json):


# trigger job/execution
POST /job
=> response gives 301 redirect to /jobs/{job-id}

# polling
GET /jobs/{job-id}
=> {status:busy}
or
=> {status:completed,result:...}
 

but in the end it depends on the problem, i like b) more but it adds more effort to implement. maybe you have more details? is it a high traffic scenario?

Glavin answered 2/6, 2010 at 20:32 Comment(1)
Thanks. The execution time is not always 30 seconds - in fact, usually it will be far less - but it could also be more. Traffic is low for now, under 10 concurrent users - but I'm looking for the solution that will scale best as this increases.Evildoer

© 2022 - 2024 — McMap. All rights reserved.