|
8 | 8 | FAILED_RETRY_INTERVAL_HOURS, |
9 | 9 | LIST_UPDATE_INTERVAL_HOURS, |
10 | 10 | MAX_CONCURRENT_ONBOARDINGS, |
| 11 | + MAX_INTEGRATION_RESULTS, |
11 | 12 | STUCK_ONBOARDING_LIST_TIMEOUT_HOURS, |
12 | 13 | STUCK_RECURRENT_LIST_TIMEOUT_HOURS, |
13 | 14 | ) |
14 | 15 |
|
15 | 16 | from .connection import get_db_connection |
16 | | -from .registry import execute, executemany, fetchrow |
| 17 | +from .registry import execute, executemany, fetchrow, fetchval |
17 | 18 |
|
18 | 19 | # Common SELECT columns joining mailinglist.lists + mailinglist.listProcessing |
19 | 20 | LIST_SELECT_COLUMNS = """ |
@@ -159,9 +160,31 @@ async def acquire_recurrent_list() -> MailingList | None: |
159 | 160 | ) |
160 | 161 |
|
161 | 162 |
|
| 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 | + |
162 | 177 | 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 | + |
165 | 188 | if not mailing_list: |
166 | 189 | mailing_list = await acquire_recurrent_list() |
167 | 190 | return mailing_list |
|
0 commit comments