Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jenkins): respect context when fetching builds #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,26 @@ func (j *Jenkins) BuildJob(ctx context.Context, name string, params map[string]s
return job.InvokeSimple(ctx, params)
}

// A task in queue will be assigned a build number in a job after a few seconds.
// this function will return the build object.
// GetBuildFromQueueID polls until a task is assigned a build number or the context is cancelled.
func (j *Jenkins) GetBuildFromQueueID(ctx context.Context, job *Job, queueid int64) (*Build, error) {
task, err := j.GetQueueItem(ctx, queueid)
if err != nil {
return nil, err
}

// Jenkins queue API has about 4.7second quiet period
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

for task.Raw.Executable.Number == 0 {
time.Sleep(1000 * time.Millisecond)
_, err = task.Poll(ctx)
if err != nil {
return nil, err
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-ticker.C:
_, err = task.Poll(ctx)
if err != nil {
return nil, err
}
}
}

Expand Down