|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | + |
| 4 | +""" |
| 5 | +InstanceConfig: builds and writes the config.yaml consumed by |
| 6 | +the FunctionStream binary via FUNCTION_STREAM_CONF. |
| 7 | +""" |
| 8 | + |
| 9 | +from pathlib import Path |
| 10 | +from typing import Any, Dict |
| 11 | + |
| 12 | +import yaml |
| 13 | + |
| 14 | +from .workspace import InstanceWorkspace |
| 15 | + |
| 16 | + |
| 17 | +class InstanceConfig: |
| 18 | + """Generates and persists config.yaml for one FunctionStream instance.""" |
| 19 | + |
| 20 | + def __init__(self, host: str, port: int, workspace: InstanceWorkspace): |
| 21 | + self._workspace = workspace |
| 22 | + self._config: Dict[str, Any] = { |
| 23 | + "service": { |
| 24 | + "service_id": f"it-{port}", |
| 25 | + "service_name": "function-stream", |
| 26 | + "version": "1.0.0", |
| 27 | + "host": host, |
| 28 | + "port": port, |
| 29 | + "debug": False, |
| 30 | + }, |
| 31 | + "logging": { |
| 32 | + "level": "info", |
| 33 | + "format": "json", |
| 34 | + "file_path": str(workspace.log_dir / "app.log"), |
| 35 | + "max_file_size": 50, |
| 36 | + "max_files": 3, |
| 37 | + }, |
| 38 | + "state_storage": { |
| 39 | + "storage_type": "memory", |
| 40 | + }, |
| 41 | + "task_storage": { |
| 42 | + "storage_type": "rocksdb", |
| 43 | + "db_path": str(workspace.data_dir / "task"), |
| 44 | + }, |
| 45 | + "stream_catalog": { |
| 46 | + "persist": True, |
| 47 | + "db_path": str(workspace.data_dir / "stream_catalog"), |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + @property |
| 52 | + def raw(self) -> Dict[str, Any]: |
| 53 | + return self._config |
| 54 | + |
| 55 | + def override(self, overrides: Dict[str, Any]) -> None: |
| 56 | + """ |
| 57 | + Apply overrides using dot-separated keys. |
| 58 | + Example: {"service.debug": True, "logging.level": "debug"} |
| 59 | + """ |
| 60 | + for dotted_key, value in overrides.items(): |
| 61 | + keys = dotted_key.split(".") |
| 62 | + target = self._config |
| 63 | + for k in keys[:-1]: |
| 64 | + target = target.setdefault(k, {}) |
| 65 | + target[keys[-1]] = value |
| 66 | + |
| 67 | + def write_to_workspace(self) -> Path: |
| 68 | + """Serialize config to the workspace config.yaml and return its path.""" |
| 69 | + with open(self._workspace.config_file, "w", encoding="utf-8") as f: |
| 70 | + yaml.dump(self._config, f, default_flow_style=False, sort_keys=False) |
| 71 | + return self._workspace.config_file |
0 commit comments