Skip to content

Commit 304a5f6

Browse files
committed
Merge branch 'm-kovalsky/cancel_item_job_instance'
2 parents ce50548 + 4e6a034 commit 304a5f6

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

src/sempy_labs/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
create_item_schedule_cron,
4545
create_item_schedule_daily,
4646
create_item_schedule_weekly,
47+
cancel_item_job_instance,
4748
)
4849
from ._delta_analyzer import (
4950
delta_analyzer,
@@ -601,4 +602,5 @@
601602
"list_domains",
602603
"get_workspace_git_outbound_policy",
603604
"set_workspace_git_outbound_policy",
605+
"cancel_item_job_instance",
604606
]

src/sempy_labs/_job_scheduler.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from uuid import UUID
1414
import sempy_labs._icons as icons
15+
import time
1516

1617

1718
@log
@@ -457,6 +458,8 @@ def create_item_schedule_weekly(
457458
458459
This is a wrapper function for the following API: `Job Scheduler - Create Item Schedule <https://learn.microsoft.com/rest/api/fabric/core/job-scheduler/create-item-schedule>`_.
459460
461+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
462+
460463
Parameters
461464
----------
462465
item : str | uuid.UUID
@@ -483,7 +486,7 @@ def create_item_schedule_weekly(
483486
or if no lakehouse attached, resolves to the workspace of the notebook.
484487
"""
485488

486-
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
489+
workspace_id = resolve_workspace_id(workspace)
487490
(item_name, item_id) = resolve_item_name_and_id(
488491
item=item, type=type, workspace=workspace
489492
)
@@ -527,3 +530,80 @@ def create_item_schedule_weekly(
527530
print(
528531
f"{icons.green_dot} The schedule for the '{item_name}' {type.lower()} has been created."
529532
)
533+
534+
535+
@log
536+
def cancel_item_job_instance(
537+
item: str | UUID,
538+
job_instance_id: UUID,
539+
type: str,
540+
workspace: Optional[str | UUID] = None,
541+
):
542+
"""
543+
Cancel an item's job instance.
544+
545+
This is a wrapper function for the following API: `Job Scheduler - Cancel Item Job Instance <https://learn.microsoft.com/rest/api/fabric/core/job-scheduler/cancel-item-job-instance>`_.
546+
547+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
548+
549+
Parameters
550+
----------
551+
item : str | uuid.UUID
552+
The item name or ID.
553+
job_instance_id : uuid.UUID
554+
The job instance ID to cancel.
555+
type : str
556+
The item `type <https://learn.microsoft.com/rest/api/fabric/core/items/list-items?tabs=HTTP#itemtype>`_. If specifying the item name as the item, the item type is required.
557+
workspace : str | uuid.UUID, default=None
558+
The Fabric workspace name or ID used by the lakehouse.
559+
Defaults to None which resolves to the workspace of the attached lakehouse
560+
or if no lakehouse attached, resolves to the workspace of the notebook.
561+
"""
562+
563+
workspace_id = resolve_workspace_id(workspace)
564+
(item_name, item_id) = resolve_item_name_and_id(
565+
item=item, type=type, workspace=workspace
566+
)
567+
568+
response = _base_api(
569+
request=f"v1/workspaces/{workspace_id}/items/{item_id}/jobs/instances/{job_instance_id}",
570+
client="fabric_sp",
571+
)
572+
current_status = response.json().get("status")
573+
574+
if current_status not in ["NotStarted", "InProgress"]:
575+
print(
576+
f"{icons.info} The job instance '{job_instance_id}' for the '{item_name}' {type.lower()} is in status '{current_status}' and cannot be cancelled."
577+
)
578+
return
579+
else:
580+
# Cancel the job instance
581+
response = _base_api(
582+
request=f"v1/workspaces/{workspace_id}/items/{item_id}/jobs/instances/{job_instance_id}/cancel",
583+
method="post",
584+
status_codes=202,
585+
client="fabric_sp",
586+
)
587+
588+
status_url = response.headers.get("Location").split("fabric.microsoft.com")[1]
589+
status = None
590+
while status not in ["Completed", "Failed", "Cancelled"]:
591+
response = _base_api(request=status_url)
592+
status = response.json().get("status")
593+
time.sleep(3)
594+
595+
if status == "Cancelled":
596+
print(
597+
f"{icons.green_dot} The job instance '{job_instance_id}' for the '{item_name}' {type.lower()} has been cancelled."
598+
)
599+
return
600+
elif status == "Failed":
601+
print(
602+
f"{icons.info} The job instance '{job_instance_id}' for the '{item_name}' {type.lower()} could not be cancelled and has failed."
603+
)
604+
return
605+
elif status == "Completed":
606+
print(
607+
f"{icons.info} The job instance '{job_instance_id}' for the '{item_name}' {type.lower()} has already completed before it could be cancelled."
608+
)
609+
return

src/sempy_labs/lakehouse/_materialized_lake_views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def refresh_materialized_lake_views(
4949
df = _base_api(
5050
request=f"/v1/workspaces/{workspace_id}/lakehouses/{lakehouse_id}/jobs/instances?jobType=RefreshMaterializedLakeViews",
5151
lro_return_df=True,
52+
method="post",
5253
)
5354

5455
status = df["Status"].iloc[0]

0 commit comments

Comments
 (0)