"""Explicit local operator actions, never exposed as a public HTTP endpoint.""" import argparse import json from messenger.pipeline import Pipeline from messenger.repository import PostgresRepository from messenger.settings import Settings from messenger.storage import ObjectStore def main(): parser = argparse.ArgumentParser() parser.add_argument("action", choices=("failed", "replay", "new-version")) parser.add_argument("attachment_id", nargs="?") args = parser.parse_args() settings = Settings.from_env() db = PostgresRepository(settings.database_url, pool_size=2) db.open() try: if args.action == "failed": with db.connection() as conn: rows = conn.execute( "SELECT job_id,attachment_id,pipeline_version,attempt,error_code," "source_expired FROM attachment_jobs WHERE state='failed' " "ORDER BY updated_at LIMIT 100" ).fetchall() print(json.dumps(rows, default=str)) else: if not args.attachment_id: parser.error("attachment_id is required") with db.connection() as conn: row = conn.execute( "SELECT j.source_key,a.object_key,a.storage_state FROM attachment_jobs j " "JOIN attachments a ON a.id=j.attachment_id AND " "a.pipeline_version=j.pipeline_version " "WHERE a.id=%s", (args.attachment_id,), ).fetchone() if not row: parser.error("Unknown attachment") store = ObjectStore(settings) try: key = row["object_key"] if row["storage_state"] == "ready" else row["source_key"] if not store.head(key): parser.error("Source is no longer retained") finally: store.close() job = Pipeline(db, settings).replay( args.attachment_id, new_version=args.action == "new-version" ) print(json.dumps({"job_id": job, "action": args.action})) except ValueError as exc: parser.error(str(exc)) finally: db.close() if __name__ == "__main__": main()