Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/semble/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from collections import defaultdict
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from functools import cache
from importlib import import_module
from pathlib import Path
from types import ModuleType

from semble.cache import resolve_cache_folder
from semble.types import CallType, SearchResult
Expand Down Expand Up @@ -37,6 +40,15 @@ class SavingsSummary:
call_type_counts: dict[str, int]


@cache
def _import_fcntl() -> ModuleType | None:
"""Return fcntl when available, otherwise None."""
try:
return import_module("fcntl")
except ImportError: # pragma: no cover
return None


def save_search_stats(
results: list[SearchResult],
call_type: CallType,
Expand All @@ -59,6 +71,14 @@ def save_search_stats(
stats_file = _get_stats_file()
stats_file.parent.mkdir(parents=True, exist_ok=True)
with stats_file.open("a") as f:
fcntl = _import_fcntl()
try:
if fcntl is not None:
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError: # pragma: no cover
return # another process holds the lock; skip this record
except OSError: # pragma: no cover

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some way to distinguish between these failure cases? I.e., just not being supported or a lock contention? The former should not be retried.

return # lock contention or unsupported filesystem; skip
f.write(json.dumps(record) + "\n")
Comment thread
Pringled marked this conversation as resolved.
except OSError:
pass
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading