Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save lassebenni/af1d21f39b004ab9f858f4facdf2766c to your computer and use it in GitHub Desktop.

Select an option

Save lassebenni/af1d21f39b004ab9f858f4facdf2766c to your computer and use it in GitHub Desktop.
Code Explainer: Week 15 ACA start and wait
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Explainer: ACA start and wait</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: #0d1117;
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
font-family: 'SF Pro Text', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
padding: 12px;
color: #c9d1d9;
}
.explainer-container {
width: 100%;
max-width: 860px;
background: #161b22;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 16px 36px rgba(0,0,0,0.6);
border: 1px solid #30363d;
display: flex;
flex-direction: column;
}
.titlebar {
background: #0d1117;
padding: 10px 16px;
display: flex;
align-items: center;
gap: 8px;
border-bottom: 1px solid #21262d;
}
.dot { width: 12px; height: 12px; border-radius: 50%; }
.dot.r { background: #ff5f56; }
.dot.y { background: #ffbd2e; }
.dot.g { background: #27c93f; }
.titlebar .title {
color: #8b949e;
font-size: 13px;
flex: 1;
text-align: center;
font-family: 'SF Mono', 'Fira Code', monospace;
}
.code-body {
padding: 14px 0;
font-family: 'SF Mono', 'Fira Code', 'Menlo', monospace;
font-size: 12px;
line-height: 1.55;
overflow-x: auto;
background: #0d1117;
max-height: 380px;
overflow-y: auto;
}
.code-line {
display: flex;
padding: 2px 16px;
cursor: pointer;
border-left: 3px solid transparent;
transition: background 0.15s ease;
}
.code-line.annotated { border-left-color: #1f6feb; background: rgba(31,111,235,0.06); }
.code-line.annotated:hover { background: rgba(31,111,235,0.22); }
.code-line.annotated.active { background: rgba(56,139,253,0.28); border-left-color: #58a6ff; }
.line-num { width: 36px; color: #484f58; text-align: right; padding-right: 16px; flex-shrink: 0; }
.line-content { white-space: pre; flex: 1; }
.kwd { color: #ff7b72; font-weight: 600; }
.fn { color: #d2a8ff; }
.str { color: #a5d6ff; }
.cmnt { color: #8b949e; font-style: italic; }
.explanation-dock {
background: #161b22;
border-top: 1px solid #30363d;
padding: 16px 20px;
min-height: 120px;
}
.badge {
font-size: 11px;
text-transform: uppercase;
font-weight: 700;
padding: 2px 8px;
border-radius: 12px;
}
.badge.key { background: rgba(255,166,87,0.15); color: #ffa657; border: 1px solid rgba(255,166,87,0.3); }
.badge.delta { background: rgba(56,139,253,0.15); color: #58a6ff; border: 1px solid rgba(56,139,253,0.3); }
.dock-header { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.dock-title { font-size: 14px; font-weight: 600; color: #f0f6fc; }
.dock-body { font-size: 13px; line-height: 1.5; color: #c9d1d9; }
.hint-text { font-size: 13px; color: #8b949e; }
</style>
</head>
<body>
<div class="explainer-container">
<div class="titlebar">
<span class="dot r"></span><span class="dot y"></span><span class="dot g"></span>
<span class="title">data/src/common/aca.py — start_and_wait()</span>
</div>
<div class="code-body">
<div class="code-line annotated" data-key="token" onclick="selectLine(this,'token')" onmouseenter="selectLine(this,'token')"><span class="line-num">27</span><span class="line-content"><span class="kwd">def</span> <span class="fn">azure_token</span>(scope=MANAGEMENT_SCOPE) -&gt; <span class="kwd">str</span>:</span></div>
<div class="code-line annotated" data-key="token" onclick="selectLine(this,'token')" onmouseenter="selectLine(this,'token')"><span class="line-num">40</span><span class="line-content"> <span class="kwd">return</span> <span class="fn">DefaultAzureCredential</span>().<span class="fn">get_token</span>(scope).token</span></div>
<div class="code-line"><span class="line-num">41</span><span class="line-content"></span></div>
<div class="code-line annotated" data-key="base" onclick="selectLine(this,'base')" onmouseenter="selectLine(this,'base')"><span class="line-num">62</span><span class="line-content"> base = <span class="str">f"https://management.azure.com/subscriptions/{subscription}…"</span></span></div>
<div class="code-line annotated" data-key="start" onclick="selectLine(this,'start')" onmouseenter="selectLine(this,'start')"><span class="line-num">68</span><span class="line-content"> request = urllib.request.<span class="fn">Request</span>(<span class="str">f"{base}/start?api-version=…"</span>, …)</span></div>
<div class="code-line annotated" data-key="start" onclick="selectLine(this,'start')" onmouseenter="selectLine(this,'start')"><span class="line-num">73</span><span class="line-content"> execution = started.<span class="fn">get</span>(<span class="str">"name"</span>, <span class="str">""</span>)</span></div>
<div class="code-line annotated" data-key="poll" onclick="selectLine(this,'poll')" onmouseenter="selectLine(this,'poll')"><span class="line-num">77</span><span class="line-content"> <span class="kwd">while</span> time.<span class="fn">monotonic</span>() &lt; deadline:</span></div>
<div class="code-line annotated" data-key="poll" onclick="selectLine(this,'poll')" onmouseenter="selectLine(this,'poll')"><span class="line-num">80</span><span class="line-content"> request = … <span class="str">f"{base}/executions?api-version=…"</span></span></div>
<div class="code-line annotated" data-key="poll" onclick="selectLine(this,'poll')" onmouseenter="selectLine(this,'poll')"><span class="line-num">85</span><span class="line-content"> current = <span class="fn">next</span>(run <span class="kwd">for</span> run <span class="kwd">in</span> executions <span class="kwd">if</span> run[<span class="str">"name"</span>] == execution)</span></div>
<div class="code-line annotated" data-key="wait" onclick="selectLine(this,'wait')" onmouseenter="selectLine(this,'wait')"><span class="line-num">88</span><span class="line-content"> <span class="kwd">if</span> status == SUCCEEDED: <span class="kwd">return</span> execution</span></div>
</div>
<div class="explanation-dock">
<div class="hint-text" id="hint">💡 Hover or tap blue lines. No `az` CLI on the Airflow VM — plain HTTP + identity token.</div>
<div id="dockContent" style="display:none;">
<div class="dock-header">
<span class="badge key" id="dockBadge">key</span>
<span class="dock-title" id="dockTitle"></span>
</div>
<div class="dock-body" id="dockBody"></div>
</div>
</div>
</div>
<script>
const explanations = {
token: {
type: 'key', badge: 'Identity',
title: 'DefaultAzureCredential',
body: 'On the prod VM: Managed Identity via IMDS. On your laptop (Astro): falls through to service principal in data/.env. One function, two environments — same as dbt_command() picking dev vs prod target.'
},
base: {
type: 'delta', badge: 'Management API',
title: 'Resource URL for the job',
body: 'Points at Microsoft.App/jobs/{job_name} in your team resource group. Subscription and RG come from Airflow Variables.'
},
start: {
type: 'delta', badge: 'POST /start',
title: 'Start one execution',
body: 'POST with Bearer token starts job-fp-ingest (or -dev). Response includes execution name — you need it to poll the right run when two overlap.'
},
poll: {
type: 'delta', badge: 'Poll loop',
title: 'GET /executions until done',
body: 'Sleep 15s, check status. Match by execution name, not "newest" — overlapping runs happen when someone triggers while the schedule is running.'
},
wait: {
type: 'key', badge: 'Why wait',
title: 'Task must not return early',
body: 'If ingest() returned after POST /start, Airflow would mark ingest green while the container still runs. dbt_build would then build on data that has not landed yet.'
}
};
function selectLine(el, key) {
const exp = explanations[key];
if (!exp) return;
document.querySelectorAll('.code-line').forEach(l => l.classList.remove('active'));
document.querySelectorAll(`.code-line[data-key="${key}"]`).forEach(l => l.classList.add('active'));
document.getElementById('hint').style.display = 'none';
document.getElementById('dockContent').style.display = 'block';
document.getElementById('dockBadge').textContent = exp.badge;
document.getElementById('dockBadge').className = 'badge ' + exp.type;
document.getElementById('dockTitle').textContent = exp.title;
document.getElementById('dockBody').textContent = exp.body;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment