0

Get Job

by
Published 4 days ago

Retrieve a single job's full definition (settings, tasks, schedule) by id.

Script databricks Verified

The script

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

3
export type DynSelect_job_id = string
4

5
// Dropdown of the workspace's jobs so users pick a job by name instead of typing its numeric id.
6
export async function job_id(auth: RT.Databricks) {
7
  const base = auth.workspace_url.replace(/\/$/, "")
8
  const response = await fetch(`${base}/api/2.2/jobs/list?limit=100`, {
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 { jobs } = (await response.json()) as {
18
    jobs?: { job_id: number; settings?: { name?: string } }[]
19
  }
20
  return (jobs ?? []).map((j) => ({
21
    value: String(j.job_id),
22
    label: `${j.settings?.name ?? "job"} (${j.job_id})`,
23
  }))
24
}
25

26
/**
27
 * Get Job
28
 * Retrieve a single job's full definition (settings, tasks, schedule) by id.
29
 */
30
export async function main(auth: RT.Databricks, job_id: DynSelect_job_id) {
31
  const base = auth.workspace_url.replace(/\/$/, "")
32
  const url = new URL(`${base}/api/2.2/jobs/get`)
33
  url.searchParams.append("job_id", job_id)
34

35
  const response = await fetch(url, {
36
    method: "GET",
37
    headers: {
38
      Authorization: `Bearer ${auth.token}`,
39
      Accept: "application/json",
40
    },
41
  })
42

43
  if (!response.ok) {
44
    throw new Error(`${response.status} ${await response.text()}`)
45
  }
46

47
  return await response.json()
48
}
49