0

I have a web application written in PHP, running on a Linux Azure virtual machine with NGINX. The application is connected to an API (written in Python) on a separate server with NGINX (similar Linux Azure virtual machine). This API performs a complex operation which takes between 30sec and 20 min to complete. So the application has to wait for it.

The problem is that with long wait times, the API respond is not registered in the web app. I have tried the following:

— verified in the endpoint of the API and the logs that the API provides a response after long processing times (it does)

I suspect it is a timeout issue so have tried:

— fixed the PHP timeout settings and the timeout for the  /login_c/check_login endpoint

— checked the code for the request and response sent and received from the API, where I am using curl method. This is the parameter for the time out of curl:

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 2100);".

The exec method executes in the background:

exec($command);

The following articles did not provide a solution:

Setting Curl's Timeout in PHP

PHP cURL methods time out on some URLs, but command line always works

Any advice on how to solve this problem?

twhale
  • 725
  • 2
  • 9
  • 25
  • Did you disabled timeout on nginx? PHP is only process, apache/nginx is server can kill PHP process. 20 minuts request is to long, try queue/cron or use message broker. – seti Apr 12 '19 at 08:07
  • Keeping a PHP process waiting for 20 minutes would, even if you got it working, not be an optimal solution. Can to modify the Python API? You could then let it report what has finished and show that in your PHP web app (when needed). There are lot's of other methods thinkable, but just waiting doesn't seem a good solution for a web app. You should however be able to get that working. – KIKO Software Apr 12 '19 at 08:10
  • Just to clarify why waiting is not a good idea: 1. You are completely dependent on the connection staying open all that time. That is a fragile solution. 2. You waste a connection. 3. If it is really a web application, will the user let the browser wait that long? – KIKO Software Apr 12 '19 at 08:15
  • @KIKOSoftware 1: We cannot make run the API run faster. But do you mean we should make the api send out some message at regular intervals (say every minute) - to keep the web app open? – twhale Apr 12 '19 at 08:41
  • @KIKOSoftware 2: (3) The response will stored for the user. So if the user closes the browser, the next time he/she opens the app the response will be there – twhale Apr 12 '19 at 08:42
  • 1. No, what I meant is that the API could store the end result, and hand it to the web app when that asks for it. I cannot be more specific given the information you gave. 2. So it is a PHP background process, independent of the browser. – KIKO Software Apr 12 '19 at 09:19
  • Okay, that makes sense. Thanks. – twhale Apr 12 '19 at 09:29
  • What you API is doing? This is empty request and response? – seti Apr 12 '19 at 09:42
  • @seti: The API sends a text message to the web app – twhale Apr 12 '19 at 11:06
  • And what web app is doing with this text? Maybe you need change some functional logic. – seti Apr 12 '19 at 13:11

2 Answers2

0

You must edit php.ini or add to php script:

ini_set("max_execution_time",1800); //for 30 minutes request
Aleksey
  • 21
  • 2
0

It seems that this solved the problem:

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 2100);".
twhale
  • 725
  • 2
  • 9
  • 25