Files
InterferenceETL/etl_app/worker.py
T

70 lines
2.7 KiB
Python

from __future__ import annotations
import argparse
import sys
import traceback
from pathlib import Path
import main
from .config import Settings, SettingsStore
from .connectors import build_client, build_storage, build_store, close_quietly
def run(settings_path: Path, output_dir: Path, dry_run: bool, cache_dir: Path | None = None) -> int:
settings = SettingsStore(settings_path).get()
print(f"storage_mode={settings.storage_mode}")
print(f"database_mode={'none' if dry_run else settings.database_mode}")
client = build_client(settings)
storage = build_storage(settings, client)
try:
source = main.StorageSource(storage, settings.source_root, settings.cell_data_directories)
store = None if dry_run else build_store(settings, client)
history = None
if store is not None and settings.history_enabled:
history = main.StorageHistoryStore(storage, settings.history_root)
cell_cache = main.CellDataCache(cache_dir) if cache_dir is not None else None
output = main.process(
source, output_dir, settings.lookback_days, store=store, history=history, cell_cache=cell_cache
)
if output is not None and not dry_run:
prune_history(storage, settings)
finally:
close_quietly(storage)
return 0
def prune_history(storage: main.FileStorage, settings: Settings) -> None:
"""Apply history retention after a new group was stored. The data is already saved, so a failure is only reported."""
if settings.history_keep_days <= 0:
return
try:
removed = main.StorageHistoryStore(storage, settings.history_root).prune(settings.history_keep_days)
except Exception as exc:
print(f"history_prune_error={type(exc).__name__}: {exc}")
return
for path in removed:
print(f"history_removed={path}")
def entry(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Run one InterferenceETL pass with the saved app settings")
parser.add_argument("--settings", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--cache", type=Path, help="directory for parsed CellData workbooks")
parser.add_argument("--dry-run", action="store_true")
args = parser.parse_args(argv)
try:
return run(args.settings, args.output, args.dry_run, args.cache)
except main.ProcessingError as exc:
print(f"error={exc}", file=sys.stderr, flush=True)
return 1
except Exception as exc:
traceback.print_exc()
print(f"error={type(exc).__name__}: {exc}", file=sys.stderr, flush=True)
return 1
if __name__ == "__main__":
raise SystemExit(entry())