fix: Improve Discord message sending logic and memory management

This commit is contained in:
Aaron 2025-11-15 11:52:54 -05:00
parent b632a76d5a
commit 697f0bf31e
2 changed files with 38 additions and 34 deletions

View File

@ -131,7 +131,7 @@ def send_discord_message(message, username="Auto Garden Bot", is_alert=False, de
except:
pass
try:
import gc
import gc # type: ignore
gc.collect(); gc.collect()
except:
pass

38
main.py
View File

@ -144,6 +144,11 @@ def load_config():
return default_config
# global variables for Discord webhook status
discord_sent = False
discord_send_attempts = 0
pending_discord_message = None
# Load configuration from file
config = load_config()
import scripts.discord_webhook as discord_webhook
@ -200,16 +205,22 @@ if wifi and wifi.isconnected():
print(f"Web Interface: http://{ifconfig[0]}")
print("="*50 + "\n")
# Send startup notification to Discord (schedule for later to avoid ENOMEM)
try:
gc.collect() # free heap before HTTPS attempt
# don't attempt HTTP/TLS immediately — schedule for retry from main loop
# Try sending Discord webhook NOW, before creating other objects
gc.collect()
mem_ok = gc.mem_free() > 140000
if mem_ok:
ok = discord_webhook.send_discord_message("Pico W online at http://{}".format(ifconfig[0]), debug=False)
if ok:
print("Discord startup notification sent")
discord_sent = True
else:
print("Discord startup notification failed")
pending_discord_message = "Pico W online at http://{}".format(ifconfig[0])
discord_send_attempts = 0
discord_sent = False
print("Startup discord message queued (will send when memory available)")
except Exception as e:
print("Discord notification scheduling error: {}".format(e))
discord_send_attempts = 1
else:
print("Not enough memory for Discord startup notification")
pending_discord_message = "Pico W online at http://{}".format(ifconfig[0])
discord_send_attempts = 1
# Start web server early so page can load even if time sync is slow
web_server = TempWebServer(port=80)
@ -401,17 +412,13 @@ last_ntp_sync = time.time() # Track when we last synced
while True:
try:
# Try to send pending discord startup message when memory permits
try:
if not globals().get('discord_sent', True) and globals().get('pending_discord_message'):
if not discord_sent and pending_discord_message and discord_send_attempts < 3:
import gc as _gc # type: ignore
# run GC before measuring free memory
_gc.collect()
_gc.collect()
# require larger headroom (observed successful test ~174 KB free)
mem_ok = getattr(_gc, 'mem_free', lambda: 0)() > 140000
if mem_ok:
try:
# production send: don't enable verbose debug prints here
ok = discord_webhook.send_discord_message(pending_discord_message, debug=False)
if ok:
print("Discord startup notification sent")
@ -422,12 +429,9 @@ while True:
print("Discord startup notification failed after retries")
discord_sent = True
except Exception:
# swallow errors here; discord module already handles backoff
discord_send_attempts += 1
if discord_send_attempts >= 3:
discord_sent = True
except Exception:
pass
# Run all monitors (each checks if it's time to run via should_run())
run_monitors(monitors)