diff --git a/celery_haystack/tasks.py b/celery_haystack/tasks.py index d8acedd..4041bc7 100644 --- a/celery_haystack/tasks.py +++ b/celery_haystack/tasks.py @@ -1,14 +1,13 @@ +from celery import Task # noqa +from celery.utils.log import get_task_logger +from django.apps import apps from django.core.exceptions import ImproperlyConfigured from django.core.management import call_command -from django.apps import apps - -from .conf import settings - -from haystack import connections, connection_router +from haystack import connection_router +from haystack import connections from haystack.exceptions import NotHandled as IndexNotFoundException -from celery.task import Task # noqa -from celery.utils.log import get_task_logger +from .conf import settings logger = get_task_logger(__name__) @@ -24,7 +23,7 @@ def split_identifier(self, identifier, **kwargs): Converts 'notes.note.23' into ('notes.note', 23). """ - bits = identifier.split('.') + bits = identifier.split(".") if len(bits) < 2: logger.error("Unable to parse object " @@ -33,15 +32,15 @@ def split_identifier(self, identifier, **kwargs): pk = bits[-1] # In case Django ever handles full paths... - object_path = '.'.join(bits[:-1]) + object_path = ".".join(bits[:-1]) return (object_path, pk) def get_model_class(self, object_path, **kwargs): """ Fetch the model's class in a standarized way. """ - bits = object_path.split('.') - app_name = '.'.join(bits[:-1]) + bits = object_path.split(".") + app_name = ".".join(bits[:-1]) classname = bits[-1] model_class = apps.get_model(app_name, classname) @@ -58,9 +57,11 @@ def get_instance(self, model_class, pk, **kwargs): try: instance = model_class._default_manager.get(pk=pk) except model_class.DoesNotExist: - logger.error("Couldn't load %s.%s.%s. Somehow it went missing?" % - (model_class._meta.app_label.lower(), - model_class._meta.object_name.lower(), pk)) + logger.error("Couldn't load %s.%s.%s. Somehow it went missing?" % ( + model_class._meta.app_label.lower(), + model_class._meta.object_name.lower(), + pk, + )) except model_class.MultipleObjectsReturned: logger.error("More than one object with pk %s. Oops?" % pk) return instance @@ -70,7 +71,8 @@ def get_indexes(self, model_class, **kwargs): Fetch the model's registered ``SearchIndex`` in a standarized way. """ try: - using_backends = connection_router.for_write(**{'models': [model_class]}) + using_backends = connection_router.for_write( + **{"models": [model_class]}) for using in using_backends: index_holder = connections[using].get_unified_index() yield index_holder.get_index(model_class), using @@ -93,10 +95,12 @@ def run(self, action, identifier, **kwargs): # Then get the model class for the object path model_class = self.get_model_class(object_path, **kwargs) for current_index, using in self.get_indexes(model_class, **kwargs): - current_index_name = ".".join([current_index.__class__.__module__, - current_index.__class__.__name__]) + current_index_name = ".".join([ + current_index.__class__.__module__, + current_index.__class__.__name__ + ]) - if action == 'delete': + if action == "delete": # If the object is gone, we'll use just the identifier # against the index. try: @@ -105,10 +109,10 @@ def run(self, action, identifier, **kwargs): logger.exception(exc) self.retry(exc=exc) else: - msg = ("Deleted '%s' (with %s)" % - (identifier, current_index_name)) + msg = "Deleted '%s' (with %s)" % (identifier, + current_index_name) logger.debug(msg) - elif action == 'update': + elif action == "update": # and the instance of the model class with the pk instance = self.get_instance(model_class, pk, **kwargs) if instance is None: @@ -124,8 +128,8 @@ def run(self, action, identifier, **kwargs): logger.exception(exc) self.retry(exc=exc) else: - msg = ("Updated '%s' (with %s)" % - (identifier, current_index_name)) + msg = "Updated '%s' (with %s)" % (identifier, + current_index_name) logger.debug(msg) else: logger.error("Unrecognized action '%s'. Moving on..." % action) @@ -137,19 +141,20 @@ class CeleryHaystackUpdateIndex(Task): A celery task class to be used to call the update_index management command from Celery. """ + def run(self, apps=None, **kwargs): defaults = { - 'batchsize': settings.CELERY_HAYSTACK_COMMAND_BATCH_SIZE, - 'age': settings.CELERY_HAYSTACK_COMMAND_AGE, - 'remove': settings.CELERY_HAYSTACK_COMMAND_REMOVE, - 'using': [settings.CELERY_HAYSTACK_DEFAULT_ALIAS], - 'workers': settings.CELERY_HAYSTACK_COMMAND_WORKERS, - 'verbosity': settings.CELERY_HAYSTACK_COMMAND_VERBOSITY, + "batchsize": settings.CELERY_HAYSTACK_COMMAND_BATCH_SIZE, + "age": settings.CELERY_HAYSTACK_COMMAND_AGE, + "remove": settings.CELERY_HAYSTACK_COMMAND_REMOVE, + "using": [settings.CELERY_HAYSTACK_DEFAULT_ALIAS], + "workers": settings.CELERY_HAYSTACK_COMMAND_WORKERS, + "verbosity": settings.CELERY_HAYSTACK_COMMAND_VERBOSITY, } defaults.update(kwargs) if apps is None: apps = settings.CELERY_HAYSTACK_COMMAND_APPS # Run the update_index management command logger.info("Starting update index") - call_command('update_index', *apps, **defaults) + call_command("update_index", *apps, **defaults) logger.info("Finishing update index")