jimmygoldshine
jimmygoldshine

Reputation: 151

DeadlineExceededError: The overall deadline for responding to the HTTP request was exceeded on Google App Engine with Django

I'm trying to host an API (using the Django rest framework) on the Google App Engine. At the root domain (https://xxxxxx-xxxxxx-xxxxxx.appspot.com/) no problems, but when I go to the endpoint /products/ I'm hitting a server 500 error and inspecting the logs I'm seeing DeadlineExceededError: The overall deadline for responding to the HTTP request was exceeded error.

It's worth noting that /products works as expected on my local machine - and loads within a second.

The trace stack looks like so:

Traceback (most recent call last):
File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/wsgi.py", line 157, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/base.py", line 124, in get_response
response = self._middleware_chain(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/viewsets.py", line 87, in view
return self.dispatch(request, *args, **kwargs)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/views.py", line 471, in dispatch
response = handler(request, *args, **kwargs)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/mixins.py", line 48, in list
return Response(serializer.data)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/serializers.py", line 701, in data
ret = super(ListSerializer, self).data
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/serializers.py", line 240, in data
self._data = self.to_representation(self.instance)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/serializers.py", line 619, in to_representation
self.child.to_representation(item) for item in iterable
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/models/sql/compiler.py", line 887, in execute_sql
cursor = self.connection.cursor()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/utils.py", line 86, in __exit__
dj_exc_value = dj_exc_type(*exc_value.args)
DeadlineExceededError: The overall deadline for responding to the HTTP request was exceeded.

It looks like it's having a problem connecting to the db so I'll include my settings.py file:

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running on production App Engine, so connect to Google Cloud SQL using
    # the unix socket at /cloudsql/<your-cloudsql-connection string>
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': 'xx.xxx.xxx.xx',
            'NAME': 'products',
            'USER': 'xxx',
            'PASSWORD': 'xxx',
            'PORT': '3306'
        }
    }

and urls.py:

from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from app import views

router = routers.DefaultRouter()
router.register(r'products', views.ProductViewSet)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(router.urls)),
    url(r'^$', views.api_root),
    url(r'^products/', views.ProductViewSet)
]

and my app.yaml file:

runtime: python27
api_version: 1
threadsafe: true

handlers:

- url: /static
  static_dir: static

- url: /.*
  script: xxxxxxx_project.wsgi.application

libraries:

- name: django
  version: "latest"

- name: MySQLdb
  version: "latest"

env_variables:
    DJANGO_SETTINGS_MODULE: 'xxxxxxx_project.settings'
    CLOUDSQL_CONNECTION_NAME: xxxxxx-xxxxxx-xxxxxx:europe-west1:xxxxxxx-xxx
    CLOUDSQL_USER: xxx
    CLOUDSQL_PASSWORD: xxx

any help would be much appreciated!

Upvotes: 0

Views: 334

Answers (1)

Ryan A.
Ryan A.

Reputation: 411

It may be worthwhile modifying your settings.py file so that it connects to Cloud SQL through the socket. Sample code can be found here.

If this doesn't help - there is more than one cause for the DeadlineExceededError; more detailed information on the possible causes can be found here, which you may find useful.

Upvotes: 1

Related Questions