Skip to content

Commit

Permalink
chore: isolate the async app from the HTTP app as well as create the …
Browse files Browse the repository at this point in the history
…`WorkerBackend.MULTIPROCESSING` async backend
  • Loading branch information
ClemDoum committed Jan 30, 2024
1 parent 42f7775 commit 7636a26
Show file tree
Hide file tree
Showing 43 changed files with 989 additions and 658 deletions.
2 changes: 2 additions & 0 deletions neo4j-app/neo4j_app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path
from neo4j_app.config import AppConfig


ROOT_DIR = Path(__file__).parent
1 change: 1 addition & 0 deletions neo4j-app/neo4j_app/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .config import ServiceConfig
6 changes: 3 additions & 3 deletions neo4j-app/neo4j_app/app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
from fastapi import APIRouter, Request

from neo4j_app.app.dependencies import (
lifespan_es_client,
lifespan_neo4j_driver,
)
from neo4j_app.app.doc import (
ADMIN_TAG,
DOC_NEO4J_CSV,
DOC_NEO4J_CSV_DESC,
)
from neo4j_app.core import AppConfig
from neo4j_app.app import ServiceConfig
from neo4j_app.core.imports import to_neo4j_csvs
from neo4j_app.core.objects import (
Neo4jCSVRequest,
Neo4jCSVResponse,
)
from neo4j_app.core.utils.logging import log_elapsed_time_cm
from neo4j_app.tasks.dependencies import lifespan_es_client

logger = logging.getLogger(__name__)

Expand All @@ -37,7 +37,7 @@ def admin_router() -> APIRouter:
async def _neo4j_csv(
project: str, payload: Neo4jCSVRequest, request: Request
) -> Neo4jCSVResponse:
config: AppConfig = request.app.state.config
config: ServiceConfig = request.app.state.config

with log_elapsed_time_cm(
logger, logging.INFO, "Exported ES to CSV in {elapsed_time} !"
Expand Down
81 changes: 81 additions & 0 deletions neo4j-app/neo4j_app/app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from __future__ import annotations

import configparser
import functools
import io
from copy import copy
from typing import Optional, TextIO

from pydantic import Field

from neo4j_app import AppConfig
from neo4j_app.core.utils.pydantic import (
BaseICIJModel,
)
from neo4j_app.icij_worker import WorkerConfig, WorkerType

_SHARED_WITH_NEO4J_WORKER_CONFIG = [
"neo4j_connection_timeout",
"neo4j_host",
"neo4j_password",
"neo4j_port",
"neo4j_uri_scheme",
"neo4j_user",
]

_SHARED_WITH_NEO4J_WORKER_CONFIG_PREFIXED = [
"cancelled_tasks_refresh_interval_s",
"task_queue_poll_interval_s",
"log_level",
]


class ServiceConfig(AppConfig):
neo4j_app_async_dependencies: Optional[str] = "neo4j_app.tasks.WORKER_LIFESPAN_DEPS"
neo4j_app_async_app: str = "neo4j_app.tasks.app"
neo4j_app_gunicorn_workers: int = 1
neo4j_app_host: str = "127.0.0.1"
neo4j_app_n_async_workers: int = 1
neo4j_app_name: str = "🕸 neo4j app"
neo4j_app_port: int = 8080
neo4j_app_task_queue_size: int = 2
neo4j_app_worker_type: WorkerType = WorkerType.neo4j
test: bool = False

@functools.cached_property
def doc_app_name(self) -> str:
return self.neo4j_app_name

def to_worker_config(self, **kwargs) -> WorkerConfig:
kwargs = copy(kwargs)
for suffix in _SHARED_WITH_NEO4J_WORKER_CONFIG_PREFIXED:
kwargs[suffix] = getattr(self, f"neo4j_app_{suffix}")

if self.test:
from neo4j_app.tests.icij_worker.conftest import MockWorkerConfig

return MockWorkerConfig(**kwargs)
from neo4j_app.icij_worker.worker.neo4j import Neo4jWorkerConfig

for k in _SHARED_WITH_NEO4J_WORKER_CONFIG:
if k in kwargs:
continue
kwargs[k] = getattr(self, k)
return Neo4jWorkerConfig(**kwargs)

def write_java_properties(self, file: TextIO):
parser = self._get_config_parser()
parser[configparser.DEFAULTSECT] = dict(
sorted(self.dict(exclude_unset=True, by_alias=True).items())
)
config_str_io = io.StringIO()
parser.write(config_str_io, space_around_delimiters=False)
config_str = config_str_io.getvalue()
# Remove the mandatory default section
config_str = config_str.replace(f"[{configparser.DEFAULTSECT}]\n", "")
file.write(config_str)


class UviCornModel(BaseICIJModel):
host: str = Field(default="127.0.0.1", const=True)
port: int
Loading

0 comments on commit 7636a26

Please sign in to comment.