3

I have deployed my Django app with Google App Engine. The app contains APIs; in each API there are calculations on thousands of records. In short I have some APIs that take more than 60 sec and I get 502 error. How can I fix that? Are google app engine's B1 or B2 instances a solution to my problem? please guide me, thanks

Rubén C.
  • 1,098
  • 6
  • 16
Nauman Sharif
  • 163
  • 11

2 Answers2

3

I have been experiencing a similar problem, and the logs tell me that the worker is timing out.

enter image description here

The default Gunicorn worker timeout is 30 seconds. My app is doing some API requests that are taking longer than 30 seconds, so that's why I'm getting timeouts. If you think this is your problem as well, you can address it by adjusting the entrypoint line in your app.yaml file:

runtime: python37
entrypoint: gunicorn -b :$PORT example.wsgi --log-level=DEBUG --timeout=30
service: default
DragonBobZ
  • 2,194
  • 18
  • 31
  • In App Engine, how would one drop a request if it has been more than `n` seconds since the client dispatched the request? – Raqib Dec 20 '21 at 23:41
  • It depends on what program you are using as a backend server. If you are using gunicorn, you would set `--timeout=n` above, and gunicorn will send a kill signal that should terminate the worker that's working on the current request. I believe you can also set up an nginx.conf for App Engine where you should be able to configure timeouts, but that won't kill workers on the backend, so setting the timeout in gunicorn in this instance would be preferable. Any mature server software you are using to run your application should provide a similar option. – DragonBobZ Dec 21 '21 at 17:37
  • I do not want to kill the worker, I just want to drop the request. In my use case, the request needs to be discarded after 5 seconds as the client timeouts the request. I expected Gunicorn to drop the request once the request times out but I am not able to achieve the behaviour. So, my question is, do I need to configure App engine or gunicorn to achieve that behaviour? – Raqib Dec 21 '21 at 19:44
  • 1
    The answer is you can probably do it either way, but I don't know which one is less effort. Gunicorn has [custom hooks](https://docs.gunicorn.org/en/latest/settings.html#worker-abort) that you can configure so you may be able to override the abort signal that's sent on a timeout. Assuming the App Engine environment you are in allows you to modify the nginx config, that would probably be the least-effort way to achieve your goal of simply dropping requests after a certain amount of time. – DragonBobZ Dec 21 '21 at 20:01
  • Thank you for clearing that out. Much appreciated – Raqib Dec 21 '21 at 20:32
1

For your specific case I can see 3 possible solutions:

  • The easiest way to fix your issue would be switching to B1 or B2 instances which support manual and basic Scaling Types, both offer the possibility to have requests running up to 24 hours.
  • If for some reason you wanted to stick to F2 instances you have the option to create a task on a task queue, this will allow you to run the requests asynchronously.
  • You could also switch to GAE Flexible, this will give you a 60 minutes maximum request timeout, as stated in this documentation.
Miguel
  • 956
  • 6
  • 20
  • can you please indicate what are the settings I can use in app.yaml to increase request duration, I have changed my instance to B1 with these settings but still timeout issue ``` instance_class: B1 basic_scaling: max_instances: 1 idle_timeout: 10m ``` – Nauman Sharif Jul 08 '19 at 06:56