Skip to content

Commit

Permalink
Fixed cloud requests bugs, updating highly recommended
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
TimMcCool authored Aug 27, 2023
1 parent aa48d1d commit 8d14e51
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
15 changes: 12 additions & 3 deletions scratchattach/_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"]()
Expand Down
35 changes: 20 additions & 15 deletions scratchattach/_cloud_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")
Expand All @@ -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
Expand Down

0 comments on commit 8d14e51

Please sign in to comment.