Skip to content

Commit

Permalink
runners: ensure run_server loads gunicorn config; print common status
Browse files Browse the repository at this point in the history
  • Loading branch information
mik3y committed Jul 24, 2022
1 parent ba2d8b9 commit 3076dfe
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 46 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ RUN DATABASE_URL=mysql:// \
ARG GIT_SHORT_SHA="unknown"
ARG VERSION="unknown"
ARG BUILD_DATE="unknown"
RUN echo "GIT_SHORT_SHA=${GIT_SHORT_SHA}" \
&& echo "VERSION=${VERSION}" \
&& echo "BUILD_DATE=${BUILD_DATE}" /etc/kegbot-version
RUN echo "GIT_SHORT_SHA=${GIT_SHORT_SHA}\nVERSION=${VERSION}\nBUILD_DATE=${BUILD_DATE}" > /etc/kegbot-version

VOLUME ["/kegbot-data"]

Expand Down
3 changes: 1 addition & 2 deletions docs/source/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ configuration when needed.

.. data:: run_server

Runs the internal web server process. See also ``run_all``.
Runs the internal web server process.

.. data:: run_workers

Expand All @@ -62,4 +62,3 @@ configuration when needed.
Runs both the web service process (``run_server``) and the worker process
(``run_workers``).


19 changes: 6 additions & 13 deletions pykeg/core/management/commands/run_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,21 @@


class Command(RunnerCommand):
help = "Runs background task queue workers."
help = "Performs `run_server` and `run_workers`"
pidfile_name = "kegbot_run_all.pid"

def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
"--gunicorn_options",
action="store",
dest="gunicorn_options",
default="-w 3",
help="Specifies extra options to pass to gunicorn.",
)

def get_commands(self, options):
ret = []
logs_dir = options.get("logs_dir")

server_command = "kegbot run_server"
if logs_dir:
server_command += " --logs_dir={}".format(logs_dir)
ret.append(("server", server_command))

workers_command = "kegbot run_workers"
if logs_dir:
workers_command += " --logs_dir={}".format(logs_dir)
ret.append(("workers", workers_command))

extra_options = options.get("gunicorn_options", "")
ret.append(("guincorn", "gunicorn pykeg.web.wsgi:application " + extra_options))
return ret
7 changes: 6 additions & 1 deletion pykeg/core/management/commands/run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ def add_arguments(self, parser):
def get_commands(self, options):
ret = []
extra_options = options.get("gunicorn_options", "")
ret.append(("guincorn", "gunicorn pykeg.web.wsgi:application " + extra_options))
command_line = (
"gunicorn pykeg.web.wsgi:application "
"--config=python:pykeg.web.gunicorn_conf "
f"{extra_options}"
).strip()
ret.append(("guincorn", command_line))
return ret
19 changes: 17 additions & 2 deletions pykeg/util/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@
import subprocess
import sys
import time
from builtins import object
from importlib import metadata as importlib_metadata

from pykeg.core.util import get_runtime_version_info

logger = logging.getLogger(__name__)

POLL_INTERVAL_SECONDS = 1.0


class Runner(object):
def get_version():
try:
return importlib_metadata.version("kegbot")
except importlib_metadata.PackageNotFoundError:
return "0.0.0"


class Runner:
"""Runs several commands together as a process group, acting as a watchdog
while running.
Expand All @@ -31,6 +40,11 @@ def __init__(self):
self.running = False
self.logger = logger

def print_startup_line(self):
runtime_info = " ".join([f"{k}={v}" for k, v in get_runtime_version_info().items()])
runtime_info = f"server_version={get_version()} " + runtime_info
logger.info(f"{sys.argv[0]} {sys.argv[1]}: starting, {runtime_info.strip()}")

def is_running(self):
return self.running

Expand All @@ -44,6 +58,7 @@ def run(self):
"""Launches all commands, watching their pids."""
assert not self.is_running(), "Already running!"
self.running = True
self.print_startup_line()

self.logger.info("Starting commands from pid={}".format(os.getpid()))
dev_null_name = getattr(os, "devnull", "/dev/null")
Expand Down
25 changes: 0 additions & 25 deletions pykeg/web/gunicorn_conf.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import multiprocessing
import os
import sys
from importlib import metadata as importlib_metadata

BANNER = """
██╗ ██╗███████╗ ██████╗ ██████╗ ██████╗ ████████╗
██║ ██╔╝██╔════╝██╔════╝ ██╔══██╗██╔═══██╗╚══██╔══╝
█████╔╝ █████╗ ██║ ███╗██████╔╝██║ ██║ ██║
██╔═██╗ ██╔══╝ ██║ ██║██╔══██╗██║ ██║ ██║
██║ ██╗███████╗╚██████╔╝██████╔╝╚██████╔╝ ██║
╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝
""".strip()


def get_version():
try:
return importlib_metadata.version("kegbot")
except importlib_metadata.PackageNotFoundError:
return "0.0.0"


if os.getenv("KEGBOT_IN_HEROKU") and os.getenv("PORT"):
# Necessary on Heroku, which doesn't respect EXPOSE :-\
Expand All @@ -28,9 +9,3 @@ def get_version():

worker_class = "gevent"
workers = multiprocessing.cpu_count() * 2 + 1

print(BANNER, file=sys.stderr)
print("kegbot-server - version {}".format(get_version()), file=sys.stderr)
print(" homepage: https://kegbot.org", file=sys.stderr)
print(" discuss: https://forum.kegbot.org", file=sys.stderr)
print("-" * 80, file=sys.stderr)

0 comments on commit 3076dfe

Please sign in to comment.