Commit 4aff4383 authored by Tomas Hlavacek's avatar Tomas Hlavacek

Async comands implemented.

parent 17afb492
...@@ -50,8 +50,7 @@ class Session(object): ...@@ -50,8 +50,7 @@ class Session(object):
self.routerid=routerid self.routerid=routerid
self.commandid=commandid self.commandid=commandid
self.parameters=parameters self.parameters=parameters
self.result=result self.error=error
self.error=None
self.finished=finished self.finished=finished
self.save() self.save()
...@@ -66,6 +65,13 @@ class Session(object): ...@@ -66,6 +65,13 @@ class Session(object):
else: else:
raise Exception('Invalid session id passed. Value was: '+sessionid) raise Exception('Invalid session id passed. Value was: '+sessionid)
@staticmethod
def getSessionOutputFileName(sessionid):
if(re.compile('^[a-zA-Z0-9]{10,128}$').match(sessionid)):
return defaults.session_dir+'/'+'ulg-'+sessionid+'.out.session'
else:
raise Exception('Invalid session id passed. Value was: '+sessionid)
@staticmethod @staticmethod
def load(sessionid): def load(sessionid):
if(sessionid == None): if(sessionid == None):
...@@ -132,11 +138,25 @@ class Session(object): ...@@ -132,11 +138,25 @@ class Session(object):
return self.parameters return self.parameters
def setResult(self,result): def setResult(self,result):
self.result = result fn = Session.getSessionOutputFileName(self.sessionid)
self.save()
f = open(fn, 'w')
f.write(result)
f.close()
def getResult(self): def getResult(self):
return self.result try:
fn = Session.getSessionOutputFileName(self.sessionid)
if(os.path.isfile(fn)):
f = open(fn, 'r')
result = f.read()
f.close()
return result
else:
return None
except:
return None
def getDecoratedResult(self,decorator_helper): def getDecoratedResult(self,decorator_helper):
if(self.getError()): if(self.getError()):
...@@ -148,8 +168,21 @@ class Session(object): ...@@ -148,8 +168,21 @@ class Session(object):
return None return None
def appendResult(self,result_fragment): def appendResult(self,result_fragment):
self.result = self.result + result_fragment fn = Session.getSessionOutputFileName(self.sessionid)
self.save()
f = open(fn, 'a')
f.write(result_fragment)
f.close()
def clearResult(self):
try:
fn = Session.getSessionOutputFileName(self.sessionid)
if(os.path.isfile(fn)):
f=open(fn,'w')
f.close()
except:
pass
def getRouter(self): def getRouter(self):
if(self.getRouterId()!=None): if(self.getRouterId()!=None):
...@@ -310,6 +343,13 @@ class ULGCgi: ...@@ -310,6 +343,13 @@ class ULGCgi:
</html>""" % url </html>""" % url
def runCommand(self,session): def runCommand(self,session):
class FakeSessionFile(object):
def __init__(self,session):
self.session = session
def write(self,string):
self.session.appendResult(string)
# try to increase usage counter # try to increase usage counter
if(self.increaseUsage()): if(self.increaseUsage()):
# start new thread if needed # start new thread if needed
...@@ -319,7 +359,7 @@ class ULGCgi: ...@@ -319,7 +359,7 @@ class ULGCgi:
def commandThreadBody(session,decreaseUsageMethod): def commandThreadBody(session,decreaseUsageMethod):
ulgmodel.debug("Running command: "+session.getCommand().getName()) ulgmodel.debug("Running command: "+session.getCommand().getName())
try: try:
session.setResult(session.getRouter().runSyncCommand(session.getCommand(),session.getParameters())) session.getRouter().runAsyncCommand(session.getCommand(),session.getParameters(),FakeSessionFile(session))
except Exception as e: except Exception as e:
ulgmodel.log("ERROR: Exception occured while running a command:" + traceback.format_exc()) ulgmodel.log("ERROR: Exception occured while running a command:" + traceback.format_exc())
session.setResult("ERROR in commandThreadBody:\n"+traceback.format_exc()) session.setResult("ERROR in commandThreadBody:\n"+traceback.format_exc())
...@@ -370,6 +410,7 @@ class ULGCgi: ...@@ -370,6 +410,7 @@ class ULGCgi:
# create and register session # create and register session
session = Session(sessionid=sessionid,routerid=routerid,commandid=commandid) session = Session(sessionid=sessionid,routerid=routerid,commandid=commandid)
session.clearResult()
# extract parameters # extract parameters
session.cleanParameters() session.cleanParameters()
...@@ -394,12 +435,12 @@ class ULGCgi: ...@@ -394,12 +435,12 @@ class ULGCgi:
if(session == None): if(session == None):
return self.HTTPRedirect(self.decorator_helper.getErrorURL()) return self.HTTPRedirect(self.decorator_helper.getErrorURL())
result_text = session.getDecoratedResult(self.decorator_helper)
if(session.isFinished()): if(session.isFinished()):
refresh=None refresh=None
else: else:
refresh = self.getRefreshInterval() refresh = self.getRefreshInterval(len(result_text))
result_text = session.getDecoratedResult(self.decorator_helper)
template = self.loader.load(defaults.index_template_file) template = self.loader.load(defaults.index_template_file)
return template.generate(defaults=defaults, return template.generate(defaults=defaults,
......
...@@ -296,7 +296,7 @@ class Router(object): ...@@ -296,7 +296,7 @@ class Router(object):
r = r + ': '+error+'</em>' if error else r+'.</em>' r = r + ': '+error+'</em>' if error else r+'.</em>'
return r return r
def runSyncCommand(self,command,parameters): def __prepareCommand(self,command,parameters):
c = command.getCommandText(parameters) c = command.getCommandText(parameters)
if(c == None): if(c == None):
...@@ -304,20 +304,13 @@ class Router(object): ...@@ -304,20 +304,13 @@ class Router(object):
return self.returnError(defaults.STRING_BAD_PARAMS) return self.returnError(defaults.STRING_BAD_PARAMS)
debug("Going to run command "+c+" on router "+self.getName()) debug("Going to run command "+c+" on router "+self.getName())
return c
r = '' def runSyncCommand(self,command,parameters):
if(defaults.debug): return self.runRawSyncCommand(self.__prepareCommand(command,parameters))
r = "DEBUG: Router.runCommand():\ncommand_name="+command.getName()+'\n'
if(parameters != None):
for pidx,p in enumerate(parameters):
r = r + " param"+str(pidx)+"="+str(p)+"\n"
r = r + "complete command="+c+"\n"
return r+self.runRawSyncCommand(c)
def runAsyncCommand(self,command,parameters,outfile): def runAsyncCommand(self,command,parameters,outfile):
# TODO return self.runRawCommand(self.__prepareCommand(command,parameters),outfile)
pass
def runRawSyncCommand(self,command): def runRawSyncCommand(self,command):
cr = StringIO.StringIO() cr = StringIO.StringIO()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment