Files
mtusi2026lab5/src/operator.py
T

48 lines
1.9 KiB
Python

"""Локальные операции над реестром заданий."""
import argparse
import json
from uuid import UUID
from botocore.exceptions import BotoCoreError, ClientError
from src import jobs
from src.storage import storage, timestamp
def main():
parser = argparse.ArgumentParser()
parser.add_argument('action', choices=('failed', 'replay', 'upgrade'))
parser.add_argument('attachment_id', type=UUID, nargs='?')
parser.add_argument('--version', type=int, default=2)
args = parser.parse_args()
if args.action in ('replay', 'upgrade') and args.attachment_id is None:
parser.error('operation requires attachment_id')
storage.pool.open(wait=True)
try:
if args.action in ('replay', 'upgrade'):
try:
if args.action == 'replay':
result = jobs.replay(args.attachment_id)
else:
job_id = jobs.upgrade(args.attachment_id, args.version)
result = (job_id, 1) if job_id else None
except (BotoCoreError, ClientError):
raise SystemExit('Источник обработки недоступен') from None
if result is None:
raise SystemExit('Операция неприменима к текущему состоянию или версии')
print(json.dumps({'job_id': str(result[0]), 'attempt': result[1]}))
else:
with storage.pool.connection() as conn:
rows = conn.execute(
"""SELECT id, attachment_id, pipeline_version, attempt, last_error, settled_at
FROM attachment_jobs WHERE state='failed' ORDER BY settled_at DESC NULLS LAST LIMIT 100"""
).fetchall()
print(json.dumps(rows, default=lambda value: str(value) if isinstance(value, UUID) else timestamp(value)))
finally:
storage.pool.close()
if __name__ == '__main__':
main()