From c11fcfacf6642ea32bfbb39861f166fa854e11e0 Mon Sep 17 00:00:00 2001 From: Samuel Lai Date: Thu, 27 Feb 2014 20:12:53 +1100 Subject: [PATCH] Fixes #9. Added ability for import_site command to resume importing if the connection to Solr is lost and restored. --- python/src/stackdump/commands/import_site.py | 22 ++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/python/src/stackdump/commands/import_site.py b/python/src/stackdump/commands/import_site.py index 3beebe9..6275075 100644 --- a/python/src/stackdump/commands/import_site.py +++ b/python/src/stackdump/commands/import_site.py @@ -13,6 +13,7 @@ import re import urllib2 import socket import tempfile +import traceback from optparse import OptionParser from xml.etree import ElementTree @@ -20,7 +21,7 @@ from sqlobject import sqlhub, connectionForURI, AND, IN, SQLObject, \ UnicodeCol, DateTimeCol, IntCol, DatabaseIndex, dbconnection from sqlobject.sqlbuilder import Delete, Insert from sqlobject.styles import DefaultStyle -from pysolr import Solr +from pysolr import Solr, SolrError from stackdump.models import Site, Badge, User from stackdump import settings @@ -538,7 +539,24 @@ class PostContentHandler(xml.sax.ContentHandler): By default, they are committed immediately. Set the ``commit`` argument to False to disable this behaviour. """ - self.solr.add(questions, commit=commit) + while True: + try: + self.solr.add(questions, commit=commit) + break + except SolrError, e: + print('An exception occurred while committing questions - ') + traceback.print_exc(file=sys.stdout) + print('') + while True: + response = raw_input('Try committing the questions again? (y/n) ').lower() + if response not in ('y', 'n'): + print("Answer either 'y' or 'n'. Answering 'n' will abort the import process.") + else: + print('') + if response == 'y': + break + else: + raise def commit_all_questions(self): """