1
0
Fork 0

xactfetch: Suppress asyncio InvalidStateError
dustin/xactfetch/pipeline/head This commit looks good Details

There is currently a [bug][0] in the Python Playwright API that causes
_asyncio_ to raise an `InvalidStateError` occasionally when the
`PlaywrightContextManager` exits.  This causes the program to exit
with a nonzero return code, even though it actually completed
successfully, which will cause the Job to be retried.  To avoid this,
we can catch and ignore the spurious exception.

I've reorganized the code a bit here because we have to wrap the whole
`with` block in the `try`/`except`; moving the contents of the block
into a function keeps the indentation level from getting out of control.

[0]: https://github.com/microsoft/playwright-python/issues/2238
Dustin 2024-07-11 21:16:40 -05:00
parent 3ff18d1042
commit 48708af68e
1 changed files with 36 additions and 30 deletions

View File

@ -13,7 +13,7 @@ from types import TracebackType
from typing import Any, Optional, Type from typing import Any, Optional, Type
import httpx import httpx
from playwright.async_api import Page from playwright.async_api import Playwright, Page
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
@ -702,10 +702,7 @@ class Chase:
return secret.decode() return secret.decode()
async def amain() -> None: async def fetch_transactions(pw: Playwright, secrets: SecretsClient) -> bool:
logging.basicConfig(level=logging.DEBUG)
secrets = SecretsClient()
await secrets.connect()
log.debug('Getting Firefly III access token') log.debug('Getting Firefly III access token')
token = (await secrets.get_secret('firefly.token')).decode() token = (await secrets.get_secret('firefly.token')).decode()
import_secret = ( import_secret = (
@ -720,7 +717,6 @@ async def amain() -> None:
) )
end_date = datetime.date.today() - datetime.timedelta(days=1) end_date = datetime.date.today() - datetime.timedelta(days=1)
failed = False failed = False
async with async_playwright() as pw, secrets:
browser = await pw.chromium.launch(headless=False) browser = await pw.chromium.launch(headless=False)
context = await browser.new_context() context = await browser.new_context()
await context.tracing.start(screenshots=True, snapshots=True) await context.tracing.start(screenshots=True, snapshots=True)
@ -732,9 +728,7 @@ async def amain() -> None:
): ):
failed = True failed = True
if 'chase' in banks: if 'chase' in banks:
if not await download_chase( if not await download_chase(page, secrets, end_date, token, importer):
page, secrets, end_date, token, importer
):
failed = True failed = True
if failed: if failed:
await context.tracing.stop(path='trace.zip') await context.tracing.stop(path='trace.zip')
@ -744,7 +738,19 @@ async def amain() -> None:
attach=f.read(), attach=f.read(),
filename='trace.zip', filename='trace.zip',
) )
return failed
async def amain() -> None:
logging.basicConfig(level=logging.DEBUG)
async with SecretsClient() as secrets:
try:
async with async_playwright() as pw:
failed = await fetch_transactions(pw, secrets)
raise SystemExit(1 if failed else 0) raise SystemExit(1 if failed else 0)
except asyncio.exceptions.InvalidStateError:
log.debug('Ignoring exception: %s', exc_info=sys.exc_info())
def main(): def main():