fix: Increase memory thresholds for Discord message sending and adjust garbage collection logging
This commit is contained in:
parent
03b26b5339
commit
ac860207d9
@ -121,33 +121,23 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False, de
|
|||||||
import time # type: ignore
|
import time # type: ignore
|
||||||
|
|
||||||
gc.collect(); gc.collect()
|
gc.collect(); gc.collect()
|
||||||
if debug:
|
|
||||||
try: print("DBG: mem after gc:", gc.mem_free() // 1024, "KB")
|
|
||||||
except: pass
|
|
||||||
|
|
||||||
# Quick mem check before importing urequests/SSL
|
# Quick mem check before importing urequests/SSL
|
||||||
mem = getattr(gc, "mem_free", lambda: None)()
|
mem = getattr(gc, "mem_free", lambda: None)()
|
||||||
if debug:
|
# Require larger headroom based on device testing (adjust if you re-test)
|
||||||
try: print("DBG: mem before import check:", (mem or 0) // 1024, "KB")
|
if mem is not None and mem < 150000:
|
||||||
except: pass
|
# quietly skip send when memory is insufficient
|
||||||
|
|
||||||
# Conservative threshold — adjust as needed
|
|
||||||
if mem is not None and mem < 90000:
|
|
||||||
if debug: print("DBG: skip send (low mem)")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Import urequests only when we plan to send
|
# Import urequests only when we plan to send
|
||||||
try:
|
try:
|
||||||
if debug: print("DBG: importing urequests...")
|
|
||||||
import urequests as requests # type: ignore
|
import urequests as requests # type: ignore
|
||||||
except Exception as e:
|
except Exception:
|
||||||
# Back off when import fails (likely low-memory)
|
|
||||||
try:
|
try:
|
||||||
_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)
|
# quiet failure to avoid spamming serial; caller can check return value
|
||||||
print("Discord webhook import failed (backing off)")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
@ -162,16 +152,8 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False, de
|
|||||||
body_bytes = ('{"content":"%s","username":"%s"}' % (content, user)).encode("utf-8")
|
body_bytes = ('{"content":"%s","username":"%s"}' % (content, user)).encode("utf-8")
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
|
|
||||||
if debug:
|
|
||||||
try: print("DBG: mem before post:", gc.mem_free() // 1024, "KB")
|
|
||||||
except: pass
|
|
||||||
|
|
||||||
resp = requests.post(url, data=body_bytes, headers=headers)
|
resp = requests.post(url, data=body_bytes, headers=headers)
|
||||||
|
|
||||||
if debug:
|
|
||||||
try: print("DBG: mem after post:", gc.mem_free() // 1024, "KB", "status:", getattr(resp, "status", None))
|
|
||||||
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)
|
||||||
|
|
||||||
@ -183,10 +165,7 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False, de
|
|||||||
_NEXT_ALLOWED_SEND_TS = time.time() + 60
|
_NEXT_ALLOWED_SEND_TS = time.time() + 60
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
if debug:
|
# quiet exception path; return False for caller to handle/backoff
|
||||||
try: print("DBG: exception in send:", e)
|
|
||||||
except: pass
|
|
||||||
print("Discord webhook exception (backing off)")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
@ -203,7 +182,6 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False, de
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
import gc as _gc # type: ignore
|
gc.collect()
|
||||||
_gc.collect()
|
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
17
main.py
17
main.py
@ -407,11 +407,12 @@ while True:
|
|||||||
# run GC before measuring free memory
|
# run GC before measuring free memory
|
||||||
_gc.collect()
|
_gc.collect()
|
||||||
_gc.collect()
|
_gc.collect()
|
||||||
# require a conservative free memory threshold before TLS (adjust to your device)
|
# require larger headroom (observed successful test ~174 KB free)
|
||||||
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, debug=True)
|
# production send: don't enable verbose debug prints here
|
||||||
|
ok = discord_webhook.send_discord_message(pending_discord_message, debug=False)
|
||||||
if ok:
|
if ok:
|
||||||
print("Discord startup notification sent")
|
print("Discord startup notification sent")
|
||||||
discord_sent = True
|
discord_sent = True
|
||||||
@ -444,13 +445,13 @@ while True:
|
|||||||
print("Daily NTP re-sync failed (will retry tomorrow)")
|
print("Daily NTP re-sync failed (will retry tomorrow)")
|
||||||
# ===== END: PERIODIC RE-SYNC =====
|
# ===== END: PERIODIC RE-SYNC =====
|
||||||
|
|
||||||
# ===== ADD THIS: AGGRESSIVE GARBAGE COLLECTION =====
|
# Aggressive GC without frequent console noise
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
if int(current_time) % 5 == 0: # Every 5 seconds
|
if int(current_time) % 5 == 0:
|
||||||
gc.collect()
|
gc.collect()
|
||||||
# Optional: Print memory stats occasionally
|
# Print memory stats infrequently (every 10 minutes)
|
||||||
if int(current_time) % 60 == 0: # Every minute
|
if int(current_time) % 600 == 0:
|
||||||
print("💾 Memory free: {} KB".format(gc.mem_free() // 1024))
|
print("Memory free: {} KB".format(gc.mem_free() // 1024))
|
||||||
# ===== END: AGGRESSIVE GC =====
|
# ===== END: AGGRESSIVE GC =====
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user