Skip to content

Commit

Permalink
add ignore_failures option to Beaker.cluster.preempt()
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Mar 1, 2024
1 parent 2801b18 commit 7216a14
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use patch releases for compatibility fixes instead.

## Unreleased

### Added

- Added `ignore_failures` option to `Beaker.cluster.preempt_jobs()`.

## [v1.26.1](https://github.com/allenai/beaker-py/releases/tag/v1.26.1) - 2024-02-28

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions beaker/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"ValidationError",
"HTTPError",
"RequestException",
"BeakerPermissionsError",
"NotFoundError",
"AccountNotFound",
"OrganizationNotFound",
Expand Down Expand Up @@ -76,6 +77,12 @@ class BeakerError(Exception):
"""


class BeakerPermissionsError(BeakerError):
"""
Raised when a user doesn't have sufficient permissions to perform an action.
"""


class NotFoundError(BeakerError):
"""
Base class for all "not found" error types.
Expand Down
15 changes: 13 additions & 2 deletions beaker/services/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,14 @@ def url(self, cluster: Union[str, Cluster]) -> str:
cluster_name = self.resolve_cluster(cluster).full_name
return f"{self.config.agent_address}/cl/{cluster_name}/details"

def preempt_jobs(self, cluster: Union[str, Cluster]) -> List[Job]:
def preempt_jobs(
self, cluster: Union[str, Cluster], ignore_failures: bool = False
) -> List[Job]:
"""
Preempt all preemptible jobs on the cluster.
:param cluster: The cluster ID, full name, or object.
:param ignore_failures: If ``True``, any jobs that fail to preempt will be ignored.
:raises ClusterNotFound: If the cluster doesn't exist.
:raises BeakerError: Any other :class:`~beaker.exceptions.BeakerError` type that can occur.
Expand All @@ -387,7 +390,15 @@ def preempt_jobs(self, cluster: Union[str, Cluster]) -> List[Job]:
continue
if job.execution.spec.context.priority != Priority.preemptible:
continue
preempted_jobs.append(self.beaker.job.preempt(job))
try:
preempted_jobs.append(self.beaker.job.preempt(job))
except BeakerPermissionsError:
if ignore_failures:
self.logger.warning(
"Failed to preempt job '%s': insufficient permissions", job.id
)
else:
raise
return preempted_jobs

def _not_found_err_msg(self, cluster: Union[str, Cluster]) -> str:
Expand Down
5 changes: 4 additions & 1 deletion beaker/services/service_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def make_request(session: requests.Session) -> requests.Response:
and 400 <= response.status_code < 500
):
# Raise a BeakerError if we're misusing the API (4xx error code).
raise BeakerError(msg)
if response.status_code == 403:
raise BeakerPermissionsError(msg)
else:
raise BeakerError(msg)
elif msg is not None:
raise HTTPError(msg, response=response) # type: ignore
else:
Expand Down

0 comments on commit 7216a14

Please sign in to comment.