don't hang if Solr example fails to start

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1407083 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2012-11-08 14:00:28 +00:00
parent 1897641123
commit c85875c33e
1 changed files with 43 additions and 23 deletions

View File

@ -463,27 +463,29 @@ def cygwinifyPaths(command):
if '; ant ' in command: command = reUnixPath.sub(unix2win, command) if '; ant ' in command: command = reUnixPath.sub(unix2win, command)
return command return command
def printFileContents(fileName):
# Assume log file was written in system's default encoding, but
# even if we are wrong, we replace errors ... the ASCII chars
# (which is what we mostly care about eg for the test seed) should
# still survive:
txt = codecs.open(fileName, 'r', encoding=sys.getdefaultencoding(), errors='replace').read()
# Encode to our output encoding (likely also system's default
# encoding):
bytes = txt.encode(sys.stdout.encoding, errors='replace')
# Decode back to string and print... we should hit no exception here
# since all errors have been replaced:
print(codecs.getdecoder(sys.stdout.encoding)(bytes)[0])
print()
def run(command, logFile): def run(command, logFile):
if cygwin: command = cygwinifyPaths(command) if cygwin: command = cygwinifyPaths(command)
if os.system('%s > %s 2>&1' % (command, logFile)): if os.system('%s > %s 2>&1' % (command, logFile)):
logPath = os.path.abspath(logFile) logPath = os.path.abspath(logFile)
print('\ncommand "%s" failed:' % command) print('\ncommand "%s" failed:' % command)
printFileContents(logFile)
# Assume log file was written in system's default encoding, but
# even if we are wrong, we replace errors ... the ASCII chars
# (which is what we mostly care about eg for the test seed) should
# still survive:
txt = codecs.open(logPath, 'r', encoding=sys.getdefaultencoding(), errors='replace').read()
# Encode to our output encoding (likely also system's default
# encoding):
bytes = txt.encode(sys.stdout.encoding, errors='replace')
# Decode back to string and print... we should hit no exception here
# since all errors have been replaced:
print(codecs.getdecoder(sys.stdout.encoding)(bytes)[0])
print()
raise RuntimeError('command "%s" failed; see log file %s' % (command, logPath)) raise RuntimeError('command "%s" failed; see log file %s' % (command, logPath))
def verifyDigests(artifact, urlString, tmpDir): def verifyDigests(artifact, urlString, tmpDir):
@ -762,19 +764,29 @@ def readSolrOutput(p, startupEvent, failureEvent, logFile):
f = open(logFile, 'wb') f = open(logFile, 'wb')
try: try:
while True: while True:
line = p.readline() line = p.stderr.readline()
if len(line) == 0: if len(line) == 0:
p.poll()
if not startupEvent.isSet():
failureEvent.set()
startupEvent.set()
break break
f.write(line) f.write(line)
f.flush() f.flush()
# print 'SOLR: %s' % line.strip() #print('SOLR: %s' % line.strip())
if not startupEvent.isSet() and line.find(b'Started SocketConnector@0.0.0.0:8983') != -1: if not startupEvent.isSet():
startupEvent.set() if line.find(b'Started SocketConnector@0.0.0.0:8983') != -1:
startupEvent.set()
elif p.poll() is not None:
failureEvent.set()
startupEvent.set()
break
except: except:
print() print()
print('Exception reading Solr output:') print('Exception reading Solr output:')
traceback.print_exc() traceback.print_exc()
failureEvent.set() failureEvent.set()
startupEvent.set()
finally: finally:
f.close() f.close()
@ -786,16 +798,24 @@ def testSolrExample(unpackPath, javaPath, isSrc):
env.update(os.environ) env.update(os.environ)
env['JAVA_HOME'] = javaPath env['JAVA_HOME'] = javaPath
env['PATH'] = '%s/bin:%s' % (javaPath, env['PATH']) env['PATH'] = '%s/bin:%s' % (javaPath, env['PATH'])
server = subprocess.Popen(['java', '-jar', 'start.jar'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) server = subprocess.Popen(['java', '-jar', 'start.jar'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, env=env)
startupEvent = threading.Event() startupEvent = threading.Event()
failureEvent = threading.Event() failureEvent = threading.Event()
serverThread = threading.Thread(target=readSolrOutput, args=(server.stderr, startupEvent, failureEvent, logFile)) serverThread = threading.Thread(target=readSolrOutput, args=(server, startupEvent, failureEvent, logFile))
serverThread.setDaemon(True) serverThread.setDaemon(True)
serverThread.start() serverThread.start()
# Make sure Solr finishes startup: # Make sure Solr finishes startup:
startupEvent.wait() if not startupEvent.wait(1800):
raise RuntimeError('startup took more than 30 minutes')
if failureEvent.isSet():
logFile = os.path.abspath(logFile)
print
print('Startup failed; see log %s' % logFile)
printFileContents(logFile)
raise RuntimeError('failure on startup; see log %s' % logFile)
print(' startup done') print(' startup done')
try: try: