Skip to content

Commit

Permalink
Add gunicorn to requirements, and include Sentry messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
abkfenris committed Jan 22, 2019
1 parent 936ed82 commit 47c9d1e
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 10 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ SECRET_KEY=a_really_long_random_string_that_no_one_should_no_and_should_probably
REDIS_CACHE=rediss://cache:6379/0
DJANGO_DEBUG=True
DJANGO_MANAGEPY_MIGRATE=off
SENTRY_DSN=some_long_string_from_sentry
```

An additional environment variable is currently configured in `docker-compose.yaml` as it is unlikely to need to be changed.
Expand Down
28 changes: 22 additions & 6 deletions app/buoy_barn/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
"""

import os
import logging

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

logger = logging.getLogger(__name__)

# SECURITY WARNING: don't run with debug turned on in production!
try:
DEBUG = bool(os.environ["DJANGO_DEBUG"] != "False")
except KeyError:
DEBUG = False

try:
sentry_sdk.init(
dsn=os.environ["SENTRY_DSN"],
integrations=[DjangoIntegration()],
environment="dev" if DEBUG else "prod",
)
logger.info("Sentry initialized")
except KeyError:
logger.warning("SENTRY_DSN missing. Sentry is not initalized")

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -22,12 +44,6 @@
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ["SECRET_KEY"]

# SECURITY WARNING: don't run with debug turned on in production!
try:
DEBUG = bool(os.environ["DJANGO_DEBUG"] != "False")
except KeyError:
DEBUG = False


ALLOWED_HOSTS = ["*"]

Expand Down
10 changes: 6 additions & 4 deletions app/deployments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from erddapy import ERDDAP
from memoize import memoize
from requests import HTTPError
from sentry_sdk import capture_exception, capture_message

from deployments.utils.erddap_datasets import filter_dataframe, retrieve_dataframe

Expand Down Expand Up @@ -92,9 +93,9 @@ def latest_erddap_values(self):
try:
row = filtered_df.iloc[-1]
except IndexError:
logger.warning(
f"Unable to find position in dataframe for {self.name} - {series.variable}"
)
message = f"Unable to find position in dataframe for {self.name} - {series.variable}"
logger.warning(message)
capture_message(message)
reading = None
time = None
else:
Expand All @@ -113,7 +114,8 @@ def latest_erddap_values(self):
"start_time": series.start_time,
}
)
except HTTPError:
except HTTPError as error:
capture_exception(error)
for series in timeseries:
readings.append(
{
Expand Down
4 changes: 4 additions & 0 deletions app/deployments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
GeoFeatureModelSerializer,
GeometrySerializerMethodField,
)
from sentry_sdk import capture_message

from .models import Platform

Expand All @@ -20,6 +21,9 @@ def get_location_point(self, obj):
try:
return obj.location
except AttributeError:
capture_message(
f"Platform ({obj.name}) does not have a valid location attribute"
)
return None

attribution = serializers.SerializerMethodField()
Expand Down
2 changes: 2 additions & 0 deletions app/forecasts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework import viewsets
from rest_framework.exceptions import NotFound
from rest_framework.response import Response
from sentry_sdk import capture_message

from forecasts.forecasts import forecast_list
from forecasts.serializers import ForecastSerializer
Expand All @@ -21,6 +22,7 @@ def retrieve(self, request, pk=None): # pylint: disable=no-self-use
try:
forecast = filtered[0]
except IndexError:
capture_message(f"Unknown forecast slug: {pk}")
raise NotFound(detail=f"Unknown forecast slug: {pk}")
seralizer = ForecastSerializer(forecast)
data = seralizer.data
Expand Down
1 change: 1 addition & 0 deletions app/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ django-cors-headers==2.4.0

# deployment
uWSGI==2.0.17.1
gunicorn==19.9.0
whitenoise==4.1.2

# development
Expand Down
4 changes: 4 additions & 0 deletions app/utils/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/sh

# from https://www.caktusgroup.com/blog/2017/03/14/production-ready-dockerfile-your-python-django-app/
# entrypoint design

set -e

until pg_isready -h $POSTGRES_HOST; do
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ web:
# command: bash -c "./utils/wait-for-it.sh db:5432 && python manage.py runserver 0:8080"
# command: "uwsgi --http :8080 --wsgi-file buoy_barn/wsgi.py --master --processes 4 --threads 2"
command: "uwsgi --module=buoy_barn.wsgi:application --master --pidfile=/tmp/project-master.pid --http :8080 --uid=1000 --gid=2000 --max-requests=5000 --vacuum --processes 4"
# command: "gunicorn --bind 0.0.0.0:8080 buoy_barn.wsgi:application"
restart: always
volumes:
- ./app:/app
Expand Down
12 changes: 12 additions & 0 deletions server_config/76_buoybarn_neracoos.httpd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName buoybarn.neracoos.org
ErrorLog "logs/buoybarn.neracoos.org-error_log"
CustomLog "logs/buoybarn.neracoos.org-access_log" common
LogLevel warn

ProxyPreserveHost On

ProxyPass / http://docker1:8080/
ProxyPassReverse / http://docker1:8080/
</VirtualHost>

0 comments on commit 47c9d1e

Please sign in to comment.