Handle network errors

This commit is contained in:
Fierelier 2020-11-26 03:55:42 +01:00
parent 1fd0c6966f
commit 6cad134cc7
1 changed files with 60 additions and 15 deletions

View File

@ -32,6 +32,7 @@ import logging
import configparser
import datetime
import json
import traceback
distro = "Discord but fast"
version = (0,1,0)
versionString = ".".join(map(str,version))
@ -692,12 +693,17 @@ def dtFromString(js):
def getMessageTimePath(channel):
return p(loginPath,"messageTimes",str(channel.id))
def getLastMessageTime(channel):
mtpath = getMessageTimePath(channel)
if not os.path.isfile(mtpath): return None
return dtFromString(open(mtpath,"r").read())
msgTimeLock = threading.Lock()
def getLastMessageTime(channel):
msgTimeLock.acquire()
mtpath = getMessageTimePath(channel)
if not os.path.isfile(mtpath): msgTimeLock.release(); return None
fh = open(mtpath,"r")
st = fh.read()
fh.close()
msgTimeLock.release()
return dtFromString(st)
def setLastMessageTime(channel,time):
msgTimeLock.acquire()
mtpath = getMessageTimePath(channel)
@ -719,8 +725,14 @@ def discordClient(token):
async def fetchLostMessages(channel):
newTime = None
async for msg in channel.history(limit=1):
newTime = msg.created_at
while True:
try:
async for msg in channel.history(limit=1):
newTime = msg.created_at
break
except Exception:
print(traceback.format_exc())
print("! could not fetch lost messages for " +str(channel.id)+ ", retrying...")
if not newTime: return False
oldTime = getLastMessageTime(channel)
@ -742,13 +754,24 @@ def discordClient(token):
guiLock.release()
if fetchMsgs == True:
async for message in channel.history(limit=100,before=newTime,after=oldTime):
guiLock.acquire()
newMsgs = None
while True:
try:
newMsgs = []
async for message in channel.history(limit=100,before=newTime,after=oldTime):
newMsgs.append(message)
break
except Exception:
print(traceback.format_exc())
print("! could not fetch lost messages for " +str(channel.id)+ ", retrying...")
guiLock.acquire()
for message in newMsgs:
for gui in openGuis:
if gui.type == "channel":
if channel != gui.channel: continue
addGuiTask(gui,gui.addMessage,(message,),{})
guiLock.release()
guiLock.release()
setLastMessageTime(channel,newTime)
return True
@ -828,9 +851,21 @@ def discordClient(token):
if oldTime:
if config["messageTracking"]["paranoidMessages"] == "true":
async for msg in message.channel.history(limit=100,before=message.created_at,after=oldTime):
print("[paranoidMessages] post-fetching message")
messages.append(msg)
newMsgs = None
while True:
try:
newMsgs = []
async for msg in message.channel.history(limit=100,before=message.created_at,after=oldTime):
newMsgs.append(msg)
break
except Exception:
print(traceback.format_exc())
print("! could not fetch lost messages for " +str(message.channel.id)+ ", retrying...")
if len(newMsgs) > 0:
print("paranoia found missed messages")
for msg in newMsgs:
messages.append(msg)
setLastMessageTime(message.channel,message.created_at)
@ -859,13 +894,23 @@ def discordClient(token):
guis.append(gui)
if gui.type == "channel":
while len(gui.pendingMessages) > 0:
await gui.channel.send(gui.pendingMessages.pop(0))
msg = gui.pendingMessages.pop(0)
try:
await gui.channel.send(msg)
except Exception:
print(traceback.format_exc())
print("! failed sending message: " +msg)
guiLock.release()
for gui in guis:
if gui.type == "channel":
if gui.ready == False:
channel = gui.channel
messages = await channel.history(limit=int(config["performance"]["historyFetch"])).flatten()
messages = []
try:
messages = await channel.history(limit=int(config["performance"]["historyFetch"])).flatten()
except Exception:
print(traceback.format_exc())
print("! failed fetching messages for channel " +str(channel.id))
messages = reversed(messages)
guiLock.acquire()
message = None