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):
self._status=MainExec.STATUS_STOPPING
ifself.ioThread:
self._tdeSubProcess.stop()
self.ioThread.join()
self.ioThread=None
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:
else:
print("Joining empty thread, doing nothing")
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"
TD_READY_MSG="TDengine is initialized successfully"
self.ioThread.daemon=True# thread dies with the program
self.ioThread.start()
self.shouldStop=False# don't let the main loop stop
defstop(self):
self.status=MainExec.STATUS_STARTING
ifnotself.subProcess:
print("Sub process already stopped")
# 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")
return
return
retCode=self.subProcess.poll()
retCode=self.subProcess.poll()
ifretCode:# valid return code, process ended
ifretCode:# valid return code, process ended
self.subProcess=None
self.subProcess=None
else:# process still alive, let's interrupt it
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
self.subProcess.send_signal(signal.SIGINT)# sub process should end, then IPC queue should end, causing IO thread to end
try:
try:
self.subProcess.wait(10)
self.subProcess.wait(10)
...
@@ -1826,41 +1907,20 @@ class SvcManager:
...
@@ -1826,41 +1907,20 @@ class SvcManager:
print("TDengine service process terminated successfully from SIG_INT")
print("TDengine service process terminated successfully from SIG_INT")
self.subProcess=None
self.subProcess=None
ifself.subProcessand(notself.subProcess.poll()):
print("Sub process is still running... pid = {}".format(self.subProcess.pid))