From 26f013750196c5028135216de386496696b02a09 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Mon, 9 Mar 2015 14:41:06 +0000 Subject: Moved usage-information to foot of script. This removes the nasty self-parsing of the script to generate the help/manual output. I've also removed global-variables and made the code almost-100% warning free. --- bin/custodian-queue | 176 ++++++++++++++++++++++------------------------------ 1 file changed, 74 insertions(+), 102 deletions(-) (limited to 'bin/custodian-queue') diff --git a/bin/custodian-queue b/bin/custodian-queue index dcb4b9a..fcbf2b3 100755 --- a/bin/custodian-queue +++ b/bin/custodian-queue @@ -1,90 +1,47 @@ -#!/usr/bin/ruby -Ilib/ -I../lib/ -rubygems +#!/usr/bin/ruby -rubygems # -# NAME -# custodian-queue - Work with the queue. -# -# SYNOPSIS -# custodian-queue [ -h | --help ] -# [ -m | --manual ] -# [ -M | --monitor ] -# [ -f | --flush ] -# [ -s | --stats ] -# -# OPTIONS +# Usage information at the end of the script. # -# -h, --help Show a help message, and exit. -# -# -m, --manual Show this manual, and exit. -# -# -M, --monitor If the queue size exceeds the threshold 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 beanstalkd queue, or flush the pending -# jobs from it. -# -# -# AUTHOR -# -# Steve Kemp -# - - -# -# Standard modules -# require 'getoptlong' - - -# -# Our code -# require 'custodian/settings' require 'custodian/queue' - # # Entry-point to our code. # -if __FILE__ == $PROGRAM_NAME then - - $FLUSH = false - $STATS = false - $help = false - $manual = false - $MONITOR = nil +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] + ['--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 + if arg + monitor = arg.to_i + else + monitor = 5000 + end when '--stats' then - $STATS = true + stats = true when '--flush' then - $FLUSH = true + flush = true when '--help' then - $help = true + help = true when '--manual' then - $manual = true + manual = true end end rescue StandardError => ex @@ -93,31 +50,12 @@ if __FILE__ == $PROGRAM_NAME then end # - # CAUTION! Here be quality kode. + # Show the help information. # - if $manual or $help - - # Open the file, stripping the shebang line - lines = File.open(__FILE__){|fh| fh.readlines}[1..-1] - - found_synopsis = false - - lines.each do |line| - - line.chomp! - break if line.empty? - - if $help and !found_synopsis - found_synopsis = (line =~ /^#\s+SYNOPSIS\s*$/) - next - end - - puts line[2..-1].to_s - - break if $help and found_synopsis and line =~ /^#\s*$/ - + if manual || help + DATA.read.split("\n").each do |line| + puts Regexp.last_match(1).dup if line =~ /^# ?(.*)/ end - exit 0 end @@ -130,37 +68,71 @@ if __FILE__ == $PROGRAM_NAME then # # Alerting on a queue that is too-full? # - if !$MONITOR.nil? + unless monitor.nil? # # Find the number of jobs # jobs = queue.size? - if jobs > $MONITOR - exit 1 + if jobs > monitor + exit 1 else - exit 0 + exit 0 end end - # # Showing stats? # - if $STATS - jobs = queue.size? - puts "There are #{jobs || 0} jobs pending." - exit(0) + if stats + jobs = queue.size? + puts "There are #{jobs || 0} jobs pending." + exit(0) end - # # Are we flushing the queue? # - if $FLUSH - queue.flush! - end - + 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 +# -- cgit v1.2.1