0

Get search job status

by
Published today

Get a search job's properties (dispatchState, isDone, isFailed, doneProgress, resultCount, messages). Poll until dispatchState is DONE or FAILED. Returns { sid, ready: false } while the job is not ready yet.

Script splunk Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
2

3
/**
4
 * Get search job status
5
 * Get a search job's properties (dispatchState, isDone, isFailed, doneProgress, resultCount, messages). Poll until dispatchState is DONE or FAILED. Returns { sid, ready: false } while the job is not ready yet.
6
 */
7
export async function main(auth: RT.Splunk, sid: string) {
8
  const base = auth.base_url.replace(/\/+$/, "")
9
  const url = new URL(`${base}/services/search/jobs/${encodeURIComponent(sid)}`)
10
  url.searchParams.append("output_mode", "json")
11

12
  const response = await fetch(url, {
13
    headers: {
14
      Authorization: `Bearer ${auth.token}`,
15
      Accept: "application/json",
16
    },
17
  })
18

19
  if (!response.ok) {
20
    throw new Error(`${response.status} ${await response.text()}`)
21
  }
22

23
  if (response.status === 204) return { sid, ready: false }
24

25
  const data = (await response.json()) as { entry?: { content?: object }[] }
26
  return { sid, ready: true, ...data.entry?.[0]?.content }
27
}
28