diff --git a/rq/scripts/__init__.py b/rq/scripts/__init__.py index e69de29..a616912 100644 --- a/rq/scripts/__init__.py +++ b/rq/scripts/__init__.py @@ -0,0 +1,16 @@ +import redis +from rq import use_connection + + +def add_standard_arguments(parser): + parser.add_argument('--host', '-H', default='localhost', + help='The Redis hostname (default: localhost)') + parser.add_argument('--port', '-p', type=int, default=6379, + help='The Redis portnumber (default: 6379)') + parser.add_argument('--db', '-d', type=int, default=0, + help='The Redis database (default: 0)') + + +def setup_redis(args): + redis_conn = redis.Redis(host=args.host, port=args.port, db=args.db) + use_connection(redis_conn) diff --git a/rq/scripts/rqinfo.py b/rq/scripts/rqinfo.py index c695d95..71c302c 100755 --- a/rq/scripts/rqinfo.py +++ b/rq/scripts/rqinfo.py @@ -4,10 +4,11 @@ import sys import os import time import argparse -import redis from redis.exceptions import ConnectionError -from rq import use_connection, Queue, Worker +from rq import Queue, Worker from rq.utils import gettermsize, make_colorizer +from rq.scripts import add_standard_arguments +from rq.scripts import setup_redis red = make_colorizer('darkred') green = make_colorizer('darkgreen') @@ -133,9 +134,7 @@ def show_both(args): def parse_args(): parser = argparse.ArgumentParser(description='RQ command-line monitor.') - parser.add_argument('--host', '-H', default='localhost', help='The Redis hostname (default: localhost)') - parser.add_argument('--port', '-p', type=int, default=6379, help='The Redis portnumber (default: 6379)') - parser.add_argument('--db', '-d', type=int, default=0, help='The Redis database (default: 0)') + add_standard_arguments(parser) parser.add_argument('--path', '-P', default='.', help='Specify the import path.') parser.add_argument('--interval', '-i', metavar='N', type=float, default=2.5, help='Updates stats every N seconds (default: don\'t poll)') parser.add_argument('--raw', '-r', action='store_true', default=False, help='Print only the raw numbers, no bar charts') @@ -163,9 +162,7 @@ def main(): if args.path: sys.path = args.path.split(':') + sys.path - # Setup connection to Redis - redis_conn = redis.Redis(host=args.host, port=args.port, db=args.db) - use_connection(redis_conn) + setup_redis(args) try: if args.only_queues: func = show_queues diff --git a/rq/scripts/rqworker.py b/rq/scripts/rqworker.py index 1a4bc76..06c3a7c 100755 --- a/rq/scripts/rqworker.py +++ b/rq/scripts/rqworker.py @@ -2,10 +2,11 @@ import sys import argparse import logbook -import redis from logbook import handlers -from rq import use_connection, Queue, Worker +from rq import Queue, Worker from redis.exceptions import ConnectionError +from rq.scripts import add_standard_arguments +from rq.scripts import setup_redis def format_colors(record, handler): @@ -40,9 +41,7 @@ def setup_loghandlers(args): def parse_args(): parser = argparse.ArgumentParser(description='Starts an RQ worker.') - parser.add_argument('--host', '-H', default='localhost', help='The Redis hostname (default: localhost)') - parser.add_argument('--port', '-p', type=int, default=6379, help='The Redis portnumber (default: 6379)') - parser.add_argument('--db', '-d', type=int, default=0, help='The Redis database (default: 0)') + add_standard_arguments(parser) parser.add_argument('--burst', '-b', action='store_true', default=False, help='Run in burst mode (quit after all work is done)') parser.add_argument('--name', '-n', default=None, help='Specify a different name') @@ -60,10 +59,7 @@ def main(): sys.path = args.path.split(':') + sys.path setup_loghandlers(args) - - # Setup connection to Redis - redis_conn = redis.Redis(host=args.host, port=args.port, db=args.db) - use_connection(redis_conn) + setup_redis(args) try: queues = map(Queue, args.queues) w = Worker(queues, name=args.name)