Files
mtusi2026lab5/app/core/job_admin.py
T

83 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Операторский интерфейс failed registry. Доступ требует credentials стенда."""
import argparse
import asyncio
import json
from uuid import UUID
from app.core.background import log_background_error
from app.core.config import Settings
from app.core.database import create_db_engine, create_session_factory
from app.core.jobs import JobSettings
from app.domain.job_admin import (
REPLAY_REASONS,
history,
list_failed,
replay_failed_job,
show_job,
)
from app.errors.exceptions import JobOperationError
from app.storage.s3 import create_s3_storage
async def execute(args):
settings = Settings()
engine = create_db_engine(settings)
storage = None
try:
factory = create_session_factory(engine)
async with factory() as session:
if args.command == "list-failed":
result = await list_failed(session, args.limit)
elif args.command == "show":
result = await show_job(session, args.job_id)
elif args.command == "history":
result = await history(session, args.job_id)
else:
storage = create_s3_storage(settings.s3)
result = await replay_failed_job(
session,
storage,
JobSettings(),
args.job_id,
request_id=args.request_id,
actor=args.actor,
reason=args.reason,
)
print(json.dumps(result, default=str), flush=True)
finally:
if storage is not None:
await storage.aclose()
await engine.dispose()
def main():
parser = argparse.ArgumentParser(
description="Реестр failed и явный replay с аудитом"
)
commands = parser.add_subparsers(dest="command", required=True)
listing = commands.add_parser("list-failed")
listing.add_argument(
"--limit", type=int, choices=range(1, 501), default=50, metavar="1..500"
)
for name in ("show", "history", "replay"):
command = commands.add_parser(name)
command.add_argument("job_id", type=UUID)
if name == "replay":
command.add_argument("--request-id", type=UUID, required=True)
command.add_argument("--actor", required=True)
command.add_argument("--reason", choices=REPLAY_REASONS, required=True)
args = parser.parse_args()
try:
asyncio.run(execute(args))
except JobOperationError as error:
print(json.dumps({"error_code": error.code}), flush=True)
raise SystemExit(2) from None
except Exception as error:
log_background_error("Job administration failed", error)
raise SystemExit(1) from None
if __name__ == "__main__":
main()