~homeworkprod/byceps

721b4db4e1cfe65e0a1e0c0c459611e83b34fa6c — Jochen Kupperschmidt 4 years ago 7b8797a
Extract function to retrieve the configuration filename from the environment that exits if that fails
3 files changed, 17 insertions(+), 20 deletions(-)

M byceps/util/system.py
M serve.py
M worker.py
M byceps/util/system.py => byceps/util/system.py +13 -0
@@ 9,6 9,7 @@ byceps.util.system
"""

import os
import sys


CONFIG_VAR_NAME = 'BYCEPS_CONFIG'


@@ 27,3 28,15 @@ def get_config_filename_from_env():
            "environment variable.".format(CONFIG_VAR_NAME))

    return env


def get_config_filename_from_env_or_exit():
    """Return the configuration filename set via environment variable.

    Exit if it isn't set.
    """
    try:
        return get_config_filename_from_env()
    except Exception as e:
        sys.stderr.write("{}\n".format(e))
        sys.exit()

M serve.py => serve.py +2 -8
@@ 8,17 8,11 @@ an environment variable.
:License: Modified BSD, see LICENSE for details.
"""

import sys

from byceps.application import create_app, init_app
from byceps.util.system import get_config_filename_from_env
from byceps.util.system import get_config_filename_from_env_or_exit


try:
    config_filename = get_config_filename_from_env()
except Exception as e:
    sys.stderr.write("{}\n".format(e))
    sys.exit()
config_filename = get_config_filename_from_env_or_exit()

app = create_app(config_filename)
init_app(app)

M worker.py => worker.py +2 -12
@@ 7,26 7,16 @@
:License: Modified BSD, see LICENSE for details.
"""

import sys

from rq import Worker

from byceps.util.jobqueue import connection, get_queue
from byceps.util.system import get_config_filename_from_env
from byceps.util.system import get_config_filename_from_env_or_exit

from bootstrap.util import app_context


def get_config_filename():
    try:
        return get_config_filename_from_env()
    except Exception as e:
        sys.stderr.write(str(e) + '\n')
        sys.exit()


if __name__ == '__main__':
    config_filename = get_config_filename()
    config_filename = get_config_filename_from_env_or_exit()

    with app_context(config_filename):
        with connection():