Samo Penic
2018-05-05 ec673dd8b594a808cb9d9a14937b3dcd4a365dc5
Added pinging client, reconnection works.
1 files modified
35 ■■■■■ changed files
playground/tsclient.py 35 ●●●●● patch | view | raw | blame | history
playground/tsclient.py
@@ -39,7 +39,10 @@
        return (rid,tape,vtu,status)
    else:
        print(response.text)
        if(response.status_code==400):
        raise ValueError
        else:
            raise NameError
def ping_run(addr,cid, rid):
@@ -49,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}
@@ -97,8 +110,8 @@
        self.id=subid
        self.ip=get_ip()
        self.hostname=get_hostname()
        self.update_seconds=update_second
#        self.subthreads=[]
        self.update_seconds=update_seconds
    def stop(self):
        self._stop_event.set()
@@ -131,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)    
                    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.