22
33from __future__ import annotations
44
5+ import time
6+ import urllib .error
7+ import urllib .request
58import webbrowser
69from pathlib import Path
710from typing import Annotated
1619app = typer .Typer (help = "View logs and traffic UI" )
1720
1821
22+ _HEALTH_TIMEOUT = 30
23+ _HEALTH_INTERVAL = 0.5
24+
25+
26+ def _wait_for_datasette (port : int ) -> None :
27+ url = f"http://localhost:{ port } /-/healthz"
28+ deadline = time .monotonic () + _HEALTH_TIMEOUT
29+ while time .monotonic () < deadline :
30+ try :
31+ urllib .request .urlopen (url , timeout = 2 ) # noqa: S310
32+ return
33+ except urllib .error .HTTPError :
34+ return # server responded — healthy enough
35+ except (urllib .error .URLError , OSError ):
36+ time .sleep (_HEALTH_INTERVAL )
37+ warning ("Datasette did not become healthy in time — opening browser anyway" )
38+
39+
1940@app .command ("start" )
2041def logs_start (
2142 port : Annotated [int | None , typer .Option ("--port" , help = "Datasette host port" )] = None ,
@@ -39,17 +60,27 @@ def logs_start(
3960 error (str (exc ))
4061 raise typer .Exit (EXIT_DOCKER_NOT_RUNNING ) from exc
4162
63+ info ("Checking for datasette image updates…" )
64+ updated = manager .pull_if_newer (datasette_image )
65+ if updated :
66+ info ("New image available — restarting datasette" )
67+ existing = manager .find_datasette ()
68+ if existing :
69+ existing .remove (force = True )
70+
4271 info (f"Starting Datasette on http://localhost:{ datasette_port } " )
4372 manager .ensure_datasette (
4473 image = datasette_image ,
4574 logs_db_path = logs_db_path ,
4675 proxy_db_path = proxy_db_path ,
4776 port = datasette_port ,
4877 )
78+
79+ _wait_for_datasette (datasette_port )
4980 success ("Datasette is ready" )
5081
5182 if not no_open :
52- webbrowser .open (f"http://localhost:{ datasette_port } " )
83+ webbrowser .open (f"http://localhost:{ datasette_port } /-/dashboards " )
5384
5485
5586@app .command ("stop" )
0 commit comments