From 582cee6a5c922e4fab63565cc09571bd63524649 Mon Sep 17 00:00:00 2001 From: Tomas Kulhanek Date: Fri, 5 Nov 2021 09:44:23 +0100 Subject: [PATCH] UPDATE script with timeouts and deleting old zip --- run_local.sh | 3 ++- save_file.py | 48 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/run_local.sh b/run_local.sh index 498b3e6..d0f3ca2 100755 --- a/run_local.sh +++ b/run_local.sh @@ -18,6 +18,7 @@ else export PATH="/home/vagrant/jupyter/bin:$PATH" fi conda activate -python3 --version +python3 --version +pip install psutil cd $COMPILER_HOME/compiler . ./worker.sh diff --git a/save_file.py b/save_file.py index a08ff8d..3da343f 100755 --- a/save_file.py +++ b/save_file.py @@ -1,11 +1,35 @@ #!/usr/bin/env python import sys import cgi, os, sys, time -import cgitb; cgitb.enable(display=0,logdir="/output/") +import cgitb; cgitb.enable(display=0,logdir="home/vagrant/Bodylight.js-FMU-Compiler/output/") form = cgi.FieldStorage() compilerdir = '/home/vagrant/Bodylight.js-FMU-Compiler/input/' outputdir = '/home/vagrant/Bodylight.js-FMU-Compiler/output/' + +def checkIfProcessRunning(processName): + ''' + Check if there is any running process that contains the given name processName. + ''' + print('checking process'+processName) + try: + import psutil + #Iterate over the all the running process + for proc in psutil.process_iter(): + try: + # Check if process name contains the given name string. + print(proc.name) + if processName in proc.name(): + print ('match'+processName) + return True + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): + print('some exception') + pass + except ImportError: + print('import error') + return True + return False + # Generator to buffer file chunks def fbuffer(f, chunk_size=10000): while True: @@ -16,11 +40,14 @@ def fbuffer(f, chunk_size=10000): def waitfor(filename,timeout=600): stop_check = False timer = 0 + step = 30 #waits until ZIP appeared or timeout is reached while not stop_check: - time.sleep(30) - timer+=30 + time.sleep(step) + timer+=step stop_check = os.path.exists(outputdir+filename) or (timer > timeout) + #TODO check process names whether it is still running + #or (not(checkIfProcessRunning('openmodelica.sh')) or not(checkIfProcessRunning('dymola.sh'))) print('... '+str(timer)+'
') sys.stdout.flush() @@ -38,14 +65,21 @@ def waitfor(filename,timeout=600): if fileitem.filename: # strip leading path from file name to avoid directory traversal attacks fn = os.path.basename(fileitem.filename) + fnname,fnext = os.path.splitext(fn) + fnamelog = fnname + '.log' + fnamezip = fnname + '.zip' + #remove zip file first + if os.path.exists(outputdir+fnamezip): + os.remove(outputdir+fnamezip) + #open file for writing from upload request f = open(compilerdir + fn, 'wb', 10000) - # Read the file in chunks for chunk in fbuffer(fileitem.file): f.write(chunk) f.close() message = 'The file "' + fn + '" was uploaded successfully' + print("Content-type: text/html\r\n\r\n") print(message) print('
') @@ -56,12 +90,6 @@ def waitfor(filename,timeout=600): sys.stdout.flush() print(sys.version) - - fnname,fnext = os.path.splitext(fn) - - fnamelog = fnname + '.log' - fnamezip = fnname + '.zip' - waitfor(fnamelog,30) #30 seconds for log to appear waitfor(fnamezip,1200) # 20 minutes wait for ZIP with JS if (os.path.exists(outputdir+fnamezip)):