diff --git a/README.rst b/README.rst index a924b83a..db36da9d 100644 --- a/README.rst +++ b/README.rst @@ -46,6 +46,7 @@ Installation 'DB': 0, 'PASSWORD': 'some-password', 'DEFAULT_TIMEOUT': 360, + 'EXCEPTION_HANDLERS': ['path.to.my.handler'], # If you need custom exception handlers in your queue. If not just delete this. }, 'high': { 'URL': os.getenv('REDISTOGO_URL', 'redis://localhost:6379/0'), # If you're on Heroku @@ -58,7 +59,6 @@ Installation } } - RQ_EXCEPTION_HANDLERS = ['path.to.my.handler'] # If you need custom exception handlers * Include ``django_rq.urls`` in your ``urls.py``: diff --git a/django_rq/management/commands/rqworker.py b/django_rq/management/commands/rqworker.py index f70a8336..b6d92b36 100644 --- a/django_rq/management/commands/rqworker.py +++ b/django_rq/management/commands/rqworker.py @@ -77,7 +77,8 @@ def handle(self, *args, **options): queues, connection=queues[0].connection, name=options['name'], - exception_handlers=get_exception_handlers() or None, + exception_handlers=get_exception_handlers( + queues[0].exception_handlers) or None, default_worker_ttl=options['worker_ttl'] ) diff --git a/django_rq/queues.py b/django_rq/queues.py index 86cf53de..8bf705b5 100644 --- a/django_rq/queues.py +++ b/django_rq/queues.py @@ -49,6 +49,7 @@ class DjangoRQ(Queue): def __init__(self, *args, **kwargs): autocommit = kwargs.pop('autocommit', None) self._autocommit = get_commit_mode() if autocommit is None else autocommit + self.exception_handlers = kwargs.pop('exception_handlers', None) super(DjangoRQ, self).__init__(*args, **kwargs) @@ -138,9 +139,13 @@ def get_queue(name='default', default_timeout=None, async=None, default_timeout = QUEUES[name].get('DEFAULT_TIMEOUT') if queue_class is None: queue_class = get_queue_class(QUEUES[name]) + + exception_handlers = QUEUES[name].get('EXCEPTION_HANDLERS', None) + return queue_class(name, default_timeout=default_timeout, connection=get_connection(name), async=async, - autocommit=autocommit) + autocommit=autocommit, + exception_handlers=exception_handlers) def get_queue_by_index(index): @@ -170,9 +175,10 @@ def filter_connection_params(queue_params): """ NON_CONNECTION_PARAMS = ('DEFAULT_TIMEOUT',) - #return {p:v for p,v in queue_params.items() if p not in NON_CONNECTION_PARAMS} + # return {p:v for p,v in queue_params.items() if p not in NON_CONNECTION_PARAMS} # Dict comprehension compatible with python 2.6 - return dict((p,v) for (p,v) in queue_params.items() if p not in NON_CONNECTION_PARAMS) + return dict((p, v) for (p, v) in queue_params.items() + if p not in NON_CONNECTION_PARAMS) def get_queues(*queue_names, **kwargs): diff --git a/django_rq/test_exception_handler.py b/django_rq/test_exception_handler.py new file mode 100644 index 00000000..fded0e9a --- /dev/null +++ b/django_rq/test_exception_handler.py @@ -0,0 +1,3 @@ + +def my_custom_exception(job, exc_type, exc_value, traceback): + return False \ No newline at end of file diff --git a/django_rq/test_settings.py b/django_rq/test_settings.py index 28256508..1208d7e9 100644 --- a/django_rq/test_settings.py +++ b/django_rq/test_settings.py @@ -99,7 +99,7 @@ 'HOST': REDIS_HOST, 'PORT': 6379, 'DB': 0, - 'DEFAULT_TIMEOUT': 500 + 'DEFAULT_TIMEOUT': 500, }, 'test': { 'HOST': REDIS_HOST, diff --git a/django_rq/workers.py b/django_rq/workers.py index 057ae449..c924eff8 100644 --- a/django_rq/workers.py +++ b/django_rq/workers.py @@ -1,18 +1,31 @@ from rq import Worker from rq.utils import import_attribute -from .queues import get_queues from .settings import EXCEPTION_HANDLERS +from .queues import get_queues -def get_exception_handlers(): - """ - Custom exception handlers could be defined in settings.py: - RQ = { - 'EXCEPTION_HANDLERS': ['path.to.handler'], +def get_exception_handlers(queue_name): + """Custom exception handler defined in QUEUE settings: + RQ_QUEUES = { + 'default': { + 'HOST': 'localhost', + 'PORT': 6379, + 'DB': 0, + 'PASSWORD': '', + 'DEFAULT_TIMEOUT': 360, + 'EXCEPTION_HANDLERS': [ + 'test_exception_handler.my_custom_exception' + ], + } } """ - return [import_attribute(path) for path in EXCEPTION_HANDLERS] + + if queue_name: + return [import_attribute(exception_handler) for exception_handler in + queue_name] + else: + return [import_attribute(path) for path in EXCEPTION_HANDLERS] def get_worker(*queue_names): @@ -20,6 +33,13 @@ def get_worker(*queue_names): Returns a RQ worker for all queues or specified ones. """ queues = get_queues(*queue_names) - return Worker(queues, - connection=queues[0].connection, - exception_handlers=get_exception_handlers() or None) + exception_handlers = get_exception_handlers(queues[0].exception_handlers) + + if exception_handlers: + return Worker( + queues, + connection=queues[0].connection, + exception_handlers=exception_handlers + ) + else: + return Worker(queues, connection=queues[0].connection)