summaryrefslogtreecommitdiff
path: root/bin/custodian-queue
blob: fcbf2b3e64fe051f4661a9eff378eedb831beb02 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/ruby -rubygems
#
# Usage information at the end of the script.
#

require 'getoptlong'
require 'custodian/settings'
require 'custodian/queue'

#
#  Entry-point to our code.
#
if __FILE__ == $PROGRAM_NAME

  flush   = false
  stats   = false
  help    = false
  manual  = false
  monitor = nil

  begin
    opts = GetoptLong.new(
      ['--flush',   '-f', GetoptLong::NO_ARGUMENT],
      ['--help',    '-h', GetoptLong::NO_ARGUMENT],
      ['--manual',  '-m', GetoptLong::NO_ARGUMENT],
      ['--monitor', '-M', GetoptLong::OPTIONAL_ARGUMENT],
      ['--stats',   '-s', GetoptLong::NO_ARGUMENT]
                          )
    opts.each do |opt, arg|
      case opt
      when '--monitor' then
        if  arg
          monitor = arg.to_i
        else
          monitor = 5000
        end
      when '--stats' then
        stats = true
      when '--flush' then
        flush = true
      when '--help' then
        help = true
      when '--manual' then
        manual = true
      end
    end
  rescue StandardError => ex
    puts "Option parsing failed: #{ex}"
    exit
  end

  #
  # Show the help information.
  #
  if manual || help
    DATA.read.split("\n").each do |line|
      puts Regexp.last_match(1).dup if line =~ /^# ?(.*)/
    end
    exit 0
  end

  #
  #  Create the queue object.
  #
  settings = Custodian::Settings.instance
  queue = Custodian::QueueType.create(settings.queue_type)

  #
  # Alerting on a queue that is too-full?
  #
  unless monitor.nil?

    #
    # Find the number of jobs
    #
    jobs = queue.size?

    if jobs > monitor
      exit 1
    else
      exit 0
    end
  end

  #
  # Showing stats?
  #
  if stats
    jobs = queue.size?
    puts "There are #{jobs || 0} jobs pending."
    exit(0)
  end

  #
  #  Are we flushing the queue?
  #
  queue.flush! if flush

end


__END__
# NAME
#  custodian-queue - Work with the queue.
#
# SYNOPSIS
#  custodian-queue  [ -h | --help ]
#                   [ -m | --manual ]
#                   [ -M | --monitor ]
#                   [ -f | --flush ]
#                   [ -s | --stats ]
#
# OPTIONS
#
#  -h, --help          Show a help message, and exit.
#
#  -m, --manual        Show this manual, and exit.
#
#  -M, --monitor       If the queue size exceeds the given threshold
#                      then report that via an exit code.
#
#  -f, --flush         Flush the queue, removing all jobs.
#
#  -s, --stats         Show the count of pending jobs.
#
#
# ABOUT
#
# This tool is designed to inspect the global queue, and also allows that
# queue to be flushed of all pending-jobs.
#
# The queue may be redis-based, or beanstalkd-based, as defined by the
# global configuration-file.
#
# AUTHOR
#
#  Steve Kemp  <steve@bytemark.co.uk>
#