From 8d14e51d0e91cb8722396c2d85d8f16192d1cd0e Mon Sep 17 00:00:00 2001 From: Tim <53166177+TimMcCool@users.noreply.github.com> Date: Mon, 28 Aug 2023 00:08:37 +0200 Subject: [PATCH] Fixed cloud requests bugs, updating highly recommended - Fixed a bug that caused the server to sometimes not respond - Fixed a "bug" that caused the server to respond to old requests Signed-off-by: Tim <53166177+TimMcCool@users.noreply.github.com> --- scratchattach/_cloud.py | 15 +++++++++++--- scratchattach/_cloud_requests.py | 35 ++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/scratchattach/_cloud.py b/scratchattach/_cloud.py index 3a01e695..55486134 100644 --- a/scratchattach/_cloud.py +++ b/scratchattach/_cloud.py @@ -289,11 +289,13 @@ def __init__(self, project_id, connection, **entries): self.__dict__.update(entries) def _update(self): + if isinstance(self.connection, CloudConnection): + log_data = get_cloud(project_id = self.connection.project_id) + else: + log_data = {} while True: try: data = self.connection.websocket.recv().split('\n') - if len(data) > 1 and time.time() < self.connection._connect_timestamp + 0.5: - continue result = [] for i in data: try: @@ -302,11 +304,18 @@ def _update(self): pass for activity in result: if "on_"+activity["method"] in self._events: - self._events["on_"+activity["method"]](self.Event(user=None, var=activity["name"][2:], name=activity["name"][2:], value=activity["value"], timestamp=time.time()*10000)) + if log_data[activity["name"][2:]] == activity["value"]: + log_data.pop(activity["name"][2:]) + else: + self._events["on_"+activity["method"]](self.Event(user=None, var=activity["name"][2:], name=activity["name"][2:], value=activity["value"], timestamp=time.time()*10000)) except Exception: try: self.connection._connect(cloud_host=self.connection.cloud_host) self.connection._handshake() + if isinstance(self.connection, CloudConnection): + log_data = get_cloud(project_id = self.connection.project_id) + else: + log_data = {} except Exception: if "on_disconnect" in self._events: self._events["on_disconnect"]() diff --git a/scratchattach/_cloud_requests.py b/scratchattach/_cloud_requests.py index f875916d..e3a2697a 100644 --- a/scratchattach/_cloud_requests.py +++ b/scratchattach/_cloud_requests.py @@ -344,28 +344,35 @@ def on_set(event): old_clouddata = [] self.call_event("on_ready") #Calls the on_ready event + if data_from_websocket is False: + old_clouddata = _cloud.get_cloud_logs(self.project_id, filter_by_var_named="TO_HOST", limit=100) + try: + self.last_timestamp = old_clouddata[0]["timestamp"] + except Exception: + self.last_timestamp = 0 + while True: time.sleep(0.001) if data_from_websocket is False: clouddata = _cloud.get_cloud_logs( - self.project_id, filter_by_var_named="TO_HOST", limit=100)[::-1] + self.project_id, filter_by_var_named="TO_HOST", limit=100) if clouddata == old_clouddata: continue else: old_clouddata = list(clouddata) - self.ws_data = [ - _cloud.CloudEvents.Event(user=activity["user"], - var=activity["name"][2:], - name=activity["name"][2:], - value=activity["value"], - timestamp=activity["timestamp"]) - for activity in clouddata - ] - - if self.ws_data != []: - event = self.ws_data.pop() + self.ws_data = [] + for activity in clouddata: + if activity["timestamp"] > self.last_timestamp: + self.ws_data.insert(0,_cloud.CloudEvents.Event(user=activity["user"], + var=activity["name"][2:], + name=activity["name"][2:], + value=activity["value"], + timestamp=activity["timestamp"])) + + while self.ws_data != []: + event = self.ws_data.pop(0) try: raw_request, request_id = event.value.split(".") @@ -380,10 +387,8 @@ def on_set(event): continue else: self.responded_request_ids.insert(0, request_id) - self.responded_request_ids = self.responded_request_ids[: - 15] + self.responded_request_ids = self.responded_request_ids[:15] except Exception: - self.last_timestamp = event.timestamp continue self.last_requester = event.user