Edits history of script submission #22662 for ' Run Job Now (databricks)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_job_id = string
    
    // Dropdown of the workspace's jobs so users pick a job by name instead of typing its numeric id.
    export async function job_id(auth: RT.Databricks) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const response = await fetch(`${base}/api/2.2/jobs/list?limit=100`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { jobs } = (await response.json()) as {
        jobs?: { job_id: number; settings?: { name?: string } }[]
      }
      return (jobs ?? []).map((j) => ({
        value: String(j.job_id),
        label: `${j.settings?.name ?? "job"} (${j.job_id})`,
      }))
    }
    
    /**
     * Run Job Now
     * Trigger a one-off run of an existing job. Pass run parameters in params (e.g. {"job_parameters": {...}}, {"notebook_params": {...}}, {"python_params": [...]}). Returns run_id; poll Get Job Run for completion.
     */
    export async function main(
      auth: RT.Databricks,
      job_id: DynSelect_job_id,
      params: { [key: string]: any }
    ) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const url = new URL(`${base}/api/2.2/jobs/run-now`)
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({ job_id: Number(job_id), ...params }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago