silas / node-jenkins

Jenkins client

Home Page:https://www.npmjs.com/package/jenkins

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

job build returns queue id but queue is empty

rikiNeustadt opened this issue · comments

I tried to use @silas solution on this issue: #102.
I get an error of 'Error: jenkins: queue.item: not found.'
After building the job I get the queue ID but, somehow the queue is broken.
Please help

async function main() {
  const queueId = await jenkins.job.build({
        name: jobName,
        parameters: {
            user: username,
        }
    });
  console.log(queueId); //30960
  let queueItem;
  while (true) {
    queueItem = await jenkins.queue.item(queueId); //Error: jenkins: queue.item: not found

    if (queueItem.executable) {
      break;
    }

    if (queueItem.cancelled) {
      console.log('queue cancelled');
      return;
    }

    console.log('waiting on queue...');
    await new Promise(r => setTimeout(r, 1000));
  }

  let job;
  while (true) {
    job = await jenkins.build.get(jobName, queueItem.executable.number);

    if (!job.building) {
      break;
    }

    console.log('waiting on job...');
    await new Promise(r => setTimeout(r, 1000));
  }

  console.log(job.fullDisplayName, job.result);
}

main();

It's possible you have to handle a 404 from the queue endpoint and retry with some max retry timeout (I've seen issues that report that the queue is guaranteed to live for at least 5 minutes, so you could use that).