|
| 1 | +# ------------------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | +# ------------------------------------------------------------------------------------------ |
| 5 | +import os |
| 6 | + |
| 7 | +from azureml._restclient.constants import RunStatus |
| 8 | +from azureml.core import Experiment, Run, Workspace |
| 9 | +from azureml.core.authentication import ServicePrincipalAuthentication |
| 10 | + |
| 11 | + |
| 12 | +def cancel_running_and_queued_jobs() -> None: |
| 13 | + environ = os.environ |
| 14 | + print("Authenticating") |
| 15 | + auth = ServicePrincipalAuthentication( |
| 16 | + tenant_id='72f988bf-86f1-41af-91ab-2d7cd011db47', |
| 17 | + service_principal_id=environ["APPLICATION_ID"], |
| 18 | + service_principal_password=environ["APPLICATION_KEY"]) |
| 19 | + print("Getting AML workspace") |
| 20 | + workspace = Workspace.get( |
| 21 | + name="InnerEye-DeepLearning", |
| 22 | + auth=auth, |
| 23 | + subscription_id=environ["SUBSCRIPTION_ID"], |
| 24 | + resource_group="InnerEye-DeepLearning") |
| 25 | + branch = environ["BRANCH"] |
| 26 | + print(f"Branch: {branch}") |
| 27 | + if not branch.startswith("refs/pull/"): |
| 28 | + print("This branch is not a PR branch, hence not cancelling anything.") |
| 29 | + exit(0) |
| 30 | + experiment_name = branch.replace("/", "_") |
| 31 | + print(f"Experiment: {experiment_name}") |
| 32 | + experiment = Experiment(workspace, name=experiment_name) |
| 33 | + print(f"Retrieved experiment {experiment.name}") |
| 34 | + for run in experiment.get_runs(include_children=True, properties={}): |
| 35 | + assert isinstance(run, Run) |
| 36 | + status_suffix = f"'{run.status}' run {run.id} ({run.display_name})" |
| 37 | + if run.status in (RunStatus.COMPLETED, RunStatus.FAILED, RunStatus.FINALIZING, RunStatus.CANCELED, |
| 38 | + RunStatus.CANCEL_REQUESTED): |
| 39 | + print(f"Skipping {status_suffix}") |
| 40 | + else: |
| 41 | + print(f"Cancelling {status_suffix}") |
| 42 | + run.cancel() |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + cancel_running_and_queued_jobs() |
0 commit comments