|
3 | 3 |
|
4 | 4 | # Copyright (c) 2021, 2023 Oracle and/or its affiliates. |
5 | 5 | # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 6 | +import time |
6 | 7 | from typing import List, Union, Dict |
7 | 8 | from urllib.parse import urlparse |
8 | 9 |
|
9 | 10 | import fsspec |
| 11 | +import oci |
10 | 12 | from ads.common.auth import default_signer |
11 | 13 | from ads.jobs.builders.base import Builder |
12 | 14 | from ads.jobs.builders.infrastructure.dataflow import DataFlow, DataFlowRun |
13 | | -from ads.jobs.builders.infrastructure.dsc_job import DataScienceJob, DataScienceJobRun |
| 15 | +from ads.jobs.builders.infrastructure.dsc_job import ( |
| 16 | + DataScienceJob, |
| 17 | + DataScienceJobRun, |
| 18 | + SLEEP_INTERVAL |
| 19 | +) |
14 | 20 | from ads.jobs.builders.runtimes.pytorch_runtime import PyTorchDistributedRuntime |
15 | 21 | from ads.jobs.builders.runtimes.container_runtime import ContainerRuntime |
16 | 22 | from ads.jobs.builders.runtimes.python_runtime import ( |
@@ -460,7 +466,29 @@ def run_list(self, **kwargs) -> list: |
460 | 466 | A list of job run instances, the actual object type depends on the infrastructure. |
461 | 467 | """ |
462 | 468 | return self.infrastructure.run_list(**kwargs) |
| 469 | + |
| 470 | + def cancel(self, wait_for_completion: bool = True) -> None: |
| 471 | + """Cancels the runs of the job. |
463 | 472 |
|
| 473 | + Parameters |
| 474 | + ---------- |
| 475 | + wait_for_completion: bool |
| 476 | + Whether to wait for run to be cancelled before proceeding. |
| 477 | + Defaults to True. |
| 478 | + """ |
| 479 | + runs = self.run_list() |
| 480 | + for run in runs: |
| 481 | + run.cancel(wait_for_completion=False) |
| 482 | + |
| 483 | + if wait_for_completion: |
| 484 | + for run in runs: |
| 485 | + while ( |
| 486 | + run.lifecycle_state != |
| 487 | + oci.data_science.models.JobRun.LIFECYCLE_STATE_CANCELED |
| 488 | + ): |
| 489 | + run.sync() |
| 490 | + time.sleep(SLEEP_INTERVAL) |
| 491 | + |
464 | 492 | def delete(self) -> None: |
465 | 493 | """Deletes the job from the infrastructure.""" |
466 | 494 | self.infrastructure.delete() |
|
0 commit comments