Replace optparse with the more flexible argparse.

We now have real subcommands.
main
Vincent Driessen 13 years ago
parent c820ba24fa
commit 56d05c3509

@ -2,7 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import time import time
import optparse import argparse
from rq import use_redis, Queue, Worker from rq import use_redis, Queue, Worker
from rq.utils import gettermsize, make_colorizer from rq.utils import gettermsize, make_colorizer
@ -34,30 +34,10 @@ def state_symbol(state):
return state return state
def parse_args(): def show_queues(args):
parser = optparse.OptionParser()
parser.add_option('-q', '--queues', dest='subcmd',
action='store_const', const='queues',
help='Shows stats for queues.')
parser.add_option('-w', '--workers', dest='subcmd',
action='store_const', const='workers',
help='Shows stats for workers.')
parser.add_option('-n', '--interval', dest='interval',
type='float',
help='The interval between polls, in seconds. Does not poll if 0.')
parser.add_option('-r', '--raw', dest='raw',
action='store_true', default=False,
help='Print only the raw numbers, no bar charts.')
parser.add_option('-Q', '--by-queue', dest='by_queue',
default=False, action='store_true',
help='Shows workers by queue.')
opts, args = parser.parse_args()
return (opts, args, parser)
def show_queues(opts, args, parser):
while True: while True:
if len(args): if len(args.queues):
qs = map(Queue, args) qs = map(Queue, args.queues)
else: else:
qs = Queue.all() qs = Queue.all()
@ -74,12 +54,12 @@ def show_queues(opts, args, parser):
scale = get_scale(max_count) scale = get_scale(max_count)
ratio = chartwidth * 1.0 / scale ratio = chartwidth * 1.0 / scale
if opts.interval: if args.interval:
os.system('clear') os.system('clear')
for q in qs: for q in qs:
count = counts[q] count = counts[q]
if not opts.raw: if not args.raw:
chart = green('|' + '█' * int(ratio * count)) chart = green('|' + '█' * int(ratio * count))
line = '%-12s %s %d' % (q.name, chart, count) line = '%-12s %s %d' % (q.name, chart, count)
else: else:
@ -87,19 +67,22 @@ def show_queues(opts, args, parser):
print(line) print(line)
num_jobs += count num_jobs += count
print('%d queues, %d jobs total' % (len(qs), num_jobs))
if opts.interval: # Print summary when not in raw mode
time.sleep(opts.interval) if not args.raw:
print('%d queues, %d jobs total' % (len(qs), num_jobs))
if args.interval:
time.sleep(args.interval)
else: else:
break break
def show_workers(opts, args, parser): def show_workers(args):
while True: while True:
qs = Queue.all() qs = Queue.all()
ws = Worker.all() ws = Worker.all()
if opts.interval: if args.interval:
os.system('clear') os.system('clear')
queues = {qname: [] for qname in qs} queues = {qname: [] for qname in qs}
@ -109,7 +92,7 @@ def show_workers(opts, args, parser):
queues[q] = [] queues[q] = []
queues[q].append(w) queues[q].append(w)
if opts.by_queue: if args.by_queue:
max_qname = max(map(lambda q: len(q.name), queues.keys())) max_qname = max(map(lambda q: len(q.name), queues.keys()))
for q in queues: for q in queues:
if queues[q]: if queues[q]:
@ -122,25 +105,36 @@ def show_workers(opts, args, parser):
print '%s %s: %s' % (w.name, state_symbol(w.state), ', '.join(w.queue_names())) print '%s %s: %s' % (w.name, state_symbol(w.state), ', '.join(w.queue_names()))
print '%d workers, %d queues' % (len(ws), len(queues)) print '%d workers, %d queues' % (len(ws), len(queues))
if opts.interval: if args.interval:
time.sleep(opts.interval) time.sleep(args.interval)
else: else:
break break
def main(): def parse_args():
opts, args, parser = parse_args() parser = argparse.ArgumentParser(description='awesome')
if not opts.subcmd: parent_parser = argparse.ArgumentParser(add_help=False)
parser.error('Specify either --queues or --workers.') parent_parser.add_argument('--interval', '-i', metavar='N', type=float, default=0, help='Updates stats every N seconds (default: don\'t poll)')
use_redis() subparsers = parser.add_subparsers()
if opts.subcmd == 'workers': queues_p = subparsers.add_parser('queues', parents=[parent_parser], help='Show queue info')
show_workers(opts, args, parser) queues_p.add_argument('--raw', '-r', action='store_true', default=False, help='Print only the raw numbers, no bar charts')
elif opts.subcmd == 'queues': queues_p.add_argument('queues', nargs='*', help='The queues to poll')
show_queues(opts, args, parser) queues_p.set_defaults(func=show_queues)
workers_p = subparsers.add_parser('workers', parents=[parent_parser], help='Show worker activity')
workers_p.add_argument('--by-queue', '-Q', dest='by_queue', default=False, action='store_true', help='Shows workers by queue')
workers_p.set_defaults(func=show_workers)
return parser.parse_args()
def main():
args = parse_args()
use_redis()
args.func(args)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save