| | |
| | | import sys |
| | | import socket |
| | | from threading import Thread, Event |
| | | import re |
| | | |
| | | def getTrisurfVersion(): |
| | | p = subprocess.Popen('trisurf --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| | | lines=p.stdout.readlines() |
| | | version=re.findall(r'[0-9a-f]{7}(?:-dirty)?', lines[0].decode('ascii')) |
| | | p.wait() |
| | | if(len(version)): |
| | | return version[0] |
| | | else: |
| | | return "unknown version" |
| | | |
| | | def get_hostname(): |
| | | return socket.gethostname() |
| | |
| | | return ((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0]) |
| | | |
| | | def get_client_id(addr, my_ip, my_hostname, subrun): |
| | | client_auth={'ip':my_ip,'hostname':my_hostname, 'subrun':subrun} |
| | | client_auth={'ip':my_ip,'hostname':my_hostname, 'subrun':subrun, 'trisurf_version':getTrisurfVersion() } |
| | | response=requests.post(addr+"/api/register/", data=client_auth) |
| | | if(response.status_code==200): |
| | | client_data=json.loads(response.text) |
| | |
| | | response=requests.post(addr+"/api/pingclient/", data=client_data) |
| | | if(response.status_code==200): |
| | | client_data=json.loads(response.text) |
| | | |
| | | return |
| | | |
| | | return client_data['concurrent_runs'] |
| | | else: |
| | | raise ValueError |
| | | |
| | |
| | | |
| | | |
| | | class ClientThread(Thread): |
| | | |
| | | def __init__(self,conn_address='http://beti.trisurf.eu',subid=0, update_seconds=100): |
| | | super(ClientThread,self).__init__() |
| | | self._stop_event = Event() |
| | |
| | | self.update_seconds=update_seconds |
| | | self.max_client_ping_time_elapsed=250 |
| | | |
| | | self.subruns=[] |
| | | |
| | | def stop(self): |
| | | self._stop_event.set() |
| | | |
| | |
| | | def join(self): |
| | | print('joining threads') |
| | | super(ClientThread, self).join() |
| | | for sub in self.subruns: |
| | | sub.stop() |
| | | sub.join() |
| | | if self.p is not None: |
| | | self.p.terminate() |
| | | if self.workingdir is not None: |
| | |
| | | return False |
| | | sleep(1) |
| | | return True |
| | | |
| | | def subrunsStartStop(self,nr): |
| | | while(self.id==0 and nr>len(self.subruns)+1): |
| | | #spawning a new worker: |
| | | print("[{}] Spawning a new worker".format(self.id)) |
| | | t=ClientThread(conn_address=self.conn_address, subid=len(self.subruns)+1,update_seconds=self.update_seconds) |
| | | t.start() |
| | | self.subruns.append(t) |
| | | while(self.id==0 and nr<len(self.subruns)+1): |
| | | print("[{}] Stopping a worker".format(self.id)) |
| | | self.subruns[-1].stop() |
| | | self.subruns[-1].join() |
| | | del self.subruns[-1] |
| | | |
| | | def run(self): |
| | | while(not self.isStopped()): #try to register |
| | |
| | | continue |
| | | #print("Got CID. getting RID.") |
| | | client_ping_time_elapsed=0 |
| | | concurrent_runs=client_ping(self.conn_address,cid) |
| | | self.subrunsStartStop(concurrent_runs) |
| | | while(not self.isStopped()): #successfully registered, now start pinging and searching for job |
| | | try: |
| | | (rid,tape,vtu,status)=get_run(self.conn_address,cid) |
| | |
| | | client_ping_time_elapsed+=10 |
| | | if(client_ping_time_elapsed>=self.max_client_ping_time_elapsed): |
| | | try: |
| | | client_ping(self.conn_address,cid) |
| | | concurrent_runs=client_ping(self.conn_address,cid) |
| | | self.subrunsStartStop(concurrent_runs) |
| | | except: |
| | | break |
| | | client_ping_time_elapsed=0 |
| | |
| | | self.workingdir=Directory('/tmp/ts_'+str(uuid.uuid4())) |
| | | self.workingdir.makeifnotexist() |
| | | self.workingdir.goto() |
| | | #print("[{}] Using directory {}".format(self.id, self.workingdir.fullpath())) |
| | | with open(self.workingdir.fullpath()+"/tape", 'w') as f: |
| | | f.write(tape) |
| | | if(int(status)==-1): |
| | |
| | | newVTU=getNewVTU(self.workingdir.fullpath()) |
| | | if newVTU: #upload |
| | | try: |
| | | for nv in sorted(newVTU): |
| | | for nvfile in sorted(newVTU): |
| | | nv=os.path.join(self.workingdir.fullpath(),nvfile) |
| | | with open(nv,'r') as f: |
| | | fc=f.read() |
| | | s=s+1 |
| | | print('[{}] Uploading {}.'.format(self.id,nv)) |
| | | print('[{}] Uploading {}.'.format(self.id,nvfile)) |
| | | upload(self.conn_address, cid, rid, fc, s) |
| | | os.unlink(nv) |
| | | except: |
| | | except Exception as e: |
| | | print("[{}] Could not upload".format(self.id)) |
| | | print(e) |
| | | self.p.terminate() |
| | | removeDir(self.workingdir.fullpath()) |
| | | self.p=None |
| | |
| | | self.sleep(self.update_seconds-1) |
| | | client_ping_time_elapsed+=self.update_seconds |
| | | if(client_ping_time_elapsed>self.max_client_ping_time_elapsed-self.update_seconds/2): |
| | | client_ping(self.conn_address,cid) |
| | | concurrent_runs=client_ping(self.conn_address,cid) |
| | | self.subrunsStartStop(concurrent_runs) |
| | | client_ping_time_elapsed=0 |
| | | |
| | | |