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,6 +670,7 @@ class ManageCommand():
if not self.process:
return "stopped"
else:
try:
code = self.process.poll()
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
@ -677,7 +679,10 @@ class ManageCommand():
time.sleep(1)
code = self.process.poll()
loop -= 1
except ProcessLookupError as e:
logging.warning("Stop process (%s)" % str(e))
try:
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
logging.debug("terminate process %s", self.name)
@ -685,7 +690,10 @@ class ManageCommand():
time.sleep(1)
code = self.process.poll()
loop -= 1
except ProcessLookupError as e:
logging.warning("Stop process (%s)" % str(e))
try:
loop = self.maxWaitEnd
while (code is None) and (loop > 0):
logging.debug("kill process %s", self.name)
@ -693,7 +701,10 @@ class ManageCommand():
time.sleep(1)
code = self.process.poll()
loop -= 1
except ProcessLookupError as e:
logging.warning("Stop process (%s)" % str(e))
try:
code = self.process.wait()
self.process = None
if self.threadRead:
@ -701,6 +712,8 @@ class ManageCommand():
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: