Clean up Worker.__init__ logic a little

main
Vincent Driessen 10 years ago
parent 4d0ae5da98
commit 891be55b87

@ -13,8 +13,9 @@ import datetime
import importlib import importlib
import logging import logging
import sys import sys
from collections import Iterable
from .compat import as_text, is_python_version from .compat import as_text, is_python_version, string_types
class _Colorizer(object): class _Colorizer(object):
@ -205,6 +206,19 @@ def first(iterable, default=None, key=None):
return default return default
def is_nonstring_iterable(obj):
"""Returns whether the obj is an iterable, but not a string"""
return isinstance(obj, Iterable) and not isinstance(obj, string_types)
def ensure_list(obj):
"""
When passed an iterable of objects, does nothing, otherwise, it returns
a list with just that object in it.
"""
return obj if is_nonstring_iterable(obj) else [obj]
def current_timestamp(): def current_timestamp():
"""Returns current UTC timestamp""" """Returns current UTC timestamp"""
return calendar.timegm(datetime.datetime.utcnow().utctimetuple()) return calendar.timegm(datetime.datetime.utcnow().utctimetuple())

@ -2,7 +2,6 @@
from __future__ import (absolute_import, division, print_function, from __future__ import (absolute_import, division, print_function,
unicode_literals) unicode_literals)
from datetime import timedelta
import errno import errno
import logging import logging
import os import os
@ -13,6 +12,7 @@ import sys
import time import time
import traceback import traceback
import warnings import warnings
from datetime import timedelta
from rq.compat import as_text, string_types, text_type from rq.compat import as_text, string_types, text_type
@ -20,11 +20,12 @@ from .connections import get_current_connection
from .exceptions import DequeueTimeout from .exceptions import DequeueTimeout
from .job import Job, JobStatus from .job import Job, JobStatus
from .logutils import setup_loghandlers from .logutils import setup_loghandlers
from .queue import get_failed_queue, Queue from .queue import Queue, get_failed_queue
from .registry import clean_registries, FinishedJobRegistry, StartedJobRegistry from .registry import FinishedJobRegistry, StartedJobRegistry, clean_registries
from .suspension import is_suspended from .suspension import is_suspended
from .timeouts import UnixSignalDeathPenalty from .timeouts import UnixSignalDeathPenalty
from .utils import enum, import_attribute, make_colorizer, utcformat, utcnow, utcparse from .utils import (ensure_list, enum, import_attribute, make_colorizer,
utcformat, utcnow, utcparse)
from .version import VERSION from .version import VERSION
try: try:
@ -126,7 +127,9 @@ class Worker(object):
if connection is None: if connection is None:
connection = get_current_connection() connection = get_current_connection()
self.connection = connection self.connection = connection
queues = self.process_queue_args(queues)
queues = [self.queue_class(name=q) if isinstance(q, text_type) else q
for q in ensure_list(queues)]
self._name = name self._name = name
self.queues = queues self.queues = queues
self.validate_queues() self.validate_queues()
@ -163,15 +166,7 @@ class Worker(object):
"""Sanity check for the given queues.""" """Sanity check for the given queues."""
for queue in self.queues: for queue in self.queues:
if not isinstance(queue, self.queue_class): if not isinstance(queue, self.queue_class):
raise TypeError('{0} is not a Queue or a string'.format(queue)) raise TypeError('{0} is not of type {1} or text type'.format(queue, self.queue_class))
def process_queue_args(self, queue_args):
"""Allow for a string, a queue an iterable of strings or an iterable of queues"""
if isinstance(queue_args, text_type):
return self.queue_class(name=queue_args)
else:
return [self.queue_class(name=queue_arg) if isinstance(queue_arg, text_type) else queue_arg
for queue_arg in queue_args]
def queue_names(self): def queue_names(self):
"""Returns the queue names of this worker's queues.""" """Returns the queue names of this worker's queues."""

Loading…
Cancel
Save