Forum Discussion
JibinSebastian
Advocate II
6 months agoMirrored Sql server not Resumeing automatically even after the Capacity resumes
I have connected a mirrored SQL database and everything was working properly. However, our Fabric capacity is scheduled to be suspended at 7:00 PM and resumed the next day at 7:00 AM. Because of this...
- 6 months ago
Hello JibinSebastian this is a code snippet you may want to try and see if it works! It restarts any mirrored database in your workspace that is not in Running status.
def get_fabric_token_via_user() -> str: """ User-context token in a Fabric notebook. NOTE: Some endpoints may fail with InsufficientScopes under SPN contexts; SPN is recommended. """ # Try Fabric audience first; fall back to built-in 'pbi' token if needed. try: return mssparkutils.credentials.getToken("https://api.fabric.microsoft.com/.default") except Exception: return mssparkutils.credentials.getToken("pbi") def fabric_request(method: str, path: str, token: str, body: Optional[dict] = None): base = "https://api.fabric.microsoft.com/v1" url = path if path.startswith("http") else f"{base}/{path}" headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json", "Accept":"application/json"} # simple 429 retry loop for attempt in range(6): resp = requests.request(method, url, headers=headers, json=body, timeout=60) if resp.status_code != 429: if not resp.ok: raise RuntimeError(f"Fabric API {method} {url} failed: {resp.status_code} {resp.text}") return resp retry_after = int(resp.headers.get("Retry-After", "5")) print(f"429 from Fabric API. Retrying in {retry_after}s ...") time.sleep(retry_after) raise RuntimeError("Fabric API throttled repeatedly (429)") fabric_token = get_fabric_token_via_user() def start_mirroring_for_workspace(workspace_id: str): print(f"\nWorkspace {workspace_id}: listing mirrored databases ...") # GET /workspaces/{workspaceId}/mirroredDatabases mirrors = fabric_request("GET", f"workspaces/{workspace_id}/mirroredDatabases", fabric_token).json().get("value", []) if not mirrors: print("No Mirrored Databases found.") return for m in mirrors: mid = m["id"] name = m.get("displayName", mid) # POST /getMirroringStatus status = fabric_request("POST", f"workspaces/{workspace_id}/mirroredDatabases/{mid}/getMirroringStatus", fabric_token).json().get("status") print(f" - '{name}': current status = {status}") if status != "Running": print(f" Starting mirroring for '{name}' ...") # POST /startMirroring fabric_request("POST", f"workspaces/{workspace_id}/mirroredDatabases/{mid}/startMirroring", fabric_token) time.sleep(2) status2 = fabric_request("POST", f"workspaces/{workspace_id}/mirroredDatabases/{mid}/getMirroringStatus", fabric_token).json().get("status") print(f" New status = {status2}") else: print(f" Already Running. Skipping.") for wsid in WORKSPACE_IDS: start_mirroring_for_workspace(wsid) print("\nDone.")
deborshi_nag
Super User
6 months agoHello JibinSebastian this is a code snippet you may want to try and see if it works! It restarts any mirrored database in your workspace that is not in Running status.
def get_fabric_token_via_user() -> str:
"""
User-context token in a Fabric notebook.
NOTE: Some endpoints may fail with InsufficientScopes under SPN contexts; SPN is recommended.
"""
# Try Fabric audience first; fall back to built-in 'pbi' token if needed.
try:
return mssparkutils.credentials.getToken("https://api.fabric.microsoft.com/.default")
except Exception:
return mssparkutils.credentials.getToken("pbi")
def fabric_request(method: str, path: str, token: str, body: Optional[dict] = None):
base = "https://api.fabric.microsoft.com/v1"
url = path if path.startswith("http") else f"{base}/{path}"
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json", "Accept":"application/json"}
# simple 429 retry loop
for attempt in range(6):
resp = requests.request(method, url, headers=headers, json=body, timeout=60)
if resp.status_code != 429:
if not resp.ok:
raise RuntimeError(f"Fabric API {method} {url} failed: {resp.status_code} {resp.text}")
return resp
retry_after = int(resp.headers.get("Retry-After", "5"))
print(f"429 from Fabric API. Retrying in {retry_after}s ...")
time.sleep(retry_after)
raise RuntimeError("Fabric API throttled repeatedly (429)")
fabric_token = get_fabric_token_via_user()
def start_mirroring_for_workspace(workspace_id: str):
print(f"\nWorkspace {workspace_id}: listing mirrored databases ...")
# GET /workspaces/{workspaceId}/mirroredDatabases
mirrors = fabric_request("GET", f"workspaces/{workspace_id}/mirroredDatabases", fabric_token).json().get("value", [])
if not mirrors:
print("No Mirrored Databases found.")
return
for m in mirrors:
mid = m["id"]
name = m.get("displayName", mid)
# POST /getMirroringStatus
status = fabric_request("POST", f"workspaces/{workspace_id}/mirroredDatabases/{mid}/getMirroringStatus", fabric_token).json().get("status")
print(f" - '{name}': current status = {status}")
if status != "Running":
print(f" Starting mirroring for '{name}' ...")
# POST /startMirroring
fabric_request("POST", f"workspaces/{workspace_id}/mirroredDatabases/{mid}/startMirroring", fabric_token)
time.sleep(2)
status2 = fabric_request("POST", f"workspaces/{workspace_id}/mirroredDatabases/{mid}/getMirroringStatus", fabric_token).json().get("status")
print(f" New status = {status2}")
else:
print(f" Already Running. Skipping.")
for wsid in WORKSPACE_IDS:
start_mirroring_for_workspace(wsid)
print("\nDone.")
JibinSebastian
Advocate II
6 months agoThis is super cool deborshi_nag it worked. thank you so much for your time and efforts.
have a great day ahead