Skip to content

Commit

Permalink
Merge branch 'main' into feat/timeline-from-yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
wookie184 authored Jul 18, 2024
2 parents 6cd6dfd + fec87a6 commit 10f3f6a
Show file tree
Hide file tree
Showing 23 changed files with 552 additions and 317 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
# Build the container, including an inline cache manifest to
# allow us to use the registry as a cache source.
- name: Build and push
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/sentry-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Create a Sentry.io release
uses: tclindner/sentry-releases-action@v1.2.0
uses: getsentry/action-release@v1
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: python-discord
SENTRY_PROJECT: site
with:
tagName: ${{ github.sha }}
environment: production
releaseNamePrefix: site@
version_prefix: site@
4 changes: 2 additions & 2 deletions .github/workflows/static-preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
# Build the container, including an inline cache manifest to
# allow us to use the registry as a cache source.
- name: Build Docker Image (Main)
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
if: github.ref == 'refs/heads/main'
with:
context: .
Expand All @@ -53,7 +53,7 @@ jobs:
# Build directly to a local folder
- name: Build Docker Image (PR)
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
if: github.ref != 'refs/heads/main'
with:
context: .
Expand Down
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ RUN if [ $STATIC_BUILD = "TRUE" ] ; \
then SECRET_KEY=dummy_value poetry run python manage.py distill-local build --traceback --force ; \
fi

# Run web server through custom manager
ENTRYPOINT ["poetry", "run", "python", "manage.py"]
CMD ["run"]
ENTRYPOINT ["poetry", "run"]
CMD ["gunicorn", "--preload", "-b", "0.0.0.0:8000", \
"pydis_site.wsgi:application", "-w", "2", "--statsd-host", \
"graphite.default.svc.cluster.local:8125", "--statsd-prefix", "site", \
"--config", "file:gunicorn.conf.py"]
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:
build:
context: .
dockerfile: Dockerfile
command: ["run", "--debug"]
command: ["python", "manage.py", "run"]
networks:
default:
aliases:
Expand Down
65 changes: 21 additions & 44 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,30 @@

class SiteManager:
"""
Manages the preparation and serving of the website.
Manages the preparation and serving of the website for local use.
Handles both development and production environments.
This class is used solely for setting up the development
environment. In production, gunicorn is invoked directly
and migrations are handled in an init container.
Usage:
manage.py run [option]...
Options:
--debug Runs a development server with debug mode enabled.
--silent Sets minimal console output.
--verbose Sets verbose console output.
"""

def __init__(self, args: list[str]):
self.debug = "--debug" in args
self.silent = "--silent" in args

if self.silent:
self.verbosity = 0
else:
self.verbosity = 2 if "--verbose" in args else 1

if self.debug:
os.environ.setdefault("DEBUG", "true")
print("Starting in debug mode.")
os.environ.setdefault("DEBUG", "true")
print("Starting in debug mode.")

@staticmethod
def create_superuser() -> None:
Expand Down Expand Up @@ -104,53 +103,31 @@ def prepare_environment(self) -> None:
call_command("migrate", verbosity=self.verbosity)

def prepare_server(self) -> None:
"""Preform runserver-specific preparation tasks."""
if self.debug:
# In Production, collectstatic is ran in the Docker image
print("Collecting static files.")
call_command(
"collectstatic",
interactive=False,
clear=True,
verbosity=self.verbosity - 1
)
"""Perform debug runserver-specific preparation tasks."""
print("Collecting static files.")
call_command(
"collectstatic",
interactive=False,
clear=True,
verbosity=self.verbosity - 1
)

self.set_dev_site_name()
self.create_superuser()
self.set_dev_site_name()
self.create_superuser()

def run_server(self) -> None:
"""Prepare and run the web server."""
def run_debug(self) -> None:
"""Prepare and run the debug web server."""
in_reloader = os.environ.get('RUN_MAIN') == 'true'

# Prevent preparing twice when in dev mode due to reloader
if not self.debug or in_reloader:
if in_reloader:
self.prepare_environment()
self.prepare_server()

print("Starting server.")

# Run the development server
if self.debug:
call_command("runserver", "0.0.0.0:8000")
return

# Import gunicorn only if we aren't in debug mode.
import gunicorn.app.wsgiapp

# Patch the arguments for gunicorn
sys.argv = [
"gunicorn",
"--preload",
"-b", "0.0.0.0:8000",
"pydis_site.wsgi:application",
"-w", "2",
"--statsd-host", "graphite.default.svc.cluster.local:8125",
"--statsd-prefix", "site",
"--config", "file:gunicorn.conf.py"
]

# Run gunicorn for the production server.
gunicorn.app.wsgiapp.run()
call_command("runserver", "0.0.0.0:8000")

def run_tests(self) -> None:
"""Prepare and run the test suite."""
Expand Down Expand Up @@ -190,7 +167,7 @@ def main() -> None:
if len(sys.argv) > 1 and sys.argv[1] in ("run", "test"):
manager = SiteManager(sys.argv)
if sys.argv[1] == "run":
manager.run_server()
manager.run_debug()
elif sys.argv[1] == "test":
manager.run_tests()

Expand Down
Loading

0 comments on commit 10f3f6a

Please sign in to comment.