Skip to content

trading_agent.requests.request_expiry_watcher

Notices requests that lapsed without an answer, and tells their desks.

Permission requests lapse after [approvals] expiry_minutes, because an action approved much later could act on stale prices. The watcher runs beside the desks and checks every few seconds.

Typical usage example:

watcher = RequestExpiryWatcher(store, responder) await watcher.run()

RequestExpiryWatcher

RequestExpiryWatcher(store, responder, check_seconds=_CHECK_SECONDS)

Saves lapsed requests as expired and tells each desk once.

Creates the watcher.

Parameters:

Name Type Description Default
store UserRequestStore

Where requests are kept.

required
responder UserRequestResponder

Tells the desks.

required
check_seconds float

How long to wait between checks.

_CHECK_SECONDS
Source code in src/trading_agent/requests/request_expiry_watcher.py
def __init__(
    self,
    store: user_request_store_module.UserRequestStore,
    responder: user_request_responder_module.UserRequestResponder,
    check_seconds: float = _CHECK_SECONDS,
):
    """Creates the watcher.

    Args:
        store (user_request_store_module.UserRequestStore): Where requests are kept.
        responder (user_request_responder_module.UserRequestResponder): Tells the desks.
        check_seconds (float): How long to wait between checks.
    """
    self._store = store
    self._responder = responder
    self._check_seconds = check_seconds

check

check()

Expires every lapsed request now and tells its desk.

Returns:

Type Description
int

How many requests expired.

Source code in src/trading_agent/requests/request_expiry_watcher.py
def check(self) -> int:
    """Expires every lapsed request now and tells its desk.

    Returns:
        int: How many requests expired.
    """
    expired = self._store.expire_lapsed()
    for request in expired:
        self._responder.report_expired(request)
    return len(expired)

run async

run()

Checks every few seconds until cancelled.

Source code in src/trading_agent/requests/request_expiry_watcher.py
async def run(self) -> None:
    """Checks every few seconds until cancelled."""
    while True:
        try:
            self.check()
        except OSError as error:
            _LOGGER.warning('Could not check for lapsed requests: %s', error)
        await asyncio.sleep(self._check_seconds)