File size: 2,970 Bytes
2bb5af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# ruff: noqa: T201
"""Watch one run to the end, printing each step as it changes state.

Separate from the smoke scripts because the thing you need while chasing a
failure is a live view of a run that already exists - not a new one.

Usage:  .venv/Scripts/python.exe scripts/watch_run.py <run-id> [--resume]
"""

from __future__ import annotations

import argparse
import sys
import time

sys.path.insert(0, ".")

from scripts.smoke_local import (  # noqa: E402
    ADMIN_EMAIL,
    client,
    existing_user_id,
    token_for,
)

MARK = {
    "running": "..",
    "succeeded": "ok",
    "failed": "!!",
    "awaiting_review": "??",
    "skipped": "--",
}


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("run_id")
    parser.add_argument("--resume", action="store_true")
    parser.add_argument("--timeout", type=int, default=5400)
    args = parser.parse_args()

    user = client(token_for(ADMIN_EMAIL, existing_user_id(ADMIN_EMAIL)))

    if args.resume:
        response = user.post(f"/v1/runs/{args.run_id}/resume")
        print(f"resume: {response.status_code}")
        if response.status_code != 202:
            print(response.text[:300])
            return 1

    seen: dict[str, str] = {}
    began = time.time()
    while time.time() - began < args.timeout:
        response = user.get(f"/v1/runs/{args.run_id}")
        if response.status_code != 200:
            print(f"! run unreadable: {response.status_code} {response.text[:160]}")
            time.sleep(6)
            continue

        body = response.json()
        for step in body.get("steps", []):
            key, state = step["step_key"], step["status"]
            if seen.get(key) != state and state != "pending":
                print(f"  {MARK.get(state, state)}  {key}")
                seen[key] = state

        if body["status"] in ("succeeded", "failed", "cancelled", "awaiting_review"):
            print(f"\nstatus: {body['status']}")
            for step in body.get("steps", []):
                if step["status"] == "failed":
                    print(f"\nfailed at {step['step_key']}:")
                    print(str(step.get("error"))[:2500])
            if body["status"] == "succeeded":
                project_id = body["project_id"]
                items = user.get(
                    f"/v1/projects/{project_id}/artifacts"
                ).json().get("items", [])
                print(f"\nartifacts: {len(items)}")
                for item in items:
                    fetched = user.get(f"/v1/artifacts/{item['id']}")
                    print(f"  {item['title']}  {item['format']}  "
                          f"{(item['size_bytes'] or 0):,} bytes  "
                          f"download={fetched.status_code}")
            return 0 if body["status"] in ("succeeded", "awaiting_review") else 1

        time.sleep(8)

    print(f"! timed out after {args.timeout}s")
    return 1


if __name__ == "__main__":
    sys.exit(main())