From 365d1d8226a8094d872c5a83f3fc26546d6f83b0 Mon Sep 17 00:00:00 2001 From: Graeme Coupar Date: Mon, 3 Sep 2012 00:48:52 +0100 Subject: [PATCH 1/5] Fixed argument handling in rqinfo rqinfo was using None for the redis host and port, which was causing exceptions when trying to connect. It was possible to supply these on the commandline, but port was just being passed straight through as a string, which also caused exceptions. This commit moves some of the argument & configuration file handling out of rqworker and into the scripts/__init__.py file, and then calls it from rqinfo. --- rq/scripts/__init__.py | 18 ++++++++++++++++++ rq/scripts/rqinfo.py | 8 ++++++++ rq/scripts/rqworker.py | 18 +++++------------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/rq/scripts/__init__.py b/rq/scripts/__init__.py index e615f8d..d80e9f4 100644 --- a/rq/scripts/__init__.py +++ b/rq/scripts/__init__.py @@ -13,6 +13,24 @@ def add_standard_arguments(parser): help='The Redis database (default: 0)') +def read_config_file(module): + """Reads all UPPERCASE variables defined in the given module file.""" + settings = __import__(module, [], [], [], -1) + return {k: v for k, v in settings.__dict__.items() if k.upper() == k} + + +def setup_default_arguments(args, settings): + """ Sets up args from settings or defaults """ + if args.host is None: + args.host = settings.get('REDIS_HOST', 'localhost') + if args.port is None: + args.port = int(settings.get('REDIS_PORT', 6379)) + else: + args.port = int(args.port) + if args.db is None: + args.db = settings.get('REDIS_DB', 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 0782cd3..6920783 100755 --- a/rq/scripts/rqinfo.py +++ b/rq/scripts/rqinfo.py @@ -9,6 +9,8 @@ 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 +from rq.scripts import read_config_file +from rq.scripts import setup_default_arguments red = make_colorizer('darkred') green = make_colorizer('darkgreen') @@ -162,6 +164,12 @@ def main(): if args.path: sys.path = args.path.split(':') + sys.path + settings = {} + if args.config: + settings = read_config_file(args.config) + + setup_default_arguments(args, settings) + setup_redis(args) try: if args.only_queues: diff --git a/rq/scripts/rqworker.py b/rq/scripts/rqworker.py index c870989..be38188 100755 --- a/rq/scripts/rqworker.py +++ b/rq/scripts/rqworker.py @@ -7,6 +7,8 @@ from rq import Queue, Worker from redis.exceptions import ConnectionError from rq.scripts import add_standard_arguments from rq.scripts import setup_redis +from rq.scripts import read_config_file +from rq.scripts import setup_default_arguments def format_colors(record, handler): @@ -53,12 +55,6 @@ def parse_args(): return parser.parse_args() -def read_config_file(module): - """Reads all UPPERCASE variables defined in the given module file.""" - settings = __import__(module, [], [], [], -1) - return {k: v for k, v in settings.__dict__.items() if k.upper() == k} - - def main(): args = parse_args() @@ -69,13 +65,9 @@ def main(): if args.config: settings = read_config_file(args.config) - # Default arguments - if args.host is None: - args.host = settings.get('REDIS_HOST', 'localhost') - if args.port is None: - args.port = int(settings.get('REDIS_PORT', 6379)) - if args.db is None: - args.db = settings.get('REDIS_DB', 0) + setup_default_arguments(args, settings) + + # Other default arguments if args.sentry_dsn is None: args.sentry_dsn = settings.get('SENTRY_DSN', None) From 2f578a4c2dec3f8baa2db364197ced089ae1d4eb Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Mon, 3 Sep 2012 08:08:33 +0200 Subject: [PATCH 2/5] Avoid dict comprehension for compatibility. This fixes #128. --- rq/scripts/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rq/scripts/__init__.py b/rq/scripts/__init__.py index d80e9f4..a675385 100644 --- a/rq/scripts/__init__.py +++ b/rq/scripts/__init__.py @@ -16,7 +16,9 @@ def add_standard_arguments(parser): def read_config_file(module): """Reads all UPPERCASE variables defined in the given module file.""" settings = __import__(module, [], [], [], -1) - return {k: v for k, v in settings.__dict__.items() if k.upper() == k} + return dict([(k, v) + for k, v in settings.__dict__.items() + if k.upper() == k]) def setup_default_arguments(args, settings): From f9137311b56280f8dc9c610034f0a1bc1803e25b Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Mon, 3 Sep 2012 08:13:22 +0200 Subject: [PATCH 3/5] Release version 0.3.2. --- CHANGES.md | 6 +++++- rq/version.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 461ccaa..c19b90e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ ### 0.3.2 -(not released yet) +(September 3rd, 2012) + +- Fixes broken `rqinfo` command. + +- Improve compatibility with Python < 2.7. diff --git a/rq/version.py b/rq/version.py index 64a0641..9cd79b5 100644 --- a/rq/version.py +++ b/rq/version.py @@ -1 +1 @@ -VERSION = '0.3.1' +VERSION = '0.3.2' From 86dc1e353373995e82bab5caca52b443fc66f7ec Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Mon, 3 Sep 2012 08:15:47 +0200 Subject: [PATCH 4/5] Prepare for next version. --- CHANGES.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index c19b90e..6f18cee 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,8 @@ +### 0.3.3 +(not released yet) + + + ### 0.3.2 (September 3rd, 2012) From 8581cd6463db0faf5f59db73f72cf89a76e9912f Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Mon, 3 Sep 2012 08:40:07 +0200 Subject: [PATCH 5/5] Don't allow jobs without Redis connection. And other connection-related fixes. --- rq/job.py | 11 +++++------ rq/queue.py | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/rq/job.py b/rq/job.py index a8c7937..0b88cd8 100644 --- a/rq/job.py +++ b/rq/job.py @@ -4,7 +4,8 @@ import times from collections import namedtuple from uuid import uuid4 from cPickle import loads, dumps, UnpicklingError -from .connections import get_current_connection +from .local import LocalStack +from .connections import resolve_connection from .exceptions import UnpickleError, NoSuchJobError @@ -135,9 +136,9 @@ class Job(object): return self._kwargs @classmethod - def exists(cls, job_id): + def exists(cls, job_id, connection=None): """Returns whether a job hash exists for the given job ID.""" - conn = get_current_connection() + conn = resolve_connection(connection) return conn.exists(cls.key_for(job_id)) @classmethod @@ -150,9 +151,7 @@ class Job(object): return job def __init__(self, id=None, connection=None): - if connection is None: - connection = get_current_connection() - self.connection = connection + self.connection = resolve_connection(connection) self._id = id self.created_at = times.now() self._func_name = None diff --git a/rq/queue.py b/rq/queue.py index 6dca8e1..7dd90d2 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -98,7 +98,7 @@ class Queue(object): job_id = self.connection.lpop(COMPACT_QUEUE) if job_id is None: break - if Job.exists(job_id): + if Job.exists(job_id, self.connection): self.connection.rpush(self.key, job_id)