Can get log of current app under /apps/log
This commit is contained in:
parent
3f34d88bbc
commit
01ffe5b124
33
main.py
33
main.py
@ -84,6 +84,23 @@ class WatchDog(threading.Thread):
|
|||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
class LogReader(threading.Thread):
|
||||||
|
def __init__(self, runner):
|
||||||
|
super().__init__()
|
||||||
|
self.runner = runner
|
||||||
|
self.log = ""
|
||||||
|
def clear(self):
|
||||||
|
self.log = ""
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while running:
|
||||||
|
try:
|
||||||
|
self.log += runner.app.stderr.read(1).decode("utf-8")
|
||||||
|
except Exception as e:
|
||||||
|
time.sleep(0.1)
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AppRunner(threading.Thread):
|
class AppRunner(threading.Thread):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -97,6 +114,8 @@ class AppRunner(threading.Thread):
|
|||||||
self.last_update = time.time()
|
self.last_update = time.time()
|
||||||
self.watchdog = WatchDog(lambda: self.appTimedOut(), lambda: self.terminateApp())
|
self.watchdog = WatchDog(lambda: self.appTimedOut(), lambda: self.terminateApp())
|
||||||
self.watchdog.start()
|
self.watchdog.start()
|
||||||
|
self.logreader = LogReader(self)
|
||||||
|
self.logreader.start()
|
||||||
if config.UseGui:
|
if config.UseGui:
|
||||||
self.gui = Gui()
|
self.gui = Gui()
|
||||||
self.gui.start()
|
self.gui.start()
|
||||||
@ -110,8 +129,10 @@ class AppRunner(threading.Thread):
|
|||||||
if self.app != None:
|
if self.app != None:
|
||||||
# don't ask
|
# don't ask
|
||||||
self.app.terminate()
|
self.app.terminate()
|
||||||
|
if self.app != None:
|
||||||
self.app.kill()
|
self.app.kill()
|
||||||
self.app.stdout.close()
|
self.app.stdout.close()
|
||||||
|
self.app.stderr.close()
|
||||||
self.last_update = time.time()
|
self.last_update = time.time()
|
||||||
self.requestApp = 0
|
self.requestApp = 0
|
||||||
|
|
||||||
@ -121,8 +142,10 @@ class AppRunner(threading.Thread):
|
|||||||
def updateApp(self):
|
def updateApp(self):
|
||||||
if self.app != None:
|
if self.app != None:
|
||||||
self.app.terminate()
|
self.app.terminate()
|
||||||
|
self.app.stderr.close()
|
||||||
args = [os.getcwd()+"/"+config.Apps[self.requestedApp]["cmd"], str(config.ScreenX), str(config.ScreenY), self.param]
|
args = [os.getcwd()+"/"+config.Apps[self.requestedApp]["cmd"], str(config.ScreenX), str(config.ScreenY), self.param]
|
||||||
self.app = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
|
self.app = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
|
||||||
|
self.logreader.clear()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while running:
|
while running:
|
||||||
@ -145,8 +168,11 @@ class AppRunner(threading.Thread):
|
|||||||
pass
|
pass
|
||||||
self.watchdog.join()
|
self.watchdog.join()
|
||||||
self.serial.join()
|
self.serial.join()
|
||||||
|
logreader.join()
|
||||||
if config.UseGui:
|
if config.UseGui:
|
||||||
self.gui.join()
|
self.gui.join()
|
||||||
|
def getLog(self):
|
||||||
|
return self.logreader.log
|
||||||
|
|
||||||
runner = AppRunner()
|
runner = AppRunner()
|
||||||
runner.start()
|
runner.start()
|
||||||
@ -175,6 +201,9 @@ def apps_start(name, param):
|
|||||||
return "ok"
|
return "ok"
|
||||||
return "not_found"
|
return "not_found"
|
||||||
|
|
||||||
|
@route("/apps/log")
|
||||||
|
def apps_log():
|
||||||
|
return runner.getLog()
|
||||||
|
|
||||||
@route("/apps/running")
|
@route("/apps/running")
|
||||||
def apps_running():
|
def apps_running():
|
||||||
@ -183,5 +212,5 @@ def apps_running():
|
|||||||
|
|
||||||
|
|
||||||
run(host="localhost", port=8000)
|
run(host="localhost", port=8000)
|
||||||
running = False
|
running = Falselog
|
||||||
runner.join()
|
runner.join()
|
Loading…
Reference in New Issue
Block a user