From 39e0800d510e6dc6b057175c2141e1b0691e25fd Mon Sep 17 00:00:00 2001 From: kert Date: Wed, 11 Nov 2026 14:33:58 -0500 Subject: [PATCH] fix(bib): a slow zotero container start no longer fails the sync docker start zotero (KasmVNC) can take over 30 s; the subprocess timeout raised after the sync had already written everything, so bib-refresh-iom exited 1. One helper now waits up to 3 min and reports rather than raises. --- src/cli/bib.py | 48 ++++++++++++-------------- tests/cli/test_bib_payment_comments.py | 15 ++++++++ 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/cli/bib.py b/src/cli/bib.py index 0bfd82b..3a0130d 100644 --- a/src/cli/bib.py +++ b/src/cli/bib.py @@ -11,6 +11,23 @@ import typer app = typer.Typer(no_args_is_help=True) +def _zotero_container(action: str) -> None: + """``docker stop|start zotero`` around a Zotero SQLite write. The + KasmVNC container can take well over 30 s to start; waiting longer + than 3 min is reported, not raised — the container keeps starting, + and the sync's work is already done.""" + import subprocess + import sys + + try: + subprocess.run( + ["docker", action, "zotero"], capture_output=True, check=False, timeout=180 + ) + except subprocess.TimeoutExpired: + verb = "still starting" if action == "start" else "still stopping" + print(f" zotero container {verb} after 180 s", file=sys.stderr) + + @app.command() def sync( dry_run: bool = typer.Option(False, "--dry-run", help="Report without applying."), @@ -930,7 +947,6 @@ def sync_zotero( running. By default we stop the 'zotero' compose service for the duration of the sync and start it again afterward. """ - import subprocess from bib import connect from bib.sync import dedupe_collection as run_dedupe @@ -941,17 +957,9 @@ def sync_zotero( items = store.list_items(tag=tag, item_type=item_type) zotero_db_path = str(path("db.zotero")) - def _docker(*args: str) -> None: - subprocess.run( - ["docker", *args], - capture_output=True, - check=False, - timeout=30, - ) - try: if hold_zotero: - _docker("stop", "zotero") + _zotero_container("stop") stats = push_to_zotero(items, store=store, zotero_db=zotero_db_path) if dedupe_collection: stats |= run_dedupe(dedupe_collection, store=store) @@ -974,7 +982,7 @@ def sync_zotero( } finally: if hold_zotero: - _docker("start", "zotero") + _zotero_container("start") for k, v in stats.items(): typer.echo(f" {k}: {v}") @@ -1017,7 +1025,6 @@ def attach_oig( @app.command(name="refresh-oig") def refresh_oig() -> None: """One-shot: ingest-oig → attach-oig → sync-zotero (OIG tag).""" - import subprocess from bib import connect from bib.oig import download_attachments, ingest_all @@ -1036,14 +1043,10 @@ def refresh_oig() -> None: typer.echo("==> sync-zotero") items = store.list_items(tag="agency:oig") try: - subprocess.run( - ["docker", "stop", "zotero"], capture_output=True, check=False, timeout=30 - ) + _zotero_container("stop") stats = push_to_zotero(items, store=store) finally: - subprocess.run( - ["docker", "start", "zotero"], capture_output=True, check=False, timeout=30 - ) + _zotero_container("start") typer.echo( f" created={stats['created']} skipped={stats['skipped']} " f"attachments={stats['attachments']}" @@ -1070,7 +1073,6 @@ def refresh_iom( Zotero is held (stopped) during the sync phase only. """ - import subprocess from bib import connect from bib.iom import check_future_updates, download_attachments, ingest_all @@ -1104,14 +1106,10 @@ def refresh_iom( typer.echo("==> sync-zotero") items = store.list_items(item_type="manual") try: - subprocess.run( - ["docker", "stop", "zotero"], capture_output=True, check=False, timeout=30 - ) + _zotero_container("stop") stats = push_to_zotero(items, store=store) finally: - subprocess.run( - ["docker", "start", "zotero"], capture_output=True, check=False, timeout=30 - ) + _zotero_container("start") typer.echo( f" created={stats['created']} skipped={stats['skipped']} " f"attachments={stats['attachments']}" diff --git a/tests/cli/test_bib_payment_comments.py b/tests/cli/test_bib_payment_comments.py index 0330af2..786ecd6 100644 --- a/tests/cli/test_bib_payment_comments.py +++ b/tests/cli/test_bib_payment_comments.py @@ -207,3 +207,18 @@ def test_dmepos_resolve_omits_the_title_needle( assert needles == [("CMS-1691-P", ""), ("CMS-1780-P", "")] for call in api.resolve_docket.call_args_list: assert "title_contains" not in call.kwargs + + +def test_zotero_container_wait_timeout_is_reported_not_raised(monkeypatch, capsys): + # The KasmVNC zotero container can take >30 s to start; the sync's + # work was done but bib-refresh-iom exited 1 on TimeoutExpired. + import subprocess + + from cli.bib import _zotero_container + + def slow(cmd, **kw): + raise subprocess.TimeoutExpired(cmd, kw.get("timeout")) + + monkeypatch.setattr(subprocess, "run", slow) + _zotero_container("start") + assert "still starting" in capsys.readouterr().err