Skip to content

Commit ce75197

Browse files
committed
Merge branch 'master' into subdomains-to-query-paths
2 parents 3699245 + 3e0c866 commit ce75197

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+430
-282
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ max-line-length=100
33
docstring-convention=all
44
import-order-style=pycharm
55
application_import_names=pydis_site
6-
exclude=__pycache__, venv, .venv, **/migrations/**, .cache/
6+
exclude=__pycache__, venv, .venv, **/migrations/**, .cache/, gunicorn.conf.py
77
ignore=
88
B311,W503,E226,S311,T000
99
# Missing Docstrings

.github/workflows/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
uses: Azure/k8s-deploy@v1
4242
with:
4343
manifests: |
44-
site/deployment.yaml
44+
namespaces/default/site/deployment.yaml
4545
images: 'ghcr.io/python-discord/site:${{ steps.sha_tag.outputs.tag }}'
4646
kubectl-version: 'latest'
4747

Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.9.5-slim-buster
1+
FROM --platform=linux/amd64 python:3.9-slim-buster
22

33
# Allow service to handle stops gracefully
44
STOPSIGNAL SIGQUIT
@@ -26,6 +26,11 @@ COPY . .
2626

2727
# Set dummy variables so collectstatic can load settings.py
2828
RUN \
29+
# Set BUILDING_DOCKER to anything but undefined so settings.py
30+
# does not insert django_prometheus into the list of installed apps.
31+
# This prevents django_prometheus from attempting to connect to the database
32+
# when the collectstatic task is ran.
33+
BUILDING_DOCKER=yes \
2934
SECRET_KEY=dummy_value \
3035
DATABASE_URL=postgres://localhost \
3136
METRICITY_DB_URL=postgres://localhost \

docker-compose.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ services:
1818
POSTGRES_DB: pysite
1919
POSTGRES_PASSWORD: pysite
2020
POSTGRES_USER: pysite
21+
healthcheck:
22+
test: ["CMD-SHELL", "pg_isready -U pysite"]
23+
interval: 2s
24+
timeout: 1s
25+
retries: 5
2126
volumes:
2227
- ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
2328

@@ -35,7 +40,8 @@ services:
3540
ports:
3641
- "127.0.0.1:8000:8000"
3742
depends_on:
38-
- postgres
43+
postgres:
44+
condition: service_healthy
3945
tty: true
4046
volumes:
4147
- .:/app:ro

gunicorn.conf.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Configuration file for gunicorn.
3+
4+
Code taken from https://github.com/prometheus/client_python#multiprocess-mode-eg-gunicorn
5+
"""
6+
from prometheus_client import multiprocess
7+
8+
9+
def child_exit(server, worker) -> None:
10+
multiprocess.mark_process_dead(worker.pid)

manage.py

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#!/usr/bin/env python
22
import os
3-
import re
4-
import socket
53
import sys
6-
import time
7-
from typing import List
84

95
import django
106
from django.contrib.auth import get_user_model
@@ -42,7 +38,7 @@ class SiteManager:
4238
--verbose Sets verbose console output.
4339
"""
4440

45-
def __init__(self, args: List[str]):
41+
def __init__(self, args: list[str]):
4642
self.debug = "--debug" in args
4743
self.silent = "--silent" in args
4844

@@ -84,38 +80,6 @@ def create_superuser() -> None:
8480
else:
8581
print(f"Existing bot token found: {token}")
8682

87-
@staticmethod
88-
def wait_for_postgres() -> None:
89-
"""Wait for the PostgreSQL database specified in DATABASE_URL."""
90-
print("Waiting for PostgreSQL database.")
91-
92-
# Get database URL based on environmental variable passed in compose
93-
database_url = os.environ["DATABASE_URL"]
94-
match = re.search(r"@([\w.]+):(\d+)/", database_url)
95-
if not match:
96-
raise OSError("Valid DATABASE_URL environmental variable not found.")
97-
domain = match.group(1)
98-
port = int(match.group(2))
99-
100-
# Attempt to connect to the database socket
101-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
102-
103-
attempts_left = 10
104-
while attempts_left:
105-
try:
106-
# Ignore 'incomplete startup packet'
107-
s.connect((domain, port))
108-
s.shutdown(socket.SHUT_RDWR)
109-
print("Database is ready.")
110-
break
111-
except socket.error:
112-
attempts_left -= 1
113-
print("Not ready yet, retrying.")
114-
time.sleep(0.5)
115-
else:
116-
print("Database could not be found, exiting.")
117-
sys.exit(1)
118-
11983
@staticmethod
12084
def set_dev_site_name() -> None:
12185
"""Set the development site domain in admin from default example."""
@@ -133,9 +97,6 @@ def prepare_server(self) -> None:
13397
"""Perform preparation tasks before running the server."""
13498
django.setup()
13599

136-
if self.debug:
137-
self.wait_for_postgres()
138-
139100
print("Applying migrations.")
140101
call_command("migrate", verbosity=self.verbosity)
141102

@@ -176,12 +137,10 @@ def run_server(self) -> None:
176137
"--preload",
177138
"-b", "0.0.0.0:8000",
178139
"pydis_site.wsgi:application",
179-
"--threads", "8",
180140
"-w", "2",
181-
"--max-requests", "1000",
182-
"--max-requests-jitter", "50",
183141
"--statsd-host", "graphite.default.svc.cluster.local:8125",
184142
"--statsd-prefix", "site",
143+
"--config", "file:gunicorn.conf.py"
185144
]
186145

187146
# Run gunicorn for the production server.

0 commit comments

Comments
 (0)