Skip to content

Commit 9eafca1

Browse files
committed
fix: gate mailing list onboarding on integration.results backlog (CM-1318)
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent 3a19d99 commit 9eafca1

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

services/apps/mailing_list_integration/src/crowdmail/database/crud.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
FAILED_RETRY_INTERVAL_HOURS,
99
LIST_UPDATE_INTERVAL_HOURS,
1010
MAX_CONCURRENT_ONBOARDINGS,
11+
MAX_INTEGRATION_RESULTS,
1112
STUCK_ONBOARDING_LIST_TIMEOUT_HOURS,
1213
STUCK_RECURRENT_LIST_TIMEOUT_HOURS,
1314
)
1415

1516
from .connection import get_db_connection
16-
from .registry import execute, executemany, fetchrow
17+
from .registry import execute, executemany, fetchrow, fetchval
1718

1819
# Common SELECT columns joining mailinglist.lists + mailinglist.listProcessing
1920
LIST_SELECT_COLUMNS = """
@@ -159,9 +160,31 @@ async def acquire_recurrent_list() -> MailingList | None:
159160
)
160161

161162

163+
async def can_onboard_more() -> bool:
164+
"""Check if system can handle more list onboarding based on activity load.
165+
166+
Returns False if integration.results count exceeds MAX_INTEGRATION_RESULTS
167+
or if the query fails (indicating high database load).
168+
"""
169+
try:
170+
integration_results_count = await fetchval("SELECT COUNT(*) FROM integration.results")
171+
return integration_results_count < MAX_INTEGRATION_RESULTS
172+
except Exception as e:
173+
logger.warning(f"Failed to get integration.results count with error: {e}")
174+
return False # if query failed mostly due to timeout then db is already under high load
175+
176+
162177
async def acquire_list_for_processing() -> MailingList | None:
163-
"""Acquire the next list to process: onboarding lists first, then due recurrent lists."""
164-
mailing_list = await acquire_onboarding_list()
178+
"""Acquire the next list to process: onboarding lists first (unless
179+
integration.results backlog is too high, e.g. an initial 100k+ message
180+
archive still draining), then due recurrent lists.
181+
"""
182+
mailing_list = None
183+
if await can_onboard_more():
184+
mailing_list = await acquire_onboarding_list()
185+
else:
186+
logger.info("Skipping onboarding due to high load on integration.results")
187+
165188
if not mailing_list:
166189
mailing_list = await acquire_recurrent_list()
167190
return mailing_list

services/apps/mailing_list_integration/src/crowdmail/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def load_env_var(key: str, required=True, default=None):
3030
LORE_MIRROR_DIR = load_env_var("LORE_MIRROR_DIR", default="/var/lore")
3131

3232
MAX_CONCURRENT_ONBOARDINGS = int(load_env_var("MAX_CONCURRENT_ONBOARDINGS", default="3"))
33+
# Delay starting new onboardings while integration.results backlog is this high,
34+
# same guard as git_integration's can_onboard_more().
35+
MAX_INTEGRATION_RESULTS = int(load_env_var("MAX_INTEGRATION_RESULTS", default="5000000"))
3336
LIST_UPDATE_INTERVAL_HOURS = int(load_env_var("LIST_UPDATE_INTERVAL_HOURS", default=24))
3437
FAILED_RETRY_INTERVAL_HOURS = int(load_env_var("FAILED_RETRY_INTERVAL_HOURS", default="6"))
3538

0 commit comments

Comments
 (0)