#!/bin/bash
# The console's restart command: ask the host watcher for build $1, return once the old engine
# has gone and the new one answers a completion.
echo "$1" > /q5055/req/want || exit 3
exec /opt/venv/bin/python -I -c '
import json, time, urllib.request
def up():
    try:
        r = urllib.request.Request("http://127.0.0.1:18671/v1/completions",
            json.dumps({"model": "model", "prompt": "hi", "max_tokens": 2}).encode(),
            {"Content-Type": "application/json"})
        return urllib.request.urlopen(r, timeout=20).status == 200
    except Exception:
        return False
t = time.time()
while up() and time.time() - t < 180:
    time.sleep(1)
t = time.time()
while not up():
    if time.time() - t > 3000:
        raise SystemExit("engine did not come back")
    time.sleep(5)
'
