fix: Enable debug logging in send_discord_message for better memory tracking

This commit is contained in:
Aaron 2025-11-15 10:03:15 -05:00
parent 79445bf879
commit 5a8d14eb4d
2 changed files with 45 additions and 58 deletions

View File

@ -30,12 +30,14 @@ def _escape_json_str(s: str) -> str:
def send_discord_message(message, username="Auto Garden Bot", is_alert=False, debug: bool = False): def send_discord_message(message, username="Auto Garden Bot", is_alert=False, debug: bool = False):
""" """
Send Discord message with aggressive GC and low-memory guard to avoid ENOMEM. Send Discord message with aggressive GC and low-memory guard to avoid ENOMEM.
When debug=True prints mem_free at important steps so you can see peak usage.
Returns True on success, False otherwise. Returns True on success, False otherwise.
""" """
global _NEXT_ALLOWED_SEND_TS global _NEXT_ALLOWED_SEND_TS
resp = None resp = None
url = _get_webhook_url(is_alert=is_alert) url = _get_webhook_url(is_alert=is_alert)
if not url: if not url:
if debug: print("DBG: no webhook URL configured")
return False return False
# Respect cooldown if we recently saw ENOMEM # Respect cooldown if we recently saw ENOMEM
@ -43,94 +45,82 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False, de
import time # type: ignore import time # type: ignore
now = time.time() now = time.time()
if _NEXT_ALLOWED_SEND_TS and now < _NEXT_ALLOWED_SEND_TS: if _NEXT_ALLOWED_SEND_TS and now < _NEXT_ALLOWED_SEND_TS:
if debug: print("DBG: backing off until", _NEXT_ALLOWED_SEND_TS)
return False return False
except: except:
pass pass
try: try:
# 1) Free heap before TLS # Lightweight local imports and GC
import gc # type: ignore import gc # type: ignore
gc.collect() import time # type: ignore
gc.collect() # run twice as a precaution
gc.collect(); gc.collect()
if debug: if debug:
try: try: print("DBG: mem after gc:", gc.mem_free() // 1024, "KB")
print("DBG: mem after gc:", gc.mem_free() // 1024, "KB") except: pass
except:
pass
# 1b) quick mem check - avoid importing urequests/TLS when too low # Quick mem check before importing urequests/SSL
try: mem = getattr(gc, "mem_free", lambda: None)()
mem = getattr(gc, "mem_free", lambda: None)() if debug:
if debug: try: print("DBG: mem before import check:", (mem or 0) // 1024, "KB")
try: except: pass
print("DBG: mem before import check:", (mem or 0) // 1024, "KB")
except:
pass
# lower threshold to match this board's free heap (~100 KB observed)
if mem is not None and mem < 90000:
if debug: print("DBG: skipping send (mem low)")
return False
except:
pass
# Conservative threshold — adjust as needed
if mem is not None and mem < 90000:
if debug: print("DBG: skip send (low mem)")
return False
# Import urequests only when we plan to send
try: try:
# 2) Import urequests locally (keeps RAM free when idle) if debug: print("DBG: importing urequests...")
import urequests as requests # type: ignore import urequests as requests # type: ignore
# import here to measure allocation impact
if debug:
print("DBG: importing urequests...")
except Exception as e: except Exception as e:
# import likely failed due to ENOMEM or missing module; back off # Back off when import fails (likely low-memory)
# do not spam full exception text to conserve heap and serial output
try: try:
import time # type: ignore
_NEXT_ALLOWED_SEND_TS = time.time() + 60 _NEXT_ALLOWED_SEND_TS = time.time() + 60
except: except:
pass pass
if debug: print("DBG: urequests import failed:", e)
print("Discord webhook import failed (backing off)") print("Discord webhook import failed (backing off)")
return False return False
gc.collect() # collect again after import to reduce fragmentation gc.collect()
if debug: if debug:
try: try: print("DBG: mem after import:", gc.mem_free() // 1024, "KB")
print("DBG: mem after import:", gc.mem_free() // 1024, "KB") except: pass
except:
pass
# 3) Keep payload tiny # Build tiny payload
url = str(url).strip().strip('\'"') url = str(url).strip().strip('\'"')
content = _escape_json_str(str(message)[:140]) # trim further content = _escape_json_str(str(message)[:140])
user = _escape_json_str(str(username)[:32]) user = _escape_json_str(str(username)[:32])
body_bytes = ('{"content":"%s","username":"%s"}' % (content, user)).encode("utf-8") body_bytes = ('{"content":"%s","username":"%s"}' % (content, user)).encode("utf-8")
# Minimal headers to reduce allocations
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
# 4) Send
if debug: if debug:
try: try: print("DBG: mem before post:", gc.mem_free() // 1024, "KB")
print("DBG: mem before post:", gc.mem_free() // 1024, "KB") except: pass
except:
pass
resp = requests.post(url, data=body_bytes, headers=headers) resp = requests.post(url, data=body_bytes, headers=headers)
if debug: if debug:
try: try: print("DBG: mem after post:", gc.mem_free() // 1024, "KB", "status:", getattr(resp, "status", None))
print("DBG: mem after post:", gc.mem_free() // 1024, "KB", "status:", getattr(resp, "status", None)) except: pass
except:
pass
status = getattr(resp, "status", getattr(resp, "status_code", None)) status = getattr(resp, "status", getattr(resp, "status_code", None))
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 longer to avoid repeated failures # On ENOMEM/MemoryError back off
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() + 60 _NEXT_ALLOWED_SEND_TS = time.time() + 60
except: except:
pass pass
# print concise message only if debug:
try: print("DBG: exception in send:", e)
except: pass
print("Discord webhook exception (backing off)") print("Discord webhook exception (backing off)")
return False return False
@ -140,18 +130,15 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False, de
resp.close() resp.close()
except: except:
pass pass
# Free refs and force GC
try: try:
# only delete names if they exist # remove large refs and force GC
if 'resp' in locals(): if 'resp' in locals(): del resp
del resp if 'body_bytes' in locals(): del body_bytes
if 'body_bytes' in locals(): if 'requests' in locals(): del requests
del body_bytes
if 'requests' in locals():
del requests
except: except:
pass pass
try: try:
gc.collect() import gc as _gc # type: ignore
_gc.collect()
except: except:
pass pass

View File

@ -411,7 +411,7 @@ while True:
mem_ok = getattr(_gc, 'mem_free', lambda: 0)() > 90000 mem_ok = getattr(_gc, 'mem_free', lambda: 0)() > 90000
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, debug=True)
if ok: if ok:
print("Discord startup notification sent") print("Discord startup notification sent")
discord_sent = True discord_sent = True