1- """Ephemeral-by-default artifact cache for the dataset viewer.
1+ """Ephemeral artifact cache for the dataset viewer.
22
3- The cache lives under a per-process temporary directory by default and is
4- removed at shutdown. Four cleanup layers cover all practical failure modes:
3+ The cache lives under a per-process temporary directory and is removed at
4+ shutdown. Four cleanup layers cover all practical failure modes:
55
661. ``atexit.register`` for normal Python exit.
772. Signal handlers for ``SIGINT`` / ``SIGTERM``.
8- 3. A FastAPI lifespan context (provided by callers ).
8+ 3. A context manager (``with CacheRoot() as cache:`` in the CLI ).
994. An orphan sweep at startup that removes directories left behind by
1010 previously-crashed processes.
1111"""
@@ -105,39 +105,28 @@ def sweep_orphans(temp_root: Path | None = None) -> list[Path]:
105105
106106
107107class CacheRoot :
108- """Context-manager-friendly artifact cache directory.
108+ """Context-manager-friendly ephemeral artifact cache directory.
109109
110- When ``persistent_dir`` is ``None`` (the default), a new ephemeral tempdir
111- named ``plaid-viewer-{pid}-{token}`` is created. The directory is
112- removed at process exit (``atexit``), on ``SIGINT`` / ``SIGTERM``, and
113- when the context manager is closed.
114-
115- When ``persistent_dir`` is provided, that directory is used as-is and is
116- **not** removed. Callers wanting persistence pass this.
110+ Creates a new tempdir named ``plaid-viewer-{pid}-{token}`` under the OS
111+ temp root. The directory is removed at process exit (``atexit``), on
112+ ``SIGINT`` / ``SIGTERM``, and when the context manager is closed.
117113 """
118114
119115 def __init__ (
120116 self ,
121- persistent_dir : Path | None = None ,
122117 * ,
123118 install_signal_handlers : bool = True ,
124119 run_orphan_sweep : bool = True ,
125120 ) -> None :
126- self ._ephemeral = persistent_dir is None
127- if self ._ephemeral :
128- if run_orphan_sweep :
129- sweep_orphans ()
130- token = uuid .uuid4 ().hex [:12 ]
131- base = Path (tempfile .gettempdir ())
132- self ._path = base / f"{ _EPHEMERAL_PREFIX } { os .getpid ()} -{ token } "
133- self ._path .mkdir (parents = True , exist_ok = False )
134- atexit .register (self ._safe_cleanup )
135- if install_signal_handlers :
136- self ._install_signal_handlers ()
137- else :
138- assert persistent_dir is not None
139- self ._path = Path (persistent_dir )
140- self ._path .mkdir (parents = True , exist_ok = True )
121+ if run_orphan_sweep :
122+ sweep_orphans ()
123+ token = uuid .uuid4 ().hex [:12 ]
124+ base = Path (tempfile .gettempdir ())
125+ self ._path = base / f"{ _EPHEMERAL_PREFIX } { os .getpid ()} -{ token } "
126+ self ._path .mkdir (parents = True , exist_ok = False )
127+ atexit .register (self ._safe_cleanup )
128+ if install_signal_handlers :
129+ self ._install_signal_handlers ()
141130 self ._closed = False
142131
143132 # ------------------------------------------------------------------ API
@@ -147,18 +136,12 @@ def path(self) -> Path:
147136 """Root directory of the cache."""
148137 return self ._path
149138
150- @property
151- def is_ephemeral (self ) -> bool :
152- """Whether the cache directory is automatically cleaned up."""
153- return self ._ephemeral
154-
155139 def close (self ) -> None :
156- """Remove the cache directory if it is ephemeral ."""
140+ """Remove the cache directory."""
157141 if self ._closed :
158142 return
159143 self ._closed = True
160- if self ._ephemeral :
161- self ._safe_cleanup ()
144+ self ._safe_cleanup ()
162145
163146 def __enter__ (self ) -> "CacheRoot" : # noqa: D105
164147 return self
0 commit comments