Skip to content

Commit 7714169

Browse files
committed
moved logic to lro function
1 parent 8797913 commit 7714169

File tree

4 files changed

+50
-62
lines changed

4 files changed

+50
-62
lines changed

src/sempy_labs/_helper_functions.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,9 @@ def lro(
15681568
status_codes: Optional[List[str]] = [200, 202],
15691569
sleep_time: Optional[int] = 1,
15701570
return_status_code: bool = False,
1571+
job_scheduler: bool = False,
15711572
):
1573+
from sempy_labs._job_scheduler import _get_item_job_instance
15721574

15731575
if response.status_code not in status_codes:
15741576
raise FabricHTTPException(response)
@@ -1578,20 +1580,32 @@ def lro(
15781580
else:
15791581
result = response
15801582
if response.status_code == status_codes[1]:
1581-
operationId = response.headers["x-ms-operation-id"]
1582-
response = client.get(f"/v1/operations/{operationId}")
1583-
response_body = json.loads(response.content)
1584-
while response_body["status"] not in ["Succeeded", "Failed"]:
1585-
time.sleep(sleep_time)
1586-
response = client.get(f"/v1/operations/{operationId}")
1587-
response_body = json.loads(response.content)
1588-
if response_body["status"] != "Succeeded":
1589-
raise FabricHTTPException(response)
1590-
if return_status_code:
1591-
result = response.status_code
1583+
if job_scheduler:
1584+
status_url = response.headers.get("Location").split("fabric.microsoft.com")[
1585+
1
1586+
]
1587+
status = None
1588+
while status not in ["Completed", "Failed"]:
1589+
response = _base_api(request=status_url)
1590+
status = response.json().get("status")
1591+
time.sleep(3)
1592+
1593+
return _get_item_job_instance(url=status_url)
15921594
else:
1593-
response = client.get(f"/v1/operations/{operationId}/result")
1594-
result = response
1595+
operation_id = response.headers["x-ms-operation-id"]
1596+
response = client.get(f"/v1/operations/{operation_id}")
1597+
response_body = json.loads(response.content)
1598+
while response_body["status"] not in ["Succeeded", "Failed"]:
1599+
time.sleep(sleep_time)
1600+
response = client.get(f"/v1/operations/{operation_id}")
1601+
response_body = json.loads(response.content)
1602+
if response_body["status"] != "Succeeded":
1603+
raise FabricHTTPException(response)
1604+
if return_status_code:
1605+
result = response.status_code
1606+
else:
1607+
response = client.get(f"/v1/operations/{operation_id}/result")
1608+
result = response
15951609

15961610
return result
15971611

@@ -2212,6 +2226,7 @@ def _base_api(
22122226
uses_pagination: bool = False,
22132227
lro_return_json: bool = False,
22142228
lro_return_status_code: bool = False,
2229+
lro_return_df: bool = False,
22152230
):
22162231
import notebookutils
22172232
from sempy_labs._authentication import _get_headers
@@ -2267,7 +2282,9 @@ def get_token(self, *scopes, **kwargs) -> AccessToken:
22672282
json=payload,
22682283
)
22692284

2270-
if lro_return_json:
2285+
if lro_return_df:
2286+
return lro(c, response, status_codes, job_scheduler=True)
2287+
elif lro_return_json:
22712288
return lro(c, response, status_codes).json()
22722289
elif lro_return_status_code:
22732290
return lro(c, response, status_codes, return_status_code=True)

src/sempy_labs/graph_model/_background_jobs.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
resolve_workspace_name_and_id,
99
)
1010
import sempy_labs._icons as icons
11-
import time
12-
from sempy_labs._job_scheduler import _get_item_job_instance
1311

1412

1513
@log
@@ -41,23 +39,16 @@ def refresh_graph(
4139
item=graph_model, type="GraphModel", workspace=workspace_id
4240
)
4341

44-
response = _base_api(
45-
request=f"/v1/workspaces/{workspace_id}/GraphModels/{item_id}/jobs/instances?jobType=RefreshGraph",
46-
method="post",
47-
)
48-
4942
print(
5043
f"{icons.in_progress} The refresh graph job for the '{item_name}' graph model within the '{workspace_name}' workspace has been initiated."
5144
)
5245

53-
status_url = response.headers.get("Location").split("fabric.microsoft.com")[1]
54-
status = None
55-
while status not in ["Completed", "Failed"]:
56-
response = _base_api(request=status_url)
57-
status = response.json().get("status")
58-
time.sleep(3)
59-
60-
df = _get_item_job_instance(url=status_url)
46+
df = _base_api(
47+
request=f"/v1/workspaces/{workspace_id}/GraphModels/{item_id}/jobs/instances?jobType=RefreshGraph",
48+
method="post",
49+
lro_return_df=True,
50+
)
51+
status = df["Status"].iloc[0]
6152

6253
if status == "Completed":
6354
print(

src/sempy_labs/lakehouse/_lakehouse.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
)
1212
import sempy_labs._icons as icons
1313
import re
14-
import time
1514
import pandas as pd
16-
from sempy_labs._job_scheduler import (
17-
_get_item_job_instance,
18-
)
1915

2016

2117
@log
@@ -247,26 +243,20 @@ def is_valid_format(time_string):
247243
if vacuum and retention_period is not None:
248244
payload["executionData"]["vacuumSettings"]["retentionPeriod"] = retention_period
249245

250-
response = _base_api(
246+
print(
247+
f"{icons.in_progress} The table maintenance job for the '{table_name}' table in the '{lakehouse_name}' lakehouse within the '{workspace_name}' workspace has been initiated."
248+
)
249+
250+
df = _base_api(
251251
request=f"/v1/workspaces/{workspace_id}/lakehouses/{lakehouse_id}/jobs/instances?jobType=TableMaintenance",
252252
method="post",
253253
payload=payload,
254-
status_codes=202,
254+
status_codes=[200, 202],
255255
client="fabric_sp",
256+
lro_return_df=True,
256257
)
257258

258-
print(
259-
f"{icons.in_progress} The table maintenance job for the '{table_name}' table in the '{lakehouse_name}' lakehouse within the '{workspace_name}' workspace has been initiated."
260-
)
261-
262-
status_url = response.headers.get("Location").split("fabric.microsoft.com")[1]
263-
status = None
264-
while status not in ["Completed", "Failed"]:
265-
response = _base_api(request=status_url)
266-
status = response.json().get("status")
267-
time.sleep(3)
268-
269-
df = _get_item_job_instance(url=status_url)
259+
status = df["Status"].iloc[0]
270260

271261
if status == "Completed":
272262
print(

src/sempy_labs/lakehouse/_materialized_lake_views.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
from uuid import UUID
1010
from sempy._utils._log import log
1111
import sempy_labs._icons as icons
12-
import time
13-
from sempy_labs._job_scheduler import (
14-
_get_item_job_instance,
15-
)
1612
import pandas as pd
1713

1814

@@ -46,22 +42,16 @@ def refresh_materialized_lake_views(
4642
lakehouse=lakehouse, workspace=workspace_id
4743
)
4844

49-
response = _base_api(
50-
request=f"/v1/workspaces/{workspace_id}/lakehouses/{lakehouse_id}/jobs/instances?jobType=RefreshMaterializedLakeViews",
51-
)
52-
5345
print(
5446
f"{icons.in_progress} The refresh materialized lake views job for the '{lakehouse_name}' lakehouse within the '{workspace_name}' workspace has been initiated."
5547
)
5648

57-
status_url = response.headers.get("Location").split("fabric.microsoft.com")[1]
58-
status = None
59-
while status not in ["Completed", "Failed"]:
60-
response = _base_api(request=status_url)
61-
status = response.json().get("status")
62-
time.sleep(3)
49+
df = _base_api(
50+
request=f"/v1/workspaces/{workspace_id}/lakehouses/{lakehouse_id}/jobs/instances?jobType=RefreshMaterializedLakeViews",
51+
lro_return_df=True,
52+
)
6353

64-
df = _get_item_job_instance(url=status_url)
54+
status = df["Status"].iloc[0]
6555

6656
if status == "Completed":
6757
print(

0 commit comments

Comments
 (0)