0

Cancel Job Run

by
Published 4 days ago

Request cancellation of an active job run. Returns immediately; the run transitions to TERMINATED asynchronously.

Script databricks Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
export type DynSelect_run_id = string
4

5
// Dropdown of recent active runs so users pick a run instead of typing its id.
6
export async function run_id(auth: RT.Databricks) {
7
  const base = auth.workspace_url.replace(/\/$/, "")
8
  const response = await fetch(
9
    `${base}/api/2.2/jobs/runs/list?active_only=true&limit=25`,
10
    {
11
      headers: {
12
        Authorization: `Bearer ${auth.token}`,
13
        Accept: "application/json",
14
      },
15
    }
16
  )
17
  if (!response.ok) {
18
    throw new Error(`${response.status} ${await response.text()}`)
19
  }
20
  const { runs } = (await response.json()) as {
21
    runs?: {
22
      run_id: number
23
      run_name?: string
24
      state?: { life_cycle_state?: string }
25
    }[]
26
  }
27
  return (runs ?? []).map((r) => ({
28
    value: String(r.run_id),
29
    label: `${r.run_name ?? "run"} (${r.run_id}) — ${r.state?.life_cycle_state ?? "?"}`,
30
  }))
31
}
32

33
/**
34
 * Cancel Job Run
35
 * Request cancellation of an active job run. Returns immediately; the run transitions to TERMINATED asynchronously.
36
 */
37
export async function main(auth: RT.Databricks, run_id: DynSelect_run_id) {
38
  const base = auth.workspace_url.replace(/\/$/, "")
39
  const url = new URL(`${base}/api/2.2/jobs/runs/cancel`)
40

41
  const response = await fetch(url, {
42
    method: "POST",
43
    headers: {
44
      Authorization: `Bearer ${auth.token}`,
45
      "Content-Type": "application/json",
46
      Accept: "application/json",
47
    },
48
    body: JSON.stringify({ run_id: Number(run_id) }),
49
  })
50

51
  if (!response.ok) {
52
    throw new Error(`${response.status} ${await response.text()}`)
53
  }
54

55
  if (response.status === 204) return { success: true }
56
  const text = await response.text()
57
  return text ? JSON.parse(text) : { success: true }
58
}
59