added running pre task same time as category tasks

This commit is contained in:
TPV committed 2026-07-14 07:35:44 -07:00
1 parent 00d3c12ee0
commit ae8b204a2c
3 files changed
+25 -18

No files matched your search

+5 -1
View File
@@ -217,7 +217,11 @@ def resolve_price_before_drop(
previous_price on a drop, otherwise None. previous_price on a drop, otherwise None.
""" """
if previous_price is not None and new_price is not None and new_price < previous_price: if (
previous_price is not None
and new_price is not None
and new_price < previous_price
):
return previous_price return previous_price
return None return None
+15 -17
View File
@@ -191,15 +191,15 @@ async def execute_monitor_logic(
proxies: AioProxies, proxies: AioProxies,
headers: dict | None = None, headers: dict | None = None,
cookies: dict | None = None, cookies: dict | None = None,
pre_task_func: Callable[..., Awaitable[None]] | None = None, concurrent_pre_task: Callable[..., Awaitable[None]] | None = None,
use_tls: bool = False, use_tls: bool = False,
**kwargs, **kwargs,
): ):
"""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, optionally runs pre_task_func, Each cycle opens a TLS or base session, dispatches task_func per category
dispatches task_func per category concurrently, logs expected failures, concurrently, logs expected failures, raises unexpected ones, then sleeps
raises unexpected ones, then sleeps briefly before the next cycle. 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
@@ -210,10 +210,13 @@ async def execute_monitor_logic(
proxies: Proxies applied to the session. proxies: Proxies applied to the session.
headers: Headers applied to the session. headers: Headers applied to the session.
cookies: Cookies applied to the base session; ignored for TLS. cookies: Cookies applied to the base session; ignored for TLS.
pre_task_func: Optional awaitable run once per cycle before category concurrent_pre_task: Optional awaitable run once per cycle, gathered
tasks, receiving session and **kwargs. alongside the category tasks and receiving session and **kwargs.
Category tasks must not depend on its side effects (session
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 pre_task_func and every task_func call. **kwargs: Forwarded verbatim to concurrent_pre_task and every
task_func call.
""" """
while True: while True:
@@ -227,15 +230,6 @@ async def execute_monitor_logic(
) )
async with session_ctx as session: async with session_ctx as session:
LOGGER.info("starting tasks") LOGGER.info("starting tasks")
if pre_task_func:
try:
await pre_task_func(session=session, **kwargs)
except EXPECTED_TASK_ERRORS as exc:
LOGGER.warning("failed to get products", exc_info=exc)
except Exception:
LOGGER.exception("unexpected error")
raise
if isinstance(category_identifiers, dict): if isinstance(category_identifiers, dict):
tasks = [ tasks = [
task_func(category_id, session, query_category_id=query, **kwargs) task_func(category_id, session, query_category_id=query, **kwargs)
@@ -247,7 +241,11 @@ async def execute_monitor_logic(
task_func(category_identifier, session, **kwargs) task_func(category_identifier, session, **kwargs)
for category_identifier in category_identifiers for category_identifier in category_identifiers
] ]
category_keys = category_identifiers category_keys = list(category_identifiers)
if concurrent_pre_task:
tasks.append(concurrent_pre_task(session=session, **kwargs))
category_keys.append("pre_task")
results = await asyncio.gather(*tasks, return_exceptions=True) results = await asyncio.gather(*tasks, return_exceptions=True)
for category_identifier, result in zip(category_keys, results): for category_identifier, result in zip(category_keys, results):
+5
View File
@@ -146,6 +146,7 @@ async def send_request(
params: dict | None = None, params: dict | None = None,
json_data: dict | None = None, json_data: dict | None = None,
data: dict | None = None, data: dict | None = None,
headers: dict | None = None,
reraise_json_error: bool = False, reraise_json_error: bool = False,
development_proxy: str | None = None, development_proxy: str | None = None,
) -> dict | str: ) -> dict | str:
@@ -164,6 +165,8 @@ async def send_request(
params: Query parameters. params: Query parameters.
json_data: JSON body. json_data: JSON body.
data: Form body. data: Form body.
headers: Per-request headers merged over the session headers,
avoiding mutation of shared session state from concurrent tasks.
reraise_json_error: Raise JSONDecodeError instead of returning raw reraise_json_error: Raise JSONDecodeError instead of returning raw
text when the response is not valid JSON. text when the response is not valid JSON.
development_proxy: Local proxy URL that overrides the pool and development_proxy: Local proxy URL that overrides the pool and
@@ -193,6 +196,7 @@ async def send_request(
params=params, params=params,
json=json_data, json=json_data,
data=data, data=data,
headers=headers,
proxy=proxy, proxy=proxy,
ssl=verify, ssl=verify,
) as response: ) as response:
@@ -205,6 +209,7 @@ async def send_request(
params=params, params=params,
json=json_data, json=json_data,
data=data, data=data,
headers=headers,
proxy=proxy, proxy=proxy,
verify=verify, verify=verify,
) )