Skip to content

Commit 3926b91

Browse files
authored
Add events for upload start and end in cacholote (#119)
* Add events for upload start and end in cacholote * all in message * cleanup * fix docstring
1 parent 7fc27db commit 3926b91

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

cacholote/config.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616
from __future__ import annotations
1717

18+
import abc
1819
import datetime
1920
import logging
2021
import pathlib
@@ -41,6 +42,14 @@
4142
)
4243

4344

45+
class Context(abc.ABC):
46+
@abc.abstractmethod
47+
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
48+
49+
@abc.abstractmethod
50+
def upload_log(self, *args: Any, **kwargs: Any) -> None: ...
51+
52+
4453
class Settings(pydantic_settings.BaseSettings):
4554
use_cache: bool = True
4655
cache_db_urlpath: Optional[str] = _DEFAULT_CACHE_DB_URLPATH
@@ -61,6 +70,7 @@ class Settings(pydantic_settings.BaseSettings):
6170
_DEFAULT_LOGGER
6271
)
6372
lock_timeout: Optional[float] = None
73+
context: Optional[Context] = None
6474

6575
@pydantic.field_validator("create_engine_kwargs")
6676
def validate_create_engine_kwargs(
@@ -151,8 +161,10 @@ class set:
151161
Note that existing tags are overwritten.
152162
return_cache_entry: bool, default: False
153163
Whether to return the cache database entry rather than decoded results.
154-
lock_timeout: fload, optional, default: None
164+
lock_timeout: float, optional, default: None
155165
Time to wait before raising an error if a cache file is locked.
166+
context: Context, optional, default: None
167+
CADS context for internal use.
156168
"""
157169

158170
def __init__(self, **kwargs: Any):

cacholote/extra_encoders.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,26 @@ def _filesystem_is_local(fs: fsspec.AbstractFileSystem) -> bool:
111111
return isinstance(fs, fsspec.get_filesystem_class("file"))
112112

113113

114+
def _kwargs_to_str(**kwargs: Any) -> str:
115+
return " ".join([f"{k}={v}" for k, v in kwargs.items()])
116+
117+
114118
@contextlib.contextmanager
115119
def _logging_timer(event: str, **kwargs: Any) -> Generator[float, None, None]:
116120
logger = config.get().logger
121+
context = config.get().context
117122
logger.info(f"start {event}", **kwargs)
123+
if event == "upload" and context is not None:
124+
context.upload_log(f"start {event}. {_kwargs_to_str(**kwargs)}")
125+
118126
tic = time.perf_counter()
119127
yield tic
120128
toc = time.perf_counter()
129+
121130
kwargs["_".join(event.split() + ["time"])] = toc - tic # elapsed time
122131
logger.info(f"end {event}", **kwargs)
132+
if event == "upload" and context is not None:
133+
context.upload_log(f"end {event}. {_kwargs_to_str(**kwargs)}")
123134

124135

125136
class FileInfoModel(pydantic.BaseModel):

0 commit comments

Comments
 (0)