|
11 | 11 | from pydantic import BaseModel |
12 | 12 | from rich.console import Console |
13 | 13 | from rich.table import Table |
| 14 | +from typing_extensions import Annotated, Doc |
14 | 15 | from uvicorn.importer import import_from_string |
15 | 16 |
|
16 | 17 | from fluid.utils import log as log_ |
@@ -38,19 +39,43 @@ class TaskManagerCLI(LazyGroup): |
38 | 39 | def __init__( |
39 | 40 | self, |
40 | 41 | task_manager_app: TaskManagerApp, |
| 42 | + log_config: dict | None = None, |
41 | 43 | **kwargs: Any, |
42 | 44 | ): |
43 | 45 | kwargs.setdefault("commands", DEFAULT_COMMANDS) |
44 | 46 | super().__init__(**kwargs) |
45 | | - self.task_manager_app = task_manager_app |
| 47 | + self.task_manager_app: Annotated[ |
| 48 | + TaskManagerApp, |
| 49 | + Doc( |
| 50 | + """ |
| 51 | + Task manager application. |
| 52 | +
|
| 53 | + This can be a FastAPI app, a callable that returns a FastAPI app, |
| 54 | + or a string import path to a FastAPI app. |
| 55 | + """ |
| 56 | + ), |
| 57 | + ] = task_manager_app |
| 58 | + self.log_config: Annotated[ |
| 59 | + dict, |
| 60 | + Doc( |
| 61 | + """ |
| 62 | + Log configuration parameters. |
| 63 | +
|
| 64 | + These parameters are passed to the log_config argument of |
| 65 | + `fluid.utils.log.config()`. |
| 66 | + """ |
| 67 | + ), |
| 68 | + ] = ( |
| 69 | + log_config or {} |
| 70 | + ) |
46 | 71 |
|
47 | 72 |
|
48 | | -def ctx_task_manager_app(ctx: click.Context) -> TaskManagerApp: |
49 | | - return ctx.parent.command.task_manager_app # type: ignore |
| 73 | +def ctx_task_manager_cli(ctx: click.Context) -> TaskManagerCLI: |
| 74 | + return ctx.parent.command # type: ignore |
50 | 75 |
|
51 | 76 |
|
52 | 77 | def ctx_app(ctx: click.Context) -> FastAPI: |
53 | | - app = ctx_task_manager_app(ctx) # type: ignore |
| 78 | + app = ctx_task_manager_cli(ctx).task_manager_app |
54 | 79 | if isinstance(app, str): |
55 | 80 | return import_from_string(app)() |
56 | 81 | elif isinstance(app, FastAPI): |
@@ -81,7 +106,8 @@ def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None |
81 | 106 | @from_pydantic(task.params_model) |
82 | 107 | def execute_task(log: bool, run_id: str, params: str, **extra: Any) -> None: |
83 | 108 | if log: |
84 | | - log_.config() |
| 109 | + log_config = ctx_task_manager_cli(ctx).log_config |
| 110 | + log_.config(**log_config) |
85 | 111 | kwargs = json.loads(params or "{}") |
86 | 112 | for value in extra.values(): |
87 | 113 | if isinstance(value, BaseModel): |
@@ -132,14 +158,14 @@ def ls(ctx: click.Context) -> None: |
132 | 158 | @click.pass_context |
133 | 159 | def serve(ctx: click.Context, host: str, port: int, reload: bool) -> None: |
134 | 160 | """Run the service""" |
135 | | - task_manager_app = ctx_task_manager_app(ctx) |
| 161 | + cli = ctx_task_manager_cli(ctx) |
136 | 162 | uvicorn.run( |
137 | | - task_manager_app, |
| 163 | + cli.task_manager_app, |
138 | 164 | port=port, |
139 | 165 | host=host, |
140 | 166 | log_level="info", |
141 | 167 | reload=reload, |
142 | | - log_config=log_.config(), |
| 168 | + log_config=log_.config(**cli.log_config), |
143 | 169 | ) |
144 | 170 |
|
145 | 171 |
|
|
0 commit comments