silas / node-jenkins

Jenkins client
https://www.npmjs.com/package/jenkins
MIT License
356 stars 85 forks source link

job build returns queue id but queue is empty #105

Open rikiNeustadt opened 2 years ago

rikiNeustadt commented 2 years ago

I tried to use @silas solution on this issue: https://github.com/silas/node-jenkins/issues/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();
silas commented 2 years ago

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).