def reindex_with_progress(waffle_when_done=False): """Rebuild elasticsearch index while updating progress bar for admins.""" try: # Init progress bar stuff: cache.set(ES_REINDEX_PROGRESS, 0.001) # An iota so it tests true in # the template if waffle_when_done: cache.set(ES_WAFFLE_WHEN_DONE, True) else: # Clear it in case there was already one there somehow: cache.delete(ES_WAFFLE_WHEN_DONE) # Reindex: start = time() for ratio in es_reindex_with_progress(): now = time() if now > start + settings.ES_REINDEX_PROGRESS_BAR_INTERVAL: # Update memcached only every so often. start = now # Format the string to avoid exponential notation, which seems # to be understood by JS but makes me nervous: cache.set(ES_REINDEX_PROGRESS, '%.5f' % ratio) if cache.get(ES_WAFFLE_WHEN_DONE): # Just go ahead and crash if the flag isn't there. flag = Flag.objects.get(name='elasticsearch') flag.everyone = True flag.save() finally: cache.delete(ES_REINDEX_PROGRESS) cache.delete(ES_WAFFLE_WHEN_DONE)
def reindex_with_progress(write_index): """Rebuild elasticsearch index while updating progress bar for admins. """ # Need to import Record here to prevent circular import from search.models import Record rec = Record( starttime=datetime.datetime.now(), text=u'Reindexing into %s' % write_index) rec.save() try: # Init progress bar stuff: cache.set(ES_REINDEX_PROGRESS, 0.001) # An iota so it tests # true in the template # Reindex: start = time() for ratio in es_reindex_with_progress(): now = time() if now > start + settings.ES_REINDEX_PROGRESS_BAR_INTERVAL: # Update memcached only every so often. start = now # Format the string to avoid exponential notation, # which seems to be understood by JS but makes me # nervous: cache.set(ES_REINDEX_PROGRESS, '%.5f' % ratio) rec.endtime = datetime.datetime.now() rec.save() except Exception: rec.text = (u'%s: Errored out %s %s' % ( rec.text, sys.exc_type, sys.exc_value)) rec.endtime = datetime.datetime.now() rec.save() raise finally: cache.delete(ES_REINDEX_PROGRESS)