Graphql api: getting jobs with wrong states

hello,

i have this graphql query:

{
    organization(slug: "xxxx") {
        agents(first: 100) {
            edges {
                node {
                    name
                    jobs(first: 10, state: [FINISHED]) {
                        edges {
                            node {
                                ... on JobTypeCommand {
                                    build {
                                        id
                                        number
                                        state
                                        createdAt
                                        finishedAt
                                        pipeline {
                                            name
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

I’m getting back a lot of jobs with the state RUNNING. Am I doing something wrong?

What I intend to get out of this query is a list of the latest jobs run by each agent, so that we know the health of that agent (i.e. did it fail the last jobs? does it fail often?)

Thanks in advance for your help

Hi!
You can use the following query to fetch all of the ‘finished’ jobs by selecting the specific cluster.

query getScheduledJobsInAClusterQueue{
 organization(slug: "{org-slug}") {
    jobs(
      first: 500,
      clusterQueue: "{cluster-queue-graphql-id}",
      state: [SCHEDULED]
    ){
      count
      pageInfo{
        endCursor
        hasNextPage
      }
      edges{
        node{
          ... on JobTypeCommand {
            uuid
            pipeline{
              name
            }
            build{
              number
            }
            url
            state
          }
        }
      }
    }
  }
}

Also, as an alternative, here is the url, where you can search Jobs by the states
https://buildkite.com/organizations/{your_org_slug}/jobs?q=state%3Afinished.

Cheers,

Hello!

That query doesn’t yield any results. All our agents are self-hosted, but we don’t have any cluster set up. They’re all unclustered. I’m wondering if that’s why we don’t get results back. I’ve tried the query without the clusterQueue param too, but got no results.

Also, I’m not looking for scheduled jobs like you did on your query, only for finished ones (failed, succeeded or canceled).

What is the issue with the query I posted in the first place? Why suggest something else? (serious question, I don’t have much experience with graphql, and want to understand)

Thank you

Fo sure ! Let me answer your questions above.

Why your query doesn’t work:
If you go to the GraphQl documentation you will see
agent(slug: ID!)
To get jobs running on a specific “each” agent, you have to specify the agent ID using the agent(slug: ID!) field in the GraphQL API. This means providing the full agent slug in the format your_org_slug/your_agent_id.

query getAgentJobs {
  agent(slug: "your_org_slug/your_agent_id") {
    uuid
    jobs(first: 10,state: [CANCELED]) {
      count
      edges {
        node {
          ... on JobTypeCommand {
            label
            url
            uuid
          }
        }
      }
    }
  }
}

Regarding my query above, if you don’t use cluster, you can delete cluster from it. Something like that:

query getFinishedJobs{
 organization(slug: "your_org_slug") {
    jobs(
      first: 500,
      state: [FINISHED,CANCELED] #Put any state you want, from the list https://buildkite.com/docs/apis/graphql/schemas/enum/jobstates, in my example SCHEDULED was an example. =) 
    ){
      count
      pageInfo{
        endCursor
        hasNextPage
      }
      edges{
        node{
          ... on JobTypeCommand {
            uuid
            pipeline{
              name
            }
            build{
              number
            }
            url
            state
          }
        }
      }
    }
  }
}

Regarding the state, you can specify a couple of states or use one from the list below: JobStates – Enums – GraphQL API | Buildkite Documentation.
Let me know if you have any questions!
Thanks,