92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
|
|
from app.config import get_settings
|
|
from app.database import SessionLocal
|
|
from app.services.watchdog_service import (
|
|
local_convert_health_summary,
|
|
local_sync_health_summary,
|
|
recover_stale_converts,
|
|
recover_stale_syncs,
|
|
)
|
|
|
|
|
|
def build_parser() -> argparse.ArgumentParser:
|
|
parser = argparse.ArgumentParser(description="Document Cleaning watchdog helpers")
|
|
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
|
|
summary = subparsers.add_parser("summary", help="Print local sync health summary")
|
|
summary.add_argument("--timeout-seconds", type=int, default=None)
|
|
|
|
convert_summary = subparsers.add_parser("convert-summary", help="Print local convert health summary")
|
|
convert_summary.add_argument("--timeout-seconds", type=int, default=None)
|
|
convert_summary.add_argument("--orphan-grace-seconds", type=int, default=None)
|
|
|
|
recover = subparsers.add_parser("recover-stale-syncs", help="Recover stale syncing records")
|
|
recover.add_argument("--timeout-seconds", type=int, default=None)
|
|
recover.add_argument(
|
|
"--reason-prefix",
|
|
default="Marked stale by watchdog after timeout.",
|
|
)
|
|
|
|
recover_convert = subparsers.add_parser("recover-stale-converts", help="Recover stale convert records")
|
|
recover_convert.add_argument("--timeout-seconds", type=int, default=None)
|
|
recover_convert.add_argument("--orphan-grace-seconds", type=int, default=None)
|
|
recover_convert.add_argument(
|
|
"--reason-prefix",
|
|
default="Marked convert as failed by watchdog.",
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
def main() -> None:
|
|
parser = build_parser()
|
|
args = parser.parse_args()
|
|
settings = get_settings()
|
|
|
|
db = SessionLocal()
|
|
try:
|
|
if args.command == "summary":
|
|
timeout_seconds = args.timeout_seconds or settings.watchdog_stale_sync_timeout_seconds
|
|
payload = local_sync_health_summary(db, timeout_seconds=timeout_seconds)
|
|
elif args.command == "convert-summary":
|
|
timeout_seconds = args.timeout_seconds or settings.watchdog_stale_convert_timeout_seconds
|
|
orphan_grace_seconds = args.orphan_grace_seconds or settings.watchdog_convert_orphan_grace_seconds
|
|
payload = local_convert_health_summary(
|
|
db,
|
|
timeout_seconds=timeout_seconds,
|
|
orphan_grace_seconds=orphan_grace_seconds,
|
|
settings=settings,
|
|
)
|
|
elif args.command == "recover-stale-syncs":
|
|
timeout_seconds = args.timeout_seconds or settings.watchdog_stale_sync_timeout_seconds
|
|
payload = recover_stale_syncs(
|
|
db,
|
|
timeout_seconds=timeout_seconds,
|
|
reason_prefix=args.reason_prefix,
|
|
)
|
|
elif args.command == "recover-stale-converts":
|
|
timeout_seconds = args.timeout_seconds or settings.watchdog_stale_convert_timeout_seconds
|
|
orphan_grace_seconds = args.orphan_grace_seconds or settings.watchdog_convert_orphan_grace_seconds
|
|
payload = recover_stale_converts(
|
|
db,
|
|
timeout_seconds=timeout_seconds,
|
|
orphan_grace_seconds=orphan_grace_seconds,
|
|
reason_prefix=args.reason_prefix,
|
|
settings=settings,
|
|
)
|
|
else:
|
|
parser.error(f"Unsupported command: {args.command}")
|
|
return
|
|
finally:
|
|
db.close()
|
|
|
|
print(json.dumps(payload, ensure_ascii=False, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|