0

Get Job Run

by
Published 4 days ago

Retrieve a single run's metadata, state and task results by run id.

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 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(`${base}/api/2.2/jobs/runs/list?limit=25`, {
9
    headers: {
10
      Authorization: `Bearer ${auth.token}`,
11
      Accept: "application/json",
12
    },
13
  })
14
  if (!response.ok) {
15
    throw new Error(`${response.status} ${await response.text()}`)
16
  }
17
  const { runs } = (await response.json()) as {
18
    runs?: {
19
      run_id: number
20
      run_name?: string
21
      state?: { life_cycle_state?: string }
22
    }[]
23
  }
24
  return (runs ?? []).map((r) => ({
25
    value: String(r.run_id),
26
    label: `${r.run_name ?? "run"} (${r.run_id}) — ${r.state?.life_cycle_state ?? "?"}`,
27
  }))
28
}
29

30
/**
31
 * Get Job Run
32
 * Retrieve a single run's metadata, state and task results by run id.
33
 */
34
export async function main(
35
  auth: RT.Databricks,
36
  run_id: DynSelect_run_id,
37
  include_history: boolean | undefined
38
) {
39
  const base = auth.workspace_url.replace(/\/$/, "")
40
  const url = new URL(`${base}/api/2.2/jobs/runs/get`)
41
  url.searchParams.append("run_id", run_id)
42
  if (include_history !== undefined)
43
    url.searchParams.append("include_history", String(include_history))
44

45
  const response = await fetch(url, {
46
    method: "GET",
47
    headers: {
48
      Authorization: `Bearer ${auth.token}`,
49
      Accept: "application/json",
50
    },
51
  })
52

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

57
  return await response.json()
58
}
59