Skip to content

Commit 8bf425b

Browse files
committed
Add killServer() function
Allows us to kill the tika server and clean up some ram. Fixes #235
1 parent 646a490 commit 8bf425b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

tika/tika.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ def make_content_disposition_header(fn):
138138
import platform
139139
from subprocess import Popen
140140
from subprocess import STDOUT
141-
from os import walk
141+
from os import walk, kill
142+
import signal
142143
import logging
143144

144145
log_path = os.getenv('TIKA_LOG_PATH', tempfile.gettempdir())
@@ -186,6 +187,9 @@ def make_content_disposition_header(fn):
186187
EncodeUtf8 = 0
187188
csvOutput = 0
188189

190+
# Tika server PID, will be used later on to kill the process and free up ram
191+
TikaServerPID = -1
192+
189193
class TikaException(Exception):
190194
pass
191195

@@ -657,6 +661,7 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
657661
return False
658662

659663
# Run java with jar args
664+
# need to check if shell=true is really needed
660665
cmd = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True)
661666

662667
# Check logs and retry as configured
@@ -676,8 +681,15 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
676681
log.error("Tika startup log message not received after %d tries." % (TikaStartupMaxRetry))
677682
return False
678683
else:
684+
TikaServerPID = cmd.pid
679685
return True
680686

687+
def killServer():
688+
if(TikaServerPID > 1):
689+
kill(TikaServerPID, signal.SIGTERM)
690+
else:
691+
log.error("Invalid server PID, won't kill")
692+
681693
def toFilename(url):
682694
'''
683695
gets url and returns filename

0 commit comments

Comments
 (0)