fix: Increase memory thresholds and backoff duration for Discord message sending

This commit is contained in:
Aaron 2025-11-15 09:42:16 -05:00
parent d2c0f68488
commit c6f46e097b
2 changed files with 20 additions and 9 deletions

View File

@ -56,7 +56,8 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False):
# 1b) quick mem check - avoid importing urequests/TLS when too low # 1b) quick mem check - avoid importing urequests/TLS when too low
try: try:
mem = getattr(gc, "mem_free", lambda: None)() mem = getattr(gc, "mem_free", lambda: None)()
if mem is not None and mem < 90000: # conservative threshold (adjust to your board) # raised threshold to reduce chance of spike during TLS/requests
if mem is not None and mem < 140000:
return False return False
except: except:
pass pass
@ -65,8 +66,14 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False):
# 2) Import urequests locally (keeps RAM free when idle) # 2) Import urequests locally (keeps RAM free when idle)
import urequests as requests # type: ignore import urequests as requests # type: ignore
except Exception as e: except Exception as e:
# if import fails due to ENOMEM or missing module, back off # import likely failed due to ENOMEM or missing module; back off
print("Discord webhook import failed:", e) # do not spam full exception text to conserve heap and serial output
try:
import time # type: ignore
_NEXT_ALLOWED_SEND_TS = time.time() + 60
except:
pass
print("Discord webhook import failed (backing off)")
return False return False
gc.collect() # collect again after import to reduce fragmentation gc.collect() # collect again after import to reduce fragmentation
@ -87,14 +94,15 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False):
return bool(status and 200 <= status < 300) return bool(status and 200 <= status < 300)
except Exception as e: except Exception as e:
# On ENOMEM/MemoryError, back off for 30 seconds to avoid repeated failures # On ENOMEM/MemoryError, back off for longer to avoid repeated failures
try: try:
if ("ENOMEM" in str(e)) or isinstance(e, MemoryError): if ("ENOMEM" in str(e)) or isinstance(e, MemoryError):
import time # type: ignore import time # type: ignore
_NEXT_ALLOWED_SEND_TS = time.time() + 30 _NEXT_ALLOWED_SEND_TS = time.time() + 60
except: except:
pass pass
print("Discord webhook exception:", e) # print concise message only
print("Discord webhook exception (backing off)")
return False return False
finally: finally:

View File

@ -404,8 +404,11 @@ while True:
try: try:
if not globals().get('discord_sent', True) and globals().get('pending_discord_message'): if not globals().get('discord_sent', True) and globals().get('pending_discord_message'):
import gc as _gc # type: ignore import gc as _gc # type: ignore
# run GC before measuring free memory
_gc.collect()
_gc.collect()
# require a conservative free memory threshold before TLS (adjust to your device) # require a conservative free memory threshold before TLS (adjust to your device)
mem_ok = getattr(_gc, 'mem_free', lambda: 0)() > 90000 mem_ok = getattr(_gc, 'mem_free', lambda: 0)() > 140000
if mem_ok: if mem_ok:
try: try:
ok = discord_webhook.send_discord_message(pending_discord_message) ok = discord_webhook.send_discord_message(pending_discord_message)
@ -417,8 +420,8 @@ while True:
if discord_send_attempts >= 3: if discord_send_attempts >= 3:
print("Discord startup notification failed after retries") print("Discord startup notification failed after retries")
discord_sent = True discord_sent = True
except Exception as e: except Exception:
print("Discord notification error: {}".format(e)) # swallow errors here; discord module already handles backoff
discord_send_attempts += 1 discord_send_attempts += 1
if discord_send_attempts >= 3: if discord_send_attempts >= 3:
discord_sent = True discord_sent = True