-
Notifications
You must be signed in to change notification settings - Fork 10
/
jenkins_unqueue.rb
executable file
·65 lines (53 loc) · 1.43 KB
/
jenkins_unqueue.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby
# coding: utf-8
# frozen_string_literal: true
require 'logger'
require 'logger/colors'
require 'optparse'
require_relative 'lib/jenkins'
require_relative 'lib/retry'
require_relative 'lib/thread_pool'
parser = OptionParser.new do |opts|
opts.banner = <<-EOS
Usage: jenkins_unqueue.rb 'regex'
regex must be a valid Ruby regular expression matching the jobs you wish to
unqueue.
Only jobs that queued can be removed from the queue (obviously)
e.g.
• All build jobs for vivid and utopic:
'^(vivid|utopic)_.*_.*'
• All unstable builds:
'^.*_unstable_.*'
• All jobs:
'.*'
EOS
end
parser.parse!
@log = Logger.new(STDOUT).tap do |l|
l.progname = 'unqueue'
l.level = Logger::INFO
end
abort parser.help if ARGV.empty?
pattern = Regexp.new(ARGV[0])
@log.info pattern
job_name_queue = Queue.new
job_names = Jenkins.client.queue.list
job_names.each do |name|
next unless pattern.match(name)
job_name_queue << name
end
BlockingThreadPool.run do
until job_name_queue.empty?
name = job_name_queue.pop(true)
Retry.retry_it(times: 5) do
id = Jenkins.client.queue.get_id(name)
@log.info "unqueueing #{name} (#{id})"
begin
Jenkins.client.api_post_request('/queue/cancelItem', id: id)
rescue => e
# jenkins returns 204 and the api gem doesn't know what to do with that
raise e unless e.message == 'Error code 204'
end
end
end
end