|
| 1 | +import contextlib |
1 | 2 | import logging |
2 | 3 | import os |
3 | 4 | import sys |
4 | 5 | import tempfile |
| 6 | +import typing |
5 | 7 |
|
6 | 8 | from django.core.management import execute_from_command_line |
7 | 9 |
|
8 | 10 | logger = logging.getLogger(__name__) |
9 | 11 |
|
10 | 12 |
|
11 | | -def main() -> None: |
| 13 | +@contextlib.contextmanager |
| 14 | +def ensure_cli_env() -> typing.Generator[None, None, None]: |
12 | 15 | """ |
13 | | - The main entry point to the Flagsmith application. |
| 16 | + Set up the environment for the main entry point of the application |
| 17 | + and clean up after it's done. |
14 | 18 |
|
15 | | - An equivalent to Django's `manage.py` script, this module is used to run management commands. |
| 19 | + Add environment-related code that needs to happen before and after Django is involved |
| 20 | + to here. |
16 | 21 |
|
17 | | - It's installed as the `flagsmith` command. |
| 22 | + Use as a context manager, e.g.: |
18 | 23 |
|
19 | | - Everything that needs to be run before Django is started should be done here. |
| 24 | + ```python |
| 25 | + with ensure_cli_env(): |
| 26 | + main() |
| 27 | + ``` |
| 28 | + """ |
| 29 | + ctx = contextlib.ExitStack() |
20 | 30 |
|
21 | | - The end goal is to eventually replace Core API's `run-docker.sh` with this. |
| 31 | + # TODO @khvn26 Move logging setup to here |
22 | 32 |
|
23 | | - Usage: |
24 | | - `flagsmith <command> [options]` |
25 | | - """ |
| 33 | + # Currently we don't install Flagsmith modules as a package, so we need to add |
| 34 | + # $CWD to the Python path to be able to import them |
| 35 | + sys.path.append(os.getcwd()) |
| 36 | + |
| 37 | + # TODO @khvn26 We should find a better way to pre-set the Django settings module |
| 38 | + # without resorting to it being set outside of the application |
26 | 39 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.dev") |
27 | 40 |
|
28 | 41 | # Set up Prometheus' multiprocess mode |
29 | 42 | if "PROMETHEUS_MULTIPROC_DIR" not in os.environ: |
30 | | - prometheus_multiproc_dir = tempfile.TemporaryDirectory( |
31 | | - prefix="prometheus_multiproc", |
| 43 | + prometheus_multiproc_dir_name = ctx.enter_context( |
| 44 | + tempfile.TemporaryDirectory( |
| 45 | + prefix="prometheus_multiproc", |
| 46 | + ) |
32 | 47 | ) |
| 48 | + |
33 | 49 | logger.info( |
34 | 50 | "Created %s for Prometheus multi-process mode", |
35 | | - prometheus_multiproc_dir.name, |
| 51 | + prometheus_multiproc_dir_name, |
36 | 52 | ) |
37 | | - os.environ["PROMETHEUS_MULTIPROC_DIR"] = prometheus_multiproc_dir.name |
| 53 | + os.environ["PROMETHEUS_MULTIPROC_DIR"] = prometheus_multiproc_dir_name |
| 54 | + |
| 55 | + if "task-processor" in sys.argv: |
| 56 | + # A hacky way to signal we're not running the API |
| 57 | + os.environ["RUN_BY_PROCESSOR"] = "true" |
| 58 | + |
| 59 | + with ctx: |
| 60 | + yield |
| 61 | + |
38 | 62 |
|
39 | | - # Run Django |
40 | | - execute_from_command_line(sys.argv) |
| 63 | +def main() -> None: |
| 64 | + """ |
| 65 | + The main entry point to the Flagsmith application. |
| 66 | +
|
| 67 | + An equivalent to Django's `manage.py` script, this module is used to run management commands. |
| 68 | +
|
| 69 | + It's installed as the `flagsmith` command. |
| 70 | +
|
| 71 | + Everything that needs to be run before Django is started should be done here. |
| 72 | +
|
| 73 | + The end goal is to eventually replace Core API's `run-docker.sh` with this. |
| 74 | +
|
| 75 | + Usage: |
| 76 | + `flagsmith <command> [options]` |
| 77 | + """ |
| 78 | + with ensure_cli_env(): |
| 79 | + # Run Django |
| 80 | + execute_from_command_line(sys.argv) |
0 commit comments