Move queue helper functions to staticmethods on their adapters.
PR Title Check / check-title (pull_request) Successful in 8s
Test Python Package / unit-tests (pull_request) Successful in 20s
Code Quality Pipeline / code-quality (pull_request) Failing after 32s
Test Python Package / integration-tests (pull_request) Successful in 1m33s
Test Python Package / coverage-report (pull_request) Successful in 16s
PR Title Check / check-title (pull_request) Successful in 8s
Test Python Package / unit-tests (pull_request) Successful in 20s
Code Quality Pipeline / code-quality (pull_request) Failing after 32s
Test Python Package / integration-tests (pull_request) Successful in 1m33s
Test Python Package / coverage-report (pull_request) Successful in 16s
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
co-authored by
Cursor
parent
1822d886b1
commit
a78320b434
@@ -13,21 +13,6 @@ from python_repositories.interfaces.queue_repository_interface import (
|
||||
)
|
||||
|
||||
|
||||
def _parse_age(value: object) -> datetime:
|
||||
"""Normalize an age field to a timezone-aware UTC datetime."""
|
||||
if isinstance(value, datetime):
|
||||
if value.tzinfo is None:
|
||||
return value.replace(tzinfo=UTC)
|
||||
return value.astimezone(UTC)
|
||||
if isinstance(value, str):
|
||||
normalized = value.replace("Z", "+00:00")
|
||||
parsed = datetime.fromisoformat(normalized)
|
||||
if parsed.tzinfo is None:
|
||||
return parsed.replace(tzinfo=UTC)
|
||||
return parsed.astimezone(UTC)
|
||||
raise ValueError(f"age value must be datetime or ISO-8601 str, got {type(value)!r}")
|
||||
|
||||
|
||||
class MemoryQueueAdapter(QueueRepositoryInterface):
|
||||
"""Thread-safe in-process FIFO queue with optional deduplication."""
|
||||
|
||||
@@ -43,6 +28,23 @@ class MemoryQueueAdapter(QueueRepositoryInterface):
|
||||
self._items: OrderedDict[Hashable, dict[str, Any]] = OrderedDict()
|
||||
self._seq = 0
|
||||
|
||||
@staticmethod
|
||||
def _parse_age(value: object) -> datetime:
|
||||
"""Normalize an age field to a timezone-aware UTC datetime."""
|
||||
if isinstance(value, datetime):
|
||||
if value.tzinfo is None:
|
||||
return value.replace(tzinfo=UTC)
|
||||
return value.astimezone(UTC)
|
||||
if isinstance(value, str):
|
||||
normalized = value.replace("Z", "+00:00")
|
||||
parsed = datetime.fromisoformat(normalized)
|
||||
if parsed.tzinfo is None:
|
||||
return parsed.replace(tzinfo=UTC)
|
||||
return parsed.astimezone(UTC)
|
||||
raise ValueError(
|
||||
f"age value must be datetime or ISO-8601 str, got {type(value)!r}"
|
||||
)
|
||||
|
||||
def enqueue(self, items: list[dict[str, Any]]) -> None:
|
||||
"""Append items; skip duplicates when ``dedup_keys`` is configured."""
|
||||
self.enqueue_and_return_added(items)
|
||||
@@ -84,13 +86,13 @@ class MemoryQueueAdapter(QueueRepositoryInterface):
|
||||
"""Remove items whose age field is strictly older than ``cutoff``."""
|
||||
if self._age_key is None:
|
||||
return 0
|
||||
cutoff_utc = _parse_age(cutoff)
|
||||
cutoff_utc = self._parse_age(cutoff)
|
||||
removed = 0
|
||||
with self._lock:
|
||||
to_remove = [
|
||||
key
|
||||
for key, item in self._items.items()
|
||||
if _parse_age(item[self._age_key]) < cutoff_utc
|
||||
if self._parse_age(item[self._age_key]) < cutoff_utc
|
||||
]
|
||||
for key in to_remove:
|
||||
del self._items[key]
|
||||
@@ -116,7 +118,7 @@ class MemoryQueueAdapter(QueueRepositoryInterface):
|
||||
if self._age_key is not None:
|
||||
if self._age_key not in item:
|
||||
raise ValueError(f"item missing age key: {self._age_key!r}")
|
||||
item[self._age_key] = _parse_age(item[self._age_key])
|
||||
item[self._age_key] = self._parse_age(item[self._age_key])
|
||||
return item
|
||||
|
||||
def _make_key(self, item: dict[str, Any]) -> Hashable:
|
||||
|
||||
Reference in New Issue
Block a user