def runjob(self): param=self.param[0] couchinfo('job {0}'.format(set(param.keys()))) envparam=submitformkey.intersection(set(param.keys())) runjobparam=runjobkey.intersection(set(param.keys())) resourceparam=resources.intersection(set(param.keys())) #moduleparam=set(param['module']) #generate batch script for PBS standard #resources resl=['{0}={1}'.format(k,param[k]) for k in resourceparam] lbuf='#PBS -l ' while len(resl)>0: lbuf=lbuf+resl.pop()+':' lla=[''] lla.append(lbuf[:-1]) lla.append('#PBS -l walltime={0}'.format(param['walltime'])) #account lla.append('#PBS -A {0}'.format(param['account'])) #environment lla.append('#PBS -N {0}'.format(param['job_name'])) #variables #lla.append('#PBS -d {0}'.format(param['initialdir'])) #mail lla.append('#PBS -m {0}'.format(param['notification'])) lla.append('#PBS -M {0}'.format(param['mail'])) #stdio lla.append('#PBS -o {0}'.format(param['initialdir'])) lla.append('#PBS -e {0}'.format(param['initialdir'])) #other rja=['-{0} {1}'.format(k,param[k]) for k in runjobparam] buffer='#!/bin/bash\n' while len(lla)>0: buffer=buffer+lla.pop()+'\n' #buffer=buffer+'#@ queue\n' if param.has_key('modules'): for m in param['modules']: buffer=buffer+'module load '+m+'\n' #buffer=buffer+'module load args\n' # insert runjob for computation node execute #buffer=buffer+'nrniv ' buffer=buffer+param['engine']+' ' # insert dll for mech selection while len(rja)>0: buffer=buffer+rja.pop()+' ' buffer=buffer+param['exe']+' ' if param.has_key('args'): for arg in param['args']: buffer=buffer+' '+arg+' ' buffer=buffer+'\n' with open('web.job','w') as file: file.write(buffer) submit=self.subprocess.Popen(['qsub','web.job'],stdout=self.subprocess.PIPE) result=submit.communicate()[0] #send to couchdb jobid or error to display couchinfo('qsub response:{0}'.format(result)) ##res=requests.post('{0}/users/_design/jobs/_update/batch/{1}'.format(AGENTSERVER,user),data=@open('web.job','r')) res=requests.post('{0}/users/_design/jobs/_update/submit/{1}'.format(AGENTSERVER,user),data=result) return
def fullstat(self): #qselect=self.subprocess.Popen(['qselect','-xu','{0}'.format(me)],stdout=self.subprocess.PIPE) couchinfo('fullstat for '+self.param[0]) qstat=self.subprocess.Popen(['qstat','-x','-f',self.param[0]],stdout=self.subprocess.PIPE) result=qstat.communicate()[0] couchinfo('qstat response:{0}'.format(result)) res=requests.post('{0}/users/_design/jobs/_update/fullstat/{1}'.format(AGENTSERVER,user),data=result) return
def do(self,linein): couchinfo('do it') for p in self.param: linein+='{0} '.format(p) try: #maybe shell in crashed self.shell.stdin.writelines(unicode(linein+'\n')) except Exception as e: self.printexception("do",e) self.killme() self.updateresp=self.requests.post('{0}/commands/_design/schedule/_update/next/{1}'.format(AGENTSERVER,self.sessionid),data=None)
def route(self,recv,*args,**kwargs): try: couchinfo(recv) buffer=recv.content if (len(buffer)<2): return couchinfo('xmlrpc: {0} '.format(buffer)) self.param,self.method=self.xmlrpclib.loads(buffer) func=getattr(self,self.method,self.domethod) func() except Exception as e: self.printexception("route exception",e)
def runagent(): pid=os.getpid() couchinfo('start agent services') while True: requests.post('{0}/agents/_design/heartbeat/_update/agent/{1}'.format(AGENTSERVER,agentname),hooks=dict(response=createprocess)) qstat=subprocess.Popen(['qstat','-T','-x','-u','{0}'.format(user)],stdout=subprocess.PIPE) result=qstat.communicate()[0] res=requests.post('{0}/users/_design/jobs/_update/jobqueue/{1}'.format(AGENTSERVER,user),data=result) localdir=os.listdir(config.DEFAULTFOLDER) res=requests.get('{0}/folders/{1}'.format(AGENTSERVER,'webfolder')) obj=res.content.replace('true','True').replace('false','False') stats=eval(obj)['stats'] remotedir=[file for file in stats] diff=[file for file in localdir if file not in remotedir] for file in diff: if 'user.author' not in xattr.listxattr('{0}/{1}'.format(config.DEFAULTFOLDER,file)): with open ('{0}/{1}'.format(config.DEFAULTFOLDER,file),'a'): os.utime('{0}/{1}'.format(config.DEFAULTFOLDER,file),None) else: os.unlink('{0}/{1}'.format(config.DEFAULTFOLDER,file)) time.sleep(6)
def createprocess(resp,*arg,**kwarg): json=eval(resp.content) couchinfo(json) res=0 for key in json.get('sessions'): if not key in proc: proc[key]=Process(target=createthread,args=(key,)) proc[key].start() else: if proc[key].is_alive(): couchinfo("process {0} is alive".format(key)) else: couchinfo("process {0} is dead".format(key)) res+=proc[key].is_alive() if(res==0): couchinfo('no active sessions: suicide!') os.system("kill -9 {0}".format(os.getpid()))
def createthread(sessionid): ch,masterfd=pty.fork() if ch==0: couchinfo("create shell process") os.execl(SHELL,'Morg@{0}'.format(sessionid)) elif ch==-1: couchinfo("Error on fork shell child!") else: couchinfo("create thread for id {0}".format(sessionid)) winsz=fctnl.ioctl(masterfd,termios.TIOCGWINSZ,'00000000') th=th_popandexec(0,sessionid,masterfd) th.start() th.join()
def printexception(self,msg,e): couchinfo("popandexec exception:{0} {1}".format(msg,e))
def getagentvars(self,res,*arg,**kwargs): couchinfo(res)
def __dir__(self): res=[a for a in dir(self) if not a.startswith('__')] couchinfo(res)
def killme(self): self.pid=self.os.getpid() couchinfo('kill myself pid:{0}'.format(self.pid)) self.os.system("kill -9 {0}".format(self.pid))
def signal(self): couchinfo('send singnal to shell') return
def restart(self): couchinfo('execute restart from main thread') return
def domethod(self): couchinfo('domethod') line='{0} '.format(self.method) return self.do(line)
def execute(self): couchinfo('execute') line='' return self.do(line)