From 7a62247107eb6c5d5370e85933c345646015b934 Mon Sep 17 00:00:00 2001 From: Caleb Johnson Date: Mon, 6 Nov 2023 12:47:26 -0600 Subject: [PATCH] Translate all job statuses to Qiskit terminology in client (#1062) * Change job status verbiage * Don't overwrite ray statuses * black * Leftover PENDING. Remove dated status printouts in readmes * Remove old FAILED. * Add bytes to the status field for longer descriptions * Add bytes to the status field for longer descriptions * Add bytes to the status field for longer descriptions * Restore migration files * Translate job statuses on client side. * Leave model as-is. Translate statuses client-side * Linter errors. Use Ray terminology in gateway tests * Revert init file * Fix weird changes that happened in init file * Revert change to core init * Revert init change * Print unknown statuses rather than error * Remove unused exception variable * Don't print in the _map_status_to_serverless --- README.md | 2 +- client/README.md | 2 +- client/quantum_serverless/core/job.py | 27 ++++++++++++++++++++++----- client/tests/core/test_pattern.py | 9 ++++----- client/tests/utils.py | 2 +- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 65f6dfb26..9dea52165 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ For user convenience, this section assumes that users will deploy the infrastruc ``` job.status() - # + # 'DONE' job.logs() # 2023-09-21 03:48:40,286\tINFO worker.py:1329 -- Using address 172.18.0.4:6379 set in the environment variable RAY_ADDRESS\n2023-09-21 03:48:40,286\tINFO worker.py:1458 -- Connecting to existing Ray cluster at address: 172.18.0.4:6379...\n2023-09-21 03:48:40,295\tINFO worker.py:1633 -- Connected to Ray cluster. View the dashboard at \x1b[1m\x1b[32m172.18.0.4:8265 \x1b[39m\x1b[22m\n diff --git a/client/README.md b/client/README.md index 1250597a1..505d1a491 100644 --- a/client/README.md +++ b/client/README.md @@ -106,7 +106,7 @@ Full docs can be found at https://qiskit-extensions.github.io/quantum-serverless ```python job.status() - # + # 'DONE' # or get logs job.logs() diff --git a/client/quantum_serverless/core/job.py b/client/quantum_serverless/core/job.py index 16d4c93cb..b6a387caa 100644 --- a/client/quantum_serverless/core/job.py +++ b/client/quantum_serverless/core/job.py @@ -132,7 +132,7 @@ def __init__(self, client: JobSubmissionClient): self._job_client = client def status(self, job_id: str): - return self._job_client.get_job_status(job_id) + return self._job_client.get_job_status(job_id).value def stop(self, job_id: str): return self._job_client.stop_job(job_id) @@ -611,7 +611,7 @@ def __init__( def status(self): """Returns status of the job.""" - return self._job_client.status(self.job_id) + return _map_status_to_serverless(self._job_client.status(self.job_id)) def stop(self): """Stops the job from running.""" @@ -634,7 +634,7 @@ def result(self, wait=True, cadence=5, verbose=False): if wait: if verbose: logging.info("Waiting for job result.") - while not self._in_terminal_state(): + while not self.in_terminal_state(): time.sleep(cadence) if verbose: logging.info(".") @@ -649,9 +649,9 @@ def result(self, wait=True, cadence=5, verbose=False): return results - def _in_terminal_state(self) -> bool: + def in_terminal_state(self) -> bool: """Checks if job is in terminal state""" - terminal_states = ["STOPPED", "SUCCEEDED", "FAILED"] + terminal_states = ["CANCELED", "DONE", "ERROR"] return self.status() in terminal_states def __repr__(self): @@ -723,3 +723,20 @@ def save_result(result: Dict[str, Any]): logging.warning("Something went wrong: %s", response.text) return response.ok + + +def _map_status_to_serverless(status: str) -> str: + """Map a status string from job client to the Qiskit terminology.""" + status_map = { + "PENDING": "INITIALIZING", + "RUNNING": "RUNNING", + "STOPPED": "CANCELED", + "SUCCEEDED": "DONE", + "FAILED": "ERROR", + "QUEUED": "QUEUED", + } + + try: + return status_map[status] + except KeyError: + return status diff --git a/client/tests/core/test_pattern.py b/client/tests/core/test_pattern.py index 68f3e9034..65111d9b6 100644 --- a/client/tests/core/test_pattern.py +++ b/client/tests/core/test_pattern.py @@ -1,7 +1,6 @@ """Tests jobs.""" import os -from ray.dashboard.modules.job.common import JobStatus from testcontainers.compose import DockerCompose from quantum_serverless import QuantumServerless, BaseProvider @@ -49,12 +48,12 @@ def test_program(): wait_for_job_completion(job) assert "42" in job.logs() - assert job.status().is_terminal() - assert job.status() == JobStatus.SUCCEEDED + assert job.in_terminal_state() + assert job.status() == "DONE" recovered_job = serverless.get_job_by_id(job.job_id) assert recovered_job.job_id == job.job_id assert "42" in recovered_job.logs() - assert recovered_job.status().is_terminal() - assert recovered_job.status() == JobStatus.SUCCEEDED + assert recovered_job.in_terminal_state() + assert recovered_job.status() == "DONE" assert isinstance(job.stop(), bool) diff --git a/client/tests/utils.py b/client/tests/utils.py index 609f639ad..16da554ff 100644 --- a/client/tests/utils.py +++ b/client/tests/utils.py @@ -18,6 +18,6 @@ def wait_for_job_completion(job: Job, timeout: int = 60): """Utility function that waits for job completion.""" must_finish = time.time() + timeout while time.time() < must_finish: - if job.status().is_terminal(): + if job.in_terminal_state(): break time.sleep(1)