From ec673dd8b594a808cb9d9a14937b3dcd4a365dc5 Mon Sep 17 00:00:00 2001 From: Samo Penic <samo.penic@gmail.com> Date: Sat, 05 May 2018 20:08:23 +0000 Subject: [PATCH] Added pinging client, reconnection works. --- playground/tsclient.py | 61 +++++++++++++++++------------- 1 files changed, 34 insertions(+), 27 deletions(-) diff --git a/playground/tsclient.py b/playground/tsclient.py index 1c2abf4..83e3938 100755 --- a/playground/tsclient.py +++ b/playground/tsclient.py @@ -4,34 +4,12 @@ from time import sleep import uuid import subprocess -#from trisurf import trisurf import os import shutil import signal import sys import socket from threading import Thread, Event -""" -p=None -workingdir=None - - - -#--- SIGINT and SIGTERM HANDLING --- -def signal_handler(signal,frame): - global p - global wirkingdir - if p is not None: - p.terminate() - if(workingdir is not None): - removeDir(workingdir.fullpath()) - print("Process ended with signal " +str(signal)) - sys.exit(0) -signal.signal(signal.SIGINT, signal_handler) -signal.signal(signal.SIGTERM, signal_handler) -#--- END SIGINT and SIGTERM---- -""" - def get_hostname(): return socket.gethostname() @@ -61,7 +39,10 @@ return (rid,tape,vtu,status) else: print(response.text) - raise ValueError + if(response.status_code==400): + raise ValueError + else: + raise NameError def ping_run(addr,cid, rid): @@ -71,6 +52,16 @@ return else: raise ValueError + +def client_ping(addr,cid): + client_data={'client_id':cid} + response=requests.post(addr+"/api/pingclient/", data=client_data) + if(response.status_code==200): + return + else: + raise ValueError + + def send_error_report(addr,cid, rid,errcode): client_data={'client_id':cid, 'run_id':rid, 'error_code':errcode} @@ -121,6 +112,7 @@ self.hostname=get_hostname() self.update_seconds=update_seconds + def stop(self): self._stop_event.set() @@ -152,12 +144,28 @@ self.sleep(10) continue #print("Got CID. getting RID.") + client_ping_time_elapsed=0 while(not self.isStopped()): try: (rid,tape,vtu,status)=get_run(self.conn_address,cid) - except: + except NameError: print("[{}] Could not get RID.".format(self.id)) - self.sleep(10) + self.sleep(10) + client_ping_time_elapsed+=10 + if(client_ping_time_elapsed>=250): + try: + client_ping(self.conn_address,cid) + except: + break + client_ping_time_elapsed=0 + #if you put break instead of continue, there is no need to ping client. And it is more robust... + continue + except ValueError: + print("[{}] Wrong CID? Getting new CID.".format(self.id)) + #self.sleep(10) + break + except: + print("[{}] Cannot connect. Server down? Retrying....".format(self.id)) break else: #start separate thread with simulations. @@ -291,13 +299,12 @@ - #--- SIGINT and SIGTERM HANDLING --- def signal_handler(signal,frame): t.stop() t.join() print("Process ended with signal " +str(signal)) - sys.exit(0) + sys.exit(signal) #--- END SIGINT and SIGTERM---- if __name__ == '__main__': -- Gitblit v1.9.3