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 (Client)

    the client object



37
38
39
40
# File 'lib/jenkins_api_client/build_queue.rb', line 37

def initialize(client)
  @client = client
  @logger = @client.logger
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



74
75
76
77
78
79
80
81
82
83
# File 'lib/jenkins_api_client/build_queue.rb', line 74

def get_age(task_name)
  @logger.info "Obtaining the age of task '#{task_name}' from the" +
    " build queue"
  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



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jenkins_api_client/build_queue.rb', line 119

def get_causes(task_name)
  @logger.info "Obtaining the causes of task '#{task_name}' from the" +
    " build queue"
  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



91
92
93
94
95
96
97
98
99
100
# File 'lib/jenkins_api_client/build_queue.rb', line 91

def get_details(task_name)
  @logger.info "Obtaining the details of task '#{task_name}' from the" +
    " build queue"
  response_json = @client.api_get_request("/queue")
  details = {}
  response_json["items"].each do |item|
    details = item if item["task"]["name"] == 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.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/jenkins_api_client/build_queue.rb', line 157

def get_eta(task_name)
  @logger.info "Obtaining the ETA for the task '#{task_name}' from the" +
    " build queue"
  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



181
182
183
184
185
186
187
188
189
190
# File 'lib/jenkins_api_client/build_queue.rb', line 181

def get_id(task_name)
  @logger.info "Obtaining the ID of task '#{task_name}' from the" +
    " build queue"
  id = nil
  details = get_details(task_name)
  unless details.empty?
    id = details["id"]
  end
  id
end

#get_item_by_id(task_id) ⇒ Hash

Obtain the item in the queue provided the ID of the task

Parameters:

  • task_id (String)

    the ID of the task

Returns:

  • (Hash)

    the details of the item in the queue



108
109
110
111
# File 'lib/jenkins_api_client/build_queue.rb', line 108

def get_item_by_id(task_id)
  @logger.info "Obtaining the details of task with ID '#{task_id}'"
  @client.api_get_request("/queue/item/#{task_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



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

def get_params(task_name)
  @logger.info "Obtaining the build parameters of task '#{task_name}'" +
    " from the build queue"
  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



139
140
141
142
143
144
145
146
147
148
# File 'lib/jenkins_api_client/build_queue.rb', line 139

def get_reason(task_name)
  @logger.info "Obtaining the reason of task '#{task_name}' from the" +
    " build queue"
  reason = nil
  details = get_details(task_name)
  unless details.empty?
    reason = details["why"]
  end
  reason
end

#is_blocked?(task_name) ⇒ Boolean

Obtains whether the task is blocked

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (Boolean)

    blocked or not



232
233
234
235
236
237
238
239
240
241
# File 'lib/jenkins_api_client/build_queue.rb', line 232

def is_blocked?(task_name)
  @logger.info "Checking if task '#{task_name}' from the build queue" +
    " is blocked"
  blocked = nil
  details = get_details(task_name)
  unless details.empty?
    blocked = details["blocked"]
  end
  blocked
end

#is_buildable?(task_name) ⇒ Boolean

Obtains whether the task is buildable

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (Boolean)

    buildable or not



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

def is_buildable?(task_name)
  @logger.info "Checking if task '#{task_name}' from the build queue" +
    " is buildable"
  buildable = nil
  details = get_details(task_name)
  unless details.empty?
    buildable = details["buildable"]
  end
  buildable
end

#is_stuck?(task_name) ⇒ Boolean

Obtains whether the task is stuck

Parameters:

  • task_name (String)

    name of the task

Returns:

  • (Boolean)

    stuck or not



249
250
251
252
253
254
255
256
257
258
# File 'lib/jenkins_api_client/build_queue.rb', line 249

def is_stuck?(task_name)
  @logger.info "Checking if task '#{task_name}' from the build queue" +
    " is stuck"
  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



58
59
60
61
62
63
64
65
66
# File 'lib/jenkins_api_client/build_queue.rb', line 58

def list
  @logger.info "Obtaining the tasks currently in the build queue"
  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



50
51
52
53
54
# File 'lib/jenkins_api_client/build_queue.rb', line 50

def size
  @logger.info "Obtaining the number of tasks in build queue"
  response_json = @client.api_get_request("/queue")
  response_json["items"].size
end

#to_sObject

Returns a string representation of BuildQueue class.



44
45
46
# File 'lib/jenkins_api_client/build_queue.rb', line 44

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