From dc30a4e658e49999b03271256c477bcad1f4f955 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Fri, 26 Oct 2012 13:25:31 +0200 Subject: [PATCH] add retry option to reindex --- apps/search/management/commands/reindex.py | 55 ++++++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/apps/search/management/commands/reindex.py b/apps/search/management/commands/reindex.py index 2d2000bb9..7032565a0 100755 --- a/apps/search/management/commands/reindex.py +++ b/apps/search/management/commands/reindex.py @@ -1,6 +1,40 @@ +import sys from django.core.management.base import BaseCommand from optparse import make_option + +def query_yes_no(question, default="yes"): + """Ask a yes/no question via raw_input() and return their answer. + + "question" is a string that is presented to the user. + "default" is the presumed answer if the user just hits . + It must be "yes" (the default), "no" or None (meaning + an answer is required of the user). + + The "answer" return value is one of "yes" or "no". + """ + valid = {"yes":True, "y":True, "ye":True, + "no":False, "n":False} + if default == None: + prompt = " [y/n] " + elif default == "yes": + prompt = " [Y/n] " + elif default == "no": + prompt = " [y/N] " + else: + raise ValueError("invalid default answer: '%s'" % default) + + while True: + sys.stdout.write(question + prompt) + choice = raw_input().lower() + if default is not None and choice == '': + return valid[default] + elif choice in valid: + return valid[choice] + else: + sys.stdout.write("Please respond with 'yes' or 'no' "\ + "(or 'y' or 'n').\n") + class Command(BaseCommand): help = 'Reindex everything.' args = '' @@ -25,15 +59,26 @@ class Command(BaseCommand): else: books += Book.objects.filter(slug=a).all() else: - books = Book.objects.all() + books = list(Book.objects.all()) - try: - for b in books: + while books: + try: + b = books[0] print b.title idx.index_book(b) idx.index.commit() - except: - idx.index.rollback() + books.pop(0) + except Exception, e: + print "Error occured: %s" % e + try: + # we might not be able to rollback + idx.index.rollback() + except: + pass + retry = query_yes_no("Retry?") + if not retry: + break + print 'Reindexing tags.' idx.index_tags() idx.index.commit() -- 2.20.1