Skip to content

Commit

Permalink
Wait for job if max pending jobs exceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
kt474 committed Jul 10, 2024
1 parent faccfd5 commit 12d9c39
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions qiskit_ibm_runtime/qiskit_runtime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,19 +864,7 @@ def run(
)

if hgp_name == "ibm-q/open/main":
try:
usage = self.usage().get("byInstance")[0]
pending_jobs = usage.get("pendingJobs")
max_pending_jobs = usage.get("maxPendingJobs")
if pending_jobs >= max_pending_jobs:
logger.warning(
"The maximum number of pending jobs on the open plan is %s. "
"You currently have %s pending jobs. Please wait until a job finishes.",
max_pending_jobs,
pending_jobs,
)
except RequestsApiError as ex:
logger.warning("Unable to retrieve open plan usage information. %s", ex)
self.check_pending_jobs()

version = inputs.get("version", 1) if inputs else 1
try:
Expand Down Expand Up @@ -942,6 +930,36 @@ def _run(self, *args: Any, **kwargs: Any) -> Union[RuntimeJob, RuntimeJobV2]:
"""Private run method"""
return self.run(*args, **kwargs)

def check_pending_jobs(self) -> None:
"""Check the number of pending jobs and wait for the oldest pending job if
the maximum number of pending jobs has been reached.
"""
try:
usage = self.usage().get("byInstance")[0]
pending_jobs = usage.get("pendingJobs")
max_pending_jobs = usage.get("maxPendingJobs")
if pending_jobs >= max_pending_jobs:
logger.warning(
"The maximum number of pending jobs on the open plan is %s. "
"You currently have %s pending jobs. Waiting for a pending job to complete.",
max_pending_jobs,
pending_jobs,
)
try:
oldest_running = self.jobs(limit=1, descending=False, pending=True)
if oldest_running:
oldest_running[0].wait_for_final_state(timeout=300)

except Exception as ex: # pylint: disable=broad-except
logger.debug(
"An error occurred while waiting for job %s to finish: %s",
oldest_running[0].job_id(),
ex,
)

except Exception as ex: # pylint: disable=broad-except
logger.warning("Unable to retrieve open plan usage information. %s", ex)

def job(self, job_id: str) -> Union[RuntimeJob, RuntimeJobV2]:
"""Retrieve a runtime job.
Expand Down

0 comments on commit 12d9c39

Please sign in to comment.