Changeset 888 in main


Ignore:
Timestamp:
03/13/12 14:57:35 (10 years ago)
Author:
pcosquer
Message:

search: remove index asynchronously to avoid to a database lock error if a file is deleted just after an insertion

Location:
trunk/openPLM
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/plmapp/search_indexes.py

    r746 r888  
    33from django.conf import settings 
    44from django.db.models import signals 
    5 from django.db.models.loading import get_model 
    65 
    76from haystack import site 
     
    98from haystack.indexes import * 
    109from haystack.models import SearchResult 
     10from haystack.utils import get_identifier 
    1111 
    1212import openPLM.plmapp.models as models 
    13 from openPLM.plmapp.tasks import update_index 
     13from openPLM.plmapp.tasks import update_index, remove_index 
    1414 
    1515# just a hack to prevent a KeyError 
     
    2626# by sdcooke 
    2727 
    28 def remove_instance_from_index(instance): 
    29     model_class = get_model(instance._meta.app_label, instance._meta.module_name) 
    30     search_index = site.get_index(model_class) 
    31     search_index.remove_object(instance) 
    32  
    3328class QueuedSearchIndex(indexes.SearchIndex): 
    3429    """ 
    35 A ``SearchIndex`` subclass that enqueues updates for later processing. 
    36  
    37 Deletes are handled instantly since a reference, not the instance, is put on the queue. It would not be hard 
    38 to update this to handle deletes as well (with a delete task). 
    39 """ 
     30    A ``SearchIndex`` subclass that enqueues updates for later processing. 
     31    """ 
    4032    # We override the built-in _setup_* methods to connect the enqueuing operation. 
    4133    def _setup_save(self, model): 
     
    5749 
    5850    def enqueue_delete(self, instance, **kwargs): 
    59         remove_instance_from_index(instance) 
     51        remove_index.delay(instance._meta.app_label, 
     52                instance._meta.module_name, get_identifier(instance)) 
    6053 
    6154################## 
  • trunk/openPLM/plmapp/tasks.py

    r619 r888  
    6161update_indexes = synchronized(update_indexes, update_index.lock) 
    6262 
     63 
     64@task(default_retry_delay = 60, max_retries = 10) 
     65def remove_index(app_name, model_name, identifier): 
     66    from haystack import site 
     67    import openPLM.plmapp.search_indexes 
     68 
     69    model_class = get_model(app_name, model_name) 
     70    search_index = site.get_index(model_class) 
     71    search_index.remove_object(identifier) 
     72remove_index = synchronized(remove_index, update_index.lock) 
     73 
     74 
    6375@task 
    6476def add(a, b): 
  • trunk/openPLM/plmapp/tests/views.py

    r864 r888  
    23072307        results = self.search("pppp.txt", "Document") 
    23082308        self.assertEqual([df], results) 
     2309        # ensure a delete file is not matched 
     2310        doc.delete_file(df) 
     2311        results = self.search("monocle", "Document") 
     2312        self.assertEqual([], results) 
    23092313 
    23102314    def test_search_in_odt(self): 
  • trunk/openPLM/settings.py

    r883 r888  
    117117    "openPLM.plmapp.tasks.update_index": {"queue": "index"}, 
    118118    "openPLM.plmapp.tasks.update_indexes": {"queue": "index"}, 
     119    "openPLM.plmapp.tasks.remove_index": {"queue": "index"}, 
    119120    "openPLM.plmapp.mail.do_send_histories_mail" : {"queue" : "mails"}, 
    120121    "openPLM.plmapp.mail.do_send_mail" : {"queue" : "mails"}, 
Note: See TracChangeset for help on using the changeset viewer.