adding multithread for http server, and manage some error

This commit is contained in:
Jean Sorgemoel 2018-08-03 17:28:00 +02:00
parent 3b901be756
commit 69cd23bf84
2 changed files with 43 additions and 30 deletions

View file

@ -142,6 +142,7 @@ import json
import fcntl
import os
import base64
from socketserver import ThreadingMixIn
try:
import bcrypt
@ -455,7 +456,7 @@ class ManageHttpRequest(http.server.SimpleHTTPRequestHandler):
self.end_headers()
class khaganatHTTPServer(http.server.HTTPServer):
class khaganatHTTPServer(ThreadingMixIn, http.server.HTTPServer):
"""
Class khaganatHTTPServer
Redefine HTTPServer (adding queue input & queue output, use by ManageHttpRequest)
@ -669,38 +670,50 @@ class ManageCommand():
if not self.process:
return "stopped"
else:
code = self.process.poll()
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
logging.debug("stop process %s", self.name)
self.process.send_signal(15)
time.sleep(1)
try:
code = self.process.poll()
loop -= 1
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
logging.debug("stop process %s", self.name)
self.process.send_signal(15)
time.sleep(1)
code = self.process.poll()
loop -= 1
except ProcessLookupError as e:
logging.warning("Stop process (%s)" % str(e))
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
logging.debug("terminate process %s", self.name)
self.process.terminate()
time.sleep(1)
code = self.process.poll()
loop -= 1
try:
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
logging.debug("terminate process %s", self.name)
self.process.terminate()
time.sleep(1)
code = self.process.poll()
loop -= 1
except ProcessLookupError as e:
logging.warning("Stop process (%s)" % str(e))
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
logging.debug("kill process %s", self.name)
self.process.send_signal(9)
time.sleep(1)
code = self.process.poll()
loop -= 1
try:
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
logging.debug("kill process %s", self.name)
self.process.send_signal(9)
time.sleep(1)
code = self.process.poll()
loop -= 1
except ProcessLookupError as e:
logging.warning("Stop process (%s)" % str(e))
code = self.process.wait()
self.process = None
if self.threadRead:
self.eventRunning.clear()
self.threadRead.join()
self.threadRead = None
logging.info("%s stopped (return code:%d)" % (self.name, code))
try:
code = self.process.wait()
self.process = None
if self.threadRead:
self.eventRunning.clear()
self.threadRead.join()
self.threadRead = None
logging.info("%s stopped (return code:%d)" % (self.name, code))
except ProcessLookupError as e:
logging.warning("Stop process (%s)" % str(e))
return "stopped"
def getlog(self, firstline):

View file

@ -339,7 +339,7 @@ class TestManager(unittest.TestCase):
self.assertEqual(res, 'stopped')
self.assertTrue(True)
except Exception as e:
self.fail('Error when run test', e)
self.fail('Error when run test (%s)' % str(e))
def test_execute_command_crashed(self):
try: