Skip to content

Commit

Permalink
UPDATE script with timeouts and deleting old zip
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasKulhanek committed Nov 5, 2021
1 parent 40e6a5e commit 582cee6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
3 changes: 2 additions & 1 deletion run_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 38 additions & 10 deletions save_file.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)+' <br/>')
sys.stdout.flush()

Expand All @@ -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('<br/>')
Expand All @@ -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)):
Expand Down

0 comments on commit 582cee6

Please sign in to comment.