Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-51198

Jenkins' CauseOfBlockage through the API

XMLWordPrintable

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Minor Minor
    • core
    • None

      Hi,

       

      It may sound like a duplicate of JENKINS-41579 but it's not.

      It's currently not possible to have the CauseOfBlockage through the json API.

      There are currently three half-baked ways to do this, but none are usable for me.

      To reproduce, create a pipeline with the following groovy script:

      node('build3') { // change for a name that you possess and one that you don't
        stage('test') {
          sleep 5
        }
      }
      

      First way

      How to do it

      When accessing  {JENKINS_URL}/queue/api/json , it shows (among other) the following piece of data:

       

      {
            "_class": "hudson.model.Queue$BuildableItem",
            "actions": [
              {}
            ],
            "blocked": false,
            "buildable": true,
            "id": 264,
            "inQueueSince": 1525768721244,
            "params": "",
            "stuck": true,
            "task": {
              "_class": "org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution$PlaceholderTask"
            },
            "url": "queue/item/264/",
            "why": "There are no nodes with the label ‘build3’",
            "buildableStartMilliseconds": 1525768721245,
            "pending": false
      },
      

      The other why messages could be, for example:
      "why": "build2 is offline",
       

      I believe all possible messages are located there: https://github.com/jenkinsci/jenkins/tree/1feb23e55ca807a664cf89986241155f24038aa5/core/src/main/resources/hudson/model/queue/CauseOfBlockage

      The problem

      I cannot use this information for 2 reasons:

      1. it is language dependent
      2. BECAUSE_LABEL_IS_BUSY and BECAUSE_NODE_IS_BUSY result in the same string
      3. the message is for humans, not made for automation

      Second way

      How to do it

      When accessing  {JENKINS_URL}/blue/rest/organizations/jenkins/pipelines/{pipeline_name}/queue/ , it should show the piece of data given in https://issues.jenkins-ci.org/browse/JENKINS-41579 .

      When I do it with my current queue (4 items in the queue), I see this:

      []
      

      Not really useful, eh?

      And with another pipeline, I see (among other lines) this:

      "causeOfBlockage": "Build #160 is already in progress (ETA:N/A)",
      

      The problem

      I think this one is self explanatory. However, there are other issues to it:

      1. All queued items don't appear
      2. When it does appear, the message is for humans, not made for automation.
      3. It is pipeline specific. I couldn't find a way to have this info for all pipelines (or only the latest run within each pipeline)
      4. It relies on Blue Ocean to be installed

       

      Third way

      How to do it

      When accessing  {JENKINS_URL}/blue/rest/organizations/jenkins/pipelines/{pipeline_name}/runs/ , it shows (among other) the following piece of data:

        {
          "_class": "io.jenkins.blueocean.rest.impl.pipeline.PipelineRunImpl",
          "_links": {
            "parent": {
              "_class": "io.jenkins.blueocean.rest.hal.Link",
              "href": "/blue/rest/organizations/jenkins/pipelines/short pipeline/"
            },
            "tests": {
              "_class": "io.jenkins.blueocean.rest.hal.Link",
              "href": "/blue/rest/organizations/jenkins/pipelines/short pipeline/runs/87/tests/"
            },
            "nodes": {
              "_class": "io.jenkins.blueocean.rest.hal.Link",
              "href": "/blue/rest/organizations/jenkins/pipelines/short pipeline/runs/87/nodes/"
            },
            "log": {
              "_class": "io.jenkins.blueocean.rest.hal.Link",
              "href": "/blue/rest/organizations/jenkins/pipelines/short pipeline/runs/87/log/"
            },
            "self": {
              "_class": "io.jenkins.blueocean.rest.hal.Link",
              "href": "/blue/rest/organizations/jenkins/pipelines/short pipeline/runs/87/"
            },
            "actions": {
              "_class": "io.jenkins.blueocean.rest.hal.Link",
              "href": "/blue/rest/organizations/jenkins/pipelines/short pipeline/runs/87/actions/"
            },
            "steps": {
              "_class": "io.jenkins.blueocean.rest.hal.Link",
              "href": "/blue/rest/organizations/jenkins/pipelines/short pipeline/runs/87/steps/"
            },
            "artifacts": {
              "_class": "io.jenkins.blueocean.rest.hal.Link",
              "href": "/blue/rest/organizations/jenkins/pipelines/short pipeline/runs/87/artifacts/"
            }
          },
          "actions": [],
          "artifactsZipFile": null,
          "causeOfBlockage": "There are no nodes with the label ‘build3’",
          "causes": [
            {
              "_class": "io.jenkins.blueocean.service.embedded.rest.AbstractRunImpl$BlueCauseImpl",
              "shortDescription": "Started by user XXX",
              "userId": "xxx",
              "userName": "XXX"
            }
          ],
          "changeSet": [],
          "description": null,
          "durationInMillis": 0,
          "enQueueTime": "2018-05-08T08:38:37.482+0000",
          "endTime": null,
          "estimatedDurationInMillis": 102598,
          "id": "87",
          "name": null,
          "organization": "jenkins",
          "pipeline": "{pipeline_name}",
          "replayable": true,
          "result": "UNKNOWN",
          "runSummary": "?",
          "startTime": "2018-05-08T08:38:37.486+0000",
          "state": "QUEUED",
          "type": "WorkflowRun",
          "branch": null,
          "commitId": null,
          "commitUrl": null,
          "pullRequest": null
        },
      

      We can see the following line:

      "causeOfBlockage": "There are no nodes with the label ‘build3’",
      

      So, here we have the information again.

      The problem

      It comes with several problems:

      1. It is pipeline specific
      2. the message is for humans, not made for automation
      3. I could not find a way to only retrieve queued items. There seems to be a way for that given here https://github.com/jenkinsci/blueocean-plugin/tree/master/blueocean-rest#find-latest-run-on-all-pipelines (and adapting with status:QUEUED or something), but {JENKINS_URL}/blue/rest/ gives me a 404.
      4. If relies on Blue Ocean to be installed

       

      What I propose (a possible solution)

      When accessing {JENKINS_URL}/queue/api/json/ :

      A new key:

      "requires": [
          {
              "_class": "org.jenkinsci.operators.Operator$OR",
              "operands": [
                  {
                      "_class": "org.jenkinsci.[...].Label",
                      "name": "agent"
                  },
                  {
                      "_class": "org.jenkinsci.[...].Label",
                      "name": "for_xxx"
                  },
                  {
                      "_class": "org.jenkinsci.[...].Label",
                      "name": "other_label"
                  },
                  {
                      "_class": "org.jenkinsci.operators.Operator$AND",
                      "operands": [
                      {
                          "_class": "org.jenkinsci.[...].Node",
                          "name": "build2"
                      },
                      {
                          "_class": "org.jenkinsci.[...].Label",
                          "name": "debug"
                      },
                      ]
                  }
              ]
          }
      ]
      

      That would explain what is required in order to unblock the queued item. In the above example, that would mean "an executor on a computer with a label 'agent', 'for_xxx' or 'other_label' or on a computer named 'build2' with label 'debug'".

      The grammar is obviously just a draft/proposal and would need some reflection first.

      But the goal would be (independently on the solution taken) to know what the job needs in order to run to make it easier (or at least reliable) to provision resource for jenkins.

       

       

       

       

            Unassigned Unassigned
            mulugruntz Samuel Giffard
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: