diff --git a/rq/scripts/__init__.py b/rq/scripts/__init__.py new file mode 100644 index 0000000..a616912 --- /dev/null +++ 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/bin/rqgenload b/rq/scripts/rqgenload.py similarity index 97% rename from bin/rqgenload rename to rq/scripts/rqgenload.py index a6e4cfd..b643c49 100755 --- a/bin/rqgenload +++ b/rq/scripts/rqgenload.py @@ -1,6 +1,6 @@ #!/usr/bin/env python import optparse -from rq import use_connection, Queue, Worker +from rq import use_connection, Queue from rq import dummy @@ -60,3 +60,4 @@ def main(): if __name__ == '__main__': main() + diff --git a/bin/rqinfo b/rq/scripts/rqinfo.py similarity index 90% rename from bin/rqinfo rename to rq/scripts/rqinfo.py index 851b4ec..71c302c 100755 --- a/bin/rqinfo +++ 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 @@ -177,6 +174,3 @@ def main(): interval(args.interval, func, args) except ConnectionError as e: print(e) - -if __name__ == '__main__': - main() diff --git a/bin/rqworker b/rq/scripts/rqworker.py similarity index 78% rename from bin/rqworker rename to rq/scripts/rqworker.py index e6a72e2..06c3a7c 100755 --- a/bin/rqworker +++ 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): @@ -27,7 +28,6 @@ def setup_loghandlers(args): loglevel = logbook.INFO formatter = format_colors - import sys handlers.NullHandler(bubble=False).push_application() handler = handlers.StreamHandler(sys.stdout, level=loglevel, bubble=False) if formatter: @@ -41,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') @@ -61,17 +59,10 @@ 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) w.work(burst=args.burst) except ConnectionError as e: print(e) - - -if __name__ == '__main__': - main() diff --git a/setup.py b/setup.py index 97a3189..3adac2e 100644 --- a/setup.py +++ b/setup.py @@ -42,7 +42,11 @@ setup( zip_safe=False, platforms='any', install_requires=get_dependencies(), - scripts=['bin/rqinfo', 'bin/rqworker'], + entry_points='''\ + [console_scripts] + rqworker = rq.scripts.rqworker:main + rqinfo = rq.scripts.rqinfo:main + ''', classifiers=[ # As from http://pypi.python.org/pypi?%3Aaction=list_classifiers #'Development Status :: 1 - Planning',