Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Added close_session #762

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions qiskit_ibm_provider/api/clients/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,11 @@ def close_session(self, session_id: str) -> None:
session_id (str): the id of the session to close
"""
self._api.runtime_session(session_id=session_id).close()

def cancel_session(self, session_id: str) -> None:
"""Cancel session

Args:
session_id (str): the id of the session to cancel
"""
self._api.runtime_session(session_id=session_id).cancel()
17 changes: 16 additions & 1 deletion qiskit_ibm_provider/api/rest/runtime_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@


from qiskit_ibm_provider.api.rest.base import RestAdapterBase
from qiskit_ibm_provider.exceptions import IBMApiError
from ..exceptions import RequestsApiError
from ..session import RetrySession


class RuntimeSession(RestAdapterBase):
"""Rest adapter for session related endpoints."""

URL_MAP = {
"self": "",
"close": "/close",
}

Expand All @@ -37,6 +40,18 @@ def __init__(
super().__init__(session, "{}/sessions/{}".format(url_prefix, session_id))

def close(self) -> None:
"""Close this session."""
"""Set accepting_jobs flag to false, so no more jobs can be submitted."""
payload = {"accepting_jobs": False}
url = self.get_url("self")
try:
self.session.patch(url, json=payload)
except RequestsApiError as ex:
if ex.status_code == 404:
pass
else:
raise IBMApiError(f"Error closing session: {ex}")

def cancel(self) -> None:
"""Cancel this session."""
url = self.get_url("close")
self.session.delete(url)
10 changes: 10 additions & 0 deletions qiskit_ibm_provider/ibm_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,16 @@ def session(self) -> Session:

def cancel_session(self) -> None:
"""Cancel session. All pending jobs will be cancelled."""
if self._session:
self._session.cancel()
if self._session.session_id:
self.provider._runtime_client.cancel_session(self._session.session_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.provider._runtime_client.cancel_session doesn't exist in this repo so it'll have to be added there as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I shouldn't have submitted for review so quickly. I copied the implementation from runtime. Also added a test, though it is pretty trivial. We will unite this functionality back when uniting the two repos.

self._session = None

def close_session(self) -> None:
"""Close the session so new jobs will no longer be accepted, but existing
queued or running jobs will run to completion. The session will be terminated once there
are no more pending jobs."""
if self._session:
self._session.cancel()
if self._session.session_id:
Expand Down
11 changes: 10 additions & 1 deletion test/integration/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,23 @@ def test_backend_run_with_session(self):
)

def test_session_cancel(self):
"""Test closing a session"""
"""Test canceling a session"""
provider = IBMProvider(self.dependencies.token, self.dependencies.url)
backend = provider.get_backend("ibmq_qasm_simulator")
backend.open_session()
self.assertTrue(backend.session.active)
backend.cancel_session()
self.assertIsNone(backend.session)

def test_session_close(self):
"""Test closing a session"""
provider = IBMProvider(self.dependencies.token, self.dependencies.url)
backend = provider.get_backend("ibmq_qasm_simulator")
backend.open_session()
self.assertTrue(backend.session.active)
backend.close_session()
self.assertIsNone(backend.session)

def test_run_after_cancel(self):
"""Test running after session is cancelled."""
provider = IBMProvider(self.dependencies.token, self.dependencies.url)
Expand Down