fix: Increase memory thresholds and backoff duration for Discord message sending
This commit is contained in:
parent
d2c0f68488
commit
c6f46e097b
@ -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
|
||||
try:
|
||||
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
|
||||
except:
|
||||
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)
|
||||
import urequests as requests # type: ignore
|
||||
except Exception as e:
|
||||
# if import fails due to ENOMEM or missing module, back off
|
||||
print("Discord webhook import failed:", e)
|
||||
# import likely failed due to ENOMEM or missing module; back off
|
||||
# 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
|
||||
|
||||
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)
|
||||
|
||||
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:
|
||||
if ("ENOMEM" in str(e)) or isinstance(e, MemoryError):
|
||||
import time # type: ignore
|
||||
_NEXT_ALLOWED_SEND_TS = time.time() + 30
|
||||
_NEXT_ALLOWED_SEND_TS = time.time() + 60
|
||||
except:
|
||||
pass
|
||||
print("Discord webhook exception:", e)
|
||||
# print concise message only
|
||||
print("Discord webhook exception (backing off)")
|
||||
return False
|
||||
|
||||
finally:
|
||||
|
||||
9
main.py
9
main.py
@ -404,8 +404,11 @@ while True:
|
||||
try:
|
||||
if not globals().get('discord_sent', True) and globals().get('pending_discord_message'):
|
||||
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)
|
||||
mem_ok = getattr(_gc, 'mem_free', lambda: 0)() > 90000
|
||||
mem_ok = getattr(_gc, 'mem_free', lambda: 0)() > 140000
|
||||
if mem_ok:
|
||||
try:
|
||||
ok = discord_webhook.send_discord_message(pending_discord_message)
|
||||
@ -417,8 +420,8 @@ while True:
|
||||
if discord_send_attempts >= 3:
|
||||
print("Discord startup notification failed after retries")
|
||||
discord_sent = True
|
||||
except Exception as e:
|
||||
print("Discord notification error: {}".format(e))
|
||||
except Exception:
|
||||
# swallow errors here; discord module already handles backoff
|
||||
discord_send_attempts += 1
|
||||
if discord_send_attempts >= 3:
|
||||
discord_sent = True
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user