small bug fix

This commit is contained in:
TPV committed 2026-07-16 14:19:33 -07:00
1 parent cdce46c033
commit 4aa0e43289
1 file changed
+43 -41
+43 -41
View File
@@ -200,9 +200,9 @@ async def execute_monitor_logic(
): ):
"""Run the monitor's category tasks in a loop with a fresh session per cycle. """Run the monitor's category tasks in a loop with a fresh session per cycle.
Each cycle opens a TLS or base session, dispatches task_func per category Each cycle opens a TLS or base session, awaits pre_task_func if given,
concurrently, logs expected failures, raises unexpected ones, then sleeps dispatches task_func per category concurrently, logs expected failures,
briefly before the next cycle. raises unexpected ones, then sleeps briefly before the next cycle.
Args: Args:
category_identifiers: List of category ids, or a mapping of category category_identifiers: List of category ids, or a mapping of category
@@ -223,8 +223,8 @@ async def execute_monitor_logic(
Category tasks must not depend on its side effects (session Category tasks must not depend on its side effects (session
headers, cookies, fetched state) since no ordering is guaranteed. headers, cookies, fetched state) since no ordering is guaranteed.
use_tls: Use the TLS fingerprint session instead of the base session. use_tls: Use the TLS fingerprint session instead of the base session.
**kwargs: Forwarded verbatim to concurrent_pre_task and every **kwargs: Forwarded verbatim to pre_task_func, concurrent_pre_task_func,
task_func call. and every task_func call.
""" """
while True: while True:
@@ -237,48 +237,50 @@ async def execute_monitor_logic(
else get_base_session(headers=headers, cookies=cookies, proxies=proxies) else get_base_session(headers=headers, cookies=cookies, proxies=proxies)
) )
async with session_ctx as session: async with session_ctx as session:
LOGGER.info("starting tasks") skip_cycle = False
if isinstance(category_identifiers, dict):
tasks = [
task_func(category_id, session, query_category_id=query, **kwargs)
for category_id, query in category_identifiers.items()
]
category_keys = list(category_identifiers.keys())
else:
tasks = [
task_func(category_identifier, session, **kwargs)
for category_identifier in category_identifiers
]
category_keys = list(category_identifiers)
if pre_task_func: if pre_task_func:
try: try:
await pre_task_func(session=session, **kwargs) await pre_task_func(session=session, **kwargs)
except EXPECTED_TASK_ERRORS as error: except EXPECTED_TASK_ERRORS as error:
LOGGER.warning("Failed to get products", exc_info=error) LOGGER.warning("pre task failed, skipping cycle", exc_info=error)
except Exception: skip_cycle = True
LOGGER.exception("Unexpected error")
raise
if concurrent_pre_task_func: if not skip_cycle:
tasks.append(concurrent_pre_task_func(session=session, **kwargs)) LOGGER.info("starting tasks")
category_keys.append("pre_task") if isinstance(category_identifiers, dict):
tasks = [
task_func(
category_id, session, query_category_id=query, **kwargs
)
for category_id, query in category_identifiers.items()
]
category_keys = list(category_identifiers.keys())
else:
tasks = [
task_func(category_identifier, session, **kwargs)
for category_identifier in category_identifiers
]
category_keys = list(category_identifiers)
results = await asyncio.gather(*tasks, return_exceptions=True) if concurrent_pre_task_func:
for category_identifier, result in zip(category_keys, results): tasks.append(concurrent_pre_task_func(session=session, **kwargs))
if isinstance(result, EXPECTED_TASK_ERRORS): category_keys.append("pre_task")
LOGGER.warning(
"failed to get products", results = await asyncio.gather(*tasks, return_exceptions=True)
extra={"category": category_identifier}, for category_identifier, result in zip(category_keys, results):
exc_info=result, if isinstance(result, EXPECTED_TASK_ERRORS):
) LOGGER.warning(
elif isinstance(result, Exception): "failed to get products",
LOGGER.error( extra={"category": category_identifier},
"unexpected error", exc_info=result,
extra={"category": category_identifier}, )
exc_info=result, elif isinstance(result, Exception):
) LOGGER.error(
raise result "unexpected error",
extra={"category": category_identifier},
exc_info=result,
)
raise result
LOGGER.info("sleeping before next tasks", extra={"delay": 0.30}) LOGGER.info("sleeping before next tasks", extra={"delay": 0.30})
await asyncio.sleep(0.30) await asyncio.sleep(0.30)