66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Drive a covered cache eviction through the bot-free HTTP adapter."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
|
|
|
|
CONTROL = "http://127.0.0.1:18081/test/v1"
|
|
INFO_HASH = "330be0cb7c2201135a2de63b28e77993745ff688"
|
|
|
|
|
|
def get_json(url: str):
|
|
with urllib.request.urlopen(url, timeout=35) as response:
|
|
return json.load(response)
|
|
|
|
|
|
def post_json(url: str, value: object):
|
|
request = urllib.request.Request(
|
|
url,
|
|
data=json.dumps(value, separators=(",", ":")).encode(),
|
|
method="POST",
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
with urllib.request.urlopen(request, timeout=35) as response:
|
|
return json.load(response)
|
|
|
|
|
|
def main() -> int:
|
|
preview = post_json(f"{CONTROL}/jobs/preview", {
|
|
"operation": "evict_cache",
|
|
"cache_client_id": "cache-1",
|
|
"resource_id": {"info_hash_v1_hex": INFO_HASH},
|
|
})
|
|
definition = preview["definition"]
|
|
job_id = definition["job_id"]
|
|
post_json(f"{CONTROL}/jobs", {
|
|
"preview_revision": preview["preview_revision"],
|
|
"definition": definition,
|
|
})
|
|
deadline = time.monotonic() + 180
|
|
while time.monotonic() < deadline:
|
|
job = get_json(f"{CONTROL}/jobs/{job_id}")
|
|
if job["state"] == "JOB_STATE_SUCCEEDED" and job["committed"]:
|
|
print(json.dumps({
|
|
"job_id": job_id,
|
|
"state": job["state"],
|
|
"committed": job["committed"],
|
|
}, sort_keys=True))
|
|
return 0
|
|
if job["state"] in {"JOB_STATE_FAILED", "JOB_STATE_CANCELLED"}:
|
|
raise RuntimeError(f"eviction did not succeed: {job}")
|
|
time.sleep(0.5)
|
|
raise RuntimeError("timed out waiting for cache eviction")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
raise SystemExit(main())
|
|
except Exception as exc:
|
|
print(f"eviction happy-path failed: {exc}", file=sys.stderr)
|
|
raise
|