Edits history of script submission #22646 for ' Cancel Job Run (databricks)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_run_id = string
    
    // Dropdown of recent active runs so users pick a run instead of typing its id.
    export async function run_id(auth: RT.Databricks) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const response = await fetch(
        `${base}/api/2.2/jobs/runs/list?active_only=true&limit=25`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { runs } = (await response.json()) as {
        runs?: {
          run_id: number
          run_name?: string
          state?: { life_cycle_state?: string }
        }[]
      }
      return (runs ?? []).map((r) => ({
        value: String(r.run_id),
        label: `${r.run_name ?? "run"} (${r.run_id}) — ${r.state?.life_cycle_state ?? "?"}`,
      }))
    }
    
    /**
     * Cancel Job Run
     * Request cancellation of an active job run. Returns immediately; the run transitions to TERMINATED asynchronously.
     */
    export async function main(auth: RT.Databricks, run_id: DynSelect_run_id) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const url = new URL(`${base}/api/2.2/jobs/runs/cancel`)
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({ run_id: Number(run_id) }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      if (response.status === 204) return { success: true }
      const text = await response.text()
      return text ? JSON.parse(text) : { success: true }
    }
    

    Submitted by hugo989 5 days ago