self._thread.daemon=True# thread dies with the program
self._thread.start()
# wait for service to start
foriinrange(0,10):
time.sleep(1.0)
# self.procIpcBatch() # don't pump message during start up
print("_zz_",end="",flush=True)
ifself._status==MainExec.STATUS_RUNNING:
logger.info("[] TDengine service READY to process requests")
return# now we've started
raiseRuntimeError("TDengine service did not start successfully")# TODO: handle this better?
defstop(self):
# can be called from both main thread or signal handler
print("Terminating TDengine service running as the sub process...")
ifself.isStopped():
print("Service already stopped")
return
ifself.isStopping():
print("Service is already being stopped")
return
# Linux will send Control-C generated SIGINT to the TDengine process already, ref: https://unix.stackexchange.com/questions/176235/fork-and-how-signals-are-delivered-to-processes
ifnotself._tdeSubProcess:
raiseRuntimeError("sub process object missing")
defjoinIoThread(self):
ifself.ioThread:
self.ioThread.join()
self.ioThread=None
self._status=MainExec.STATUS_STOPPING
self._tdeSubProcess.stop()
ifself._tdeSubProcess.isRunning():# still running
print("FAILED to stop sub process, it is still running... pid = {}".format(self.subProcess.pid))
else:
self._tdeSubProcess=None# not running any more
self.join()# stop the thread, change the status, etc.
defjoin(self):
# TODO: sanity check
ifnotself.isStopping():
raiseRuntimeError("Unexpected status when ending svc mgr thread: {}".format(self._status))
ifself._thread:
self._thread.join()
self._thread=None
self._status=MainExec.STATUS_STOPPED
else:
print("Joining empty thread, doing nothing")
def_trimQueue(self,targetSize):
iftargetSize<=0:
return# do nothing
q=self._ipcQueue
if(q.qsize()<=targetSize):# no need to trim
return
logger.debug("Triming IPC queue to target size: {}".format(targetSize))
itemsToTrim=q.qsize()-targetSize
foriinrange(0,itemsToTrim):
try:
q.get_nowait()
exceptEmpty:
break# break out of for loop, no more trimming
TD_READY_MSG="TDengine is initialized successfully"
self.ioThread.daemon=True# thread dies with the program
self.ioThread.start()
close_fds=ON_POSIX)# had text=True, which interferred with reading EOF
self.shouldStop=False# don't let the main loop stop
self.status=MainExec.STATUS_STARTING
# wait for service to start
foriinrange(0,10):
time.sleep(1.0)
self._procIpcBatch()# pump messages
print("_zz_",end="",flush=True)
ifself.status==MainExec.STATUS_RUNNING:
print("TDengine service READY to process requests")
return# now we've started
raiseRuntimeError("TDengine service did not start successfully")# TODO: handle this better?
defstopTaosService(self):
# can be called from both main thread or signal handler
print("Terminating TDengine service running as the sub process...")
# Linux will send Control-C generated SIGINT to the TDengine process already, ref: https://unix.stackexchange.com/questions/176235/fork-and-how-signals-are-delivered-to-processes
ifnotself.subProcess:
print("Process already stopped")
defstop(self):
ifnotself.subProcess:
print("Sub process already stopped")
return
retCode=self.subProcess.poll()
ifretCode:# valid return code, process ended
self.subProcess=None
else:# process still alive, let's interrupt it
print("Sub process still running, sending SIG_INT and waiting for it to stop...")
print("Sub process is running, sending SIG_INT and waiting for it to terminate...")
self.subProcess.send_signal(signal.SIGINT)# sub process should end, then IPC queue should end, causing IO thread to end
try:
self.subProcess.wait(10)
...
...
@@ -1826,41 +1907,20 @@ class SvcManager:
print("TDengine service process terminated successfully from SIG_INT")
self.subProcess=None
ifself.subProcessand(notself.subProcess.poll()):
print("Sub process is still running... pid = {}".format(self.subProcess.pid))