Class: JenkinsApi::Client::BuildQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins_api_client/build_queue.rb

Overview

This classes communicates with the Build Queue API exposed by Jenkins at “/queue” that gives information about jobs/tasks in the queue and their details.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ BuildQueue

Initializes a new BuildQueue object.

Parameters:

  • client (Object)

    a reference to Client



35
36
37
# File 'lib/jenkins_api_client/build_queue.rb', line 35

def initialize(client)
  @client = client
end

Instance Method Details

#get_age(task_name) ⇒ Fixnum

Gets the time number of seconds the task is in the queue

Parameters:

  • task_name (String)

    Name of the task/job

Returns:

  • (Fixnum)

    age in seconds



69
70
71
72
73
74
75
76
# File 'lib/jenkins_api_client/build_queue.rb', line 69

def get_age(task_name)
  age = nil
  details = get_details(task_name)
  unless details.empty?
    age = Time.now - Time.at(details["inQueueSince"].to_i/1000)
  end
  age
end

#get_causes(task_name) ⇒ Array

Obtains the causes from the build queue for the specified task

Parameters:

  • task_name (String)

Returns:

  • (Array)

    causes for the task to be in queue



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jenkins_api_client/build_queue.rb', line 99

def get_causes(task_name)
  causes = nil
  details = get_details(task_name)
  unless details.empty?
    causes = []
    details["actions"].each do |action|
      causes << action["causes"]
    end
  end
  causes
end

#get_details(task_name) ⇒ Hash

Obtains the detail Hash from the API response

Parameters:

  • task_name (String)

    Name of the task/job

Returns:

  • (Hash)

    Queue details of the specified task/job



84
85
86
87
88
89
90
91
# File 'lib/jenkins_api_client/build_queue.rb', line 84

def get_details(task_name)
  response_json = @client.api_get_request("/queue")
  details = {}
  response_json["items"].each do |item|
    details = item if item["task"]["name"]
  end
  details
end

#get_eta(task_name) ⇒ String

Obtains the ETA given by Jenkins if any

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (String)

    ETA for the task, nil if no task found and N/A for tasks with no ETA info.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jenkins_api_client/build_queue.rb', line 133

def get_eta(task_name)
  eta = nil
  details = get_details(task_name)
  unless details.empty?
    matched = details["why"].match(/.*\(ETA:(.*)\)/)
    if matched.nil?
      # Task is found, but ETA information is not available
      eta = "N/A"
    else
      # ETA information is available
      eta = matched[1].strip
    end
  end
  eta
end

#get_id(task_name) ⇒ String

Obtains the ID of the task from the queue

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (String)

    ID of the task, nil of no task is found



155
156
157
158
159
160
161
162
# File 'lib/jenkins_api_client/build_queue.rb', line 155

def get_id(task_name)
  id = nil
  details = get_details(task_name)
  unless details.empty?
    id = details["id"]
  end
  id
end

#get_params(task_name) ⇒ String

Obtains the params from the build queue

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (String)

    params, nil if the no task is found



170
171
172
173
174
175
176
177
# File 'lib/jenkins_api_client/build_queue.rb', line 170

def get_params(task_name)
  params = nil
  details = get_details(task_name)
  unless details.empty?
    params = details["params"]
  end
  params
end

#get_reason(task_name) ⇒ String

Obtains the reason why the task is in queue

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (String)

    reason for being in queue, nil if no task found



117
118
119
120
121
122
123
124
# File 'lib/jenkins_api_client/build_queue.rb', line 117

def get_reason(task_name)
  reason = nil
  details = get_details(task_name)
  unless details.empty?
    reason = details["why"]
  end
  reason
end

#is_blocked?(task_name) ⇒ TrueClass|FalseClass

Obtains whether the task is blocked

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (TrueClass|FalseClass)

    blocked or not



200
201
202
203
204
205
206
207
# File 'lib/jenkins_api_client/build_queue.rb', line 200

def is_blocked?(task_name)
  blocked = nil
  details = get_details(task_name)
  unless details.empty?
    blocked = details["blocked"]
  end
  blocked
end

#is_buildable?(task_name) ⇒ TrueClass|FalseClass

Obtains whether the task is buildable

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (TrueClass|FalseClass)

    buildable or not



185
186
187
188
189
190
191
192
# File 'lib/jenkins_api_client/build_queue.rb', line 185

def is_buildable?(task_name)
  buildable = nil
  details = get_details(task_name)
  unless details.empty?
    buildable = details["buildable"]
  end
  buildable
end

#is_stuck?(task_name) ⇒ TrueClass|FalseClass

Obtains whether the task is stuck

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (TrueClass|FalseClass)

    stuck or not



215
216
217
218
219
220
221
222
# File 'lib/jenkins_api_client/build_queue.rb', line 215

def is_stuck?(task_name)
  stuck = nil
  details = get_details(task_name)
  unless details.empty?
    stuck = details["stuck"]
  end
  stuck
end

#listObject

Lists all tasks currently in the build queue



54
55
56
57
58
59
60
61
# File 'lib/jenkins_api_client/build_queue.rb', line 54

def list
  response_json = @client.api_get_request("/queue")
  tasks = []
  response_json["items"].each do |item|
    tasks << item["task"]["name"]
  end
  tasks
end

#sizeObject

Gives the number of jobs currently in the build queue



47
48
49
50
# File 'lib/jenkins_api_client/build_queue.rb', line 47

def size
  response_json = @client.api_get_request("/queue")
  response_json["items"].size
end

#to_sObject

Returns a string representation of BuildQueue class.



41
42
43
# File 'lib/jenkins_api_client/build_queue.rb', line 41

def to_s
  "#<JenkinsApi::Client::BuildQueue>"
end