Heroku and Django random crashes

941 views Asked by At

Sometimes when I push to heroku I get a message saying:

Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.

If you are the application owner, check your logs for details.

I then run the command

$ heroku restart

a few times and its suddenly working fine again.

The error message from heroku logs is very long, here is a snippet from the bottom:

2013-08-25T03:30:26.824149+00:00 app[web.1]:     self._setup(name)
2013-08-25T03:30:26.824453+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
2013-08-25T03:30:26.824453+00:00 app[web.1]:     mod = importlib.import_module(self.SETTINGS_MODULE)
2013-08-25T03:30:26.824453+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
2013-08-25T03:30:26.824453+00:00 app[web.1]:     self._wrapped = Settings(settings_module)
2013-08-25T03:30:26.824453+00:00 app[web.1]:     __import__(name)
2013-08-25T03:30:26.824453+00:00 app[web.1]:   File "/app/zinnia/settings.py", line 5, in <module>
2013-08-25T03:30:26.824453+00:00 app[web.1]:     ('http://django-blog-zinnia.com/xmlrpc/',))
2013-08-25T03:30:26.824453+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
2013-08-25T03:30:26.824453+00:00 app[web.1]:     self._setup(name)
2013-08-25T03:30:26.824759+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 152, in __init__
2013-08-25T03:30:26.824759+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
2013-08-25T03:30:26.824759+00:00 app[web.1]:     self._wrapped = Settings(settings_module)
2013-08-25T03:30:26.824759+00:00 app[web.1]:     raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
2013-08-25T03:30:26.824759+00:00 app[web.1]: ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2013-08-25T03:30:26.825073+00:00 app[web.1]: 2013-08-25 03:30:26 [7] [INFO] Worker exiting (pid: 7)
2013-08-25T03:30:26.972807+00:00 app[web.1]: 2013-08-25 03:30:26 [2] [INFO] Shutting down: Master
2013-08-25T03:30:26.972807+00:00 app[web.1]: 2013-08-25 03:30:26 [2] [INFO] Reason: Worker failed to boot.
2013-08-25T03:30:27.576714+00:00 heroku[web.1]: Process exited with status 0
2013-08-25T03:30:28.190167+00:00 heroku[web.1]: Process exited with status 3
2013-08-25T03:30:28.205185+00:00 heroku[web.1]: State changed from starting to crashed

Does anyone have experience of this sort of thing?

EDIT: Here are my settings.py (fake SECRET):

# Django settings for myblog project.

#Note: as of 25/08/13 all this is configured for use on Heroku, need
#to make it more generic

DEBUG = False
TEMPLATE_DEBUG = False


SECRET_KEY = 'ljw+(6^2fhoa^1e_-$-u(tk6=vig*dw)r@s640o^t2b)^x*(^h'

ADMINS = (
    # ('Your Name', '[email protected]'),
)

MANAGERS = ADMINS

#Warning:being overriden towards the end
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '/home/mike/dev/myblog/myblog.db',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'GMT'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-uk'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = '/app/media'

ZINNIA_UPLOAD_TO='uploads'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATICFILES_DIR = ('zinna/static')

# Static root configuration
STATIC_ROOT = '/app'

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
#STATIC_URL = '/home/mike/dev/myblog/static/'
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    '/home/mike/dev/myblog/zinnia/static',
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    #    'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'myblog.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'myblog.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    'templates',
)

TEMPLATE_CONTEXT_PROCESSORS = (
  'django.contrib.auth.context_processors.auth',
  'django.core.context_processors.i18n',
  'django.core.context_processors.request',
  'django.core.context_processors.media',
  'zinnia.context_processors.version', # Optional
#  'zinnia.context_processors.media', #trying to solve heroku pain with this
  )

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    'django.contrib.comments',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
    'tagging',
    'mptt',
    'zinnia',
    'blog',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

#added just now so heroku can work:
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']
1

There are 1 answers

2
Burhan Khalid On

The problem is you have a stale settings file somewhere. Make sure you purge all .pyc files from your working directory (and add them to .gitignore).

In addition you have invalid paths /home/mike/dev/myblog/zinnia/static', for your static files; you should read this article that explains how to setup django for static assets on heroku.