def _watch_job_state()

in exec/src/klio_exec/commands/stop.py [0:0]


def _watch_job_state(job, timeout=600):
    timeout_end = datetime.datetime.now() + datetime.timedelta(seconds=timeout)

    request = (
        _client.projects()
        .locations()
        .jobs()
        .get(
            jobId=job["id"],
            projectId=job["projectId"],
            location=job["location"],
        )
    )

    while datetime.datetime.now() < timeout_end:
        try:
            resp = request.execute()
        except Exception as e:
            msg = (
                "Failed to get current status for job '{}'. Error: {}.\n"
                "Trying again after 5s...".format(job["name"], e)
            )
            logging.info(msg)
            time.sleep(5)
            continue

        if resp["currentState"] in JOB_STATE_MAP.values():
            return
        else:
            msg = "Waiting for job '{}' to reach a terminal state...".format(
                job["name"]
            )
            logging.info(msg)
            time.sleep(5)

    msg = "Job '{}' did not reach a terminal state after '{}' seconds.".format(
        job["name"], timeout
    )
    logging.error(msg)
    raise SystemExit(1)