svc-infra 0.1.630__py3-none-any.whl → 0.1.631__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of svc-infra might be problematic. Click here for more details.
- svc_infra/billing/jobs.py +14 -2
- svc_infra/cache/__init__.py +4 -0
- svc_infra/cache/add.py +158 -0
- svc_infra/docs/cache.md +58 -0
- {svc_infra-0.1.630.dist-info → svc_infra-0.1.631.dist-info}/METADATA +1 -1
- {svc_infra-0.1.630.dist-info → svc_infra-0.1.631.dist-info}/RECORD +8 -7
- {svc_infra-0.1.630.dist-info → svc_infra-0.1.631.dist-info}/WHEEL +0 -0
- {svc_infra-0.1.630.dist-info → svc_infra-0.1.631.dist-info}/entry_points.txt +0 -0
svc_infra/billing/jobs.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import inspect
|
|
3
4
|
from datetime import datetime, timezone
|
|
4
5
|
from typing import Any, Awaitable, Callable, Dict, Optional
|
|
5
6
|
|
|
@@ -123,6 +124,17 @@ def make_billing_job_handler(
|
|
|
123
124
|
→ emits topic 'billing.invoice.created'
|
|
124
125
|
"""
|
|
125
126
|
|
|
127
|
+
async def _maybe_commit(session: Any) -> None:
|
|
128
|
+
"""Commit if the session exposes a commit method (await if coroutine).
|
|
129
|
+
|
|
130
|
+
This makes the handler resilient in tests/dev where a dummy session is used.
|
|
131
|
+
"""
|
|
132
|
+
commit = getattr(session, "commit", None)
|
|
133
|
+
if callable(commit):
|
|
134
|
+
result = commit()
|
|
135
|
+
if inspect.isawaitable(result):
|
|
136
|
+
await result
|
|
137
|
+
|
|
126
138
|
async def _handler(job: Job) -> None:
|
|
127
139
|
name = job.name
|
|
128
140
|
data: Dict[str, Any] = job.payload or {}
|
|
@@ -136,7 +148,7 @@ def make_billing_job_handler(
|
|
|
136
148
|
async with session_factory() as session:
|
|
137
149
|
svc = AsyncBillingService(session=session, tenant_id=tenant_id)
|
|
138
150
|
total = await svc.aggregate_daily(metric=metric, day_start=day_start)
|
|
139
|
-
await session
|
|
151
|
+
await _maybe_commit(session)
|
|
140
152
|
webhooks.publish(
|
|
141
153
|
"billing.usage_aggregated",
|
|
142
154
|
{
|
|
@@ -161,7 +173,7 @@ def make_billing_job_handler(
|
|
|
161
173
|
invoice_id = await svc.generate_monthly_invoice(
|
|
162
174
|
period_start=period_start, period_end=period_end, currency=currency
|
|
163
175
|
)
|
|
164
|
-
await session
|
|
176
|
+
await _maybe_commit(session)
|
|
165
177
|
webhooks.publish(
|
|
166
178
|
"billing.invoice.created",
|
|
167
179
|
{
|
svc_infra/cache/__init__.py
CHANGED
|
@@ -5,6 +5,8 @@ This module offers high-level decorators for read/write caching, cache invalidat
|
|
|
5
5
|
and resource-based cache management.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
|
+
from .add import add_cache
|
|
9
|
+
|
|
8
10
|
# Core decorators - main public API
|
|
9
11
|
from .decorators import cached # alias for cache_read
|
|
10
12
|
from .decorators import mutates # alias for cache_write
|
|
@@ -32,4 +34,6 @@ __all__ = [
|
|
|
32
34
|
# Resource-based caching
|
|
33
35
|
"resource",
|
|
34
36
|
"entity",
|
|
37
|
+
# Easy integration helper
|
|
38
|
+
"add_cache",
|
|
35
39
|
]
|
svc_infra/cache/add.py
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
Easy integration helper to wire the cache backend into an ASGI app lifecycle.
|
|
5
|
+
|
|
6
|
+
Contract:
|
|
7
|
+
- Idempotent: multiple calls are safe; startup/shutdown handlers are registered once.
|
|
8
|
+
- Env-driven defaults: respects CACHE_URL/REDIS_URL, CACHE_PREFIX, CACHE_VERSION, APP_ENV.
|
|
9
|
+
- Lifecycle: registers startup (init + readiness probe) and shutdown (graceful close).
|
|
10
|
+
- Ergonomics: exposes the underlying cache instance at app.state.cache by default.
|
|
11
|
+
|
|
12
|
+
This does not replace the per-function decorators (`cache_read`, `cache_write`) and
|
|
13
|
+
does not alter existing direct APIs; it simply standardizes initialization and wiring.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
import os
|
|
18
|
+
from typing import Any, Callable, Optional
|
|
19
|
+
|
|
20
|
+
from svc_infra.cache.backend import DEFAULT_READINESS_TIMEOUT
|
|
21
|
+
from svc_infra.cache.backend import instance as _instance
|
|
22
|
+
from svc_infra.cache.backend import setup_cache as _setup_cache
|
|
23
|
+
from svc_infra.cache.backend import shutdown_cache as _shutdown_cache
|
|
24
|
+
from svc_infra.cache.backend import wait_ready as _wait_ready
|
|
25
|
+
|
|
26
|
+
logger = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _derive_settings(
|
|
30
|
+
url: Optional[str], prefix: Optional[str], version: Optional[str]
|
|
31
|
+
) -> tuple[str, str, str]:
|
|
32
|
+
"""Derive cache settings from parameters or environment variables.
|
|
33
|
+
|
|
34
|
+
Precedence:
|
|
35
|
+
- explicit function arguments
|
|
36
|
+
- environment variables (CACHE_URL/REDIS_URL, CACHE_PREFIX, CACHE_VERSION)
|
|
37
|
+
- sensible defaults (mem://, "svc", "v1")
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
derived_url = url or os.getenv("CACHE_URL") or os.getenv("REDIS_URL") or "mem://"
|
|
41
|
+
derived_prefix = prefix or os.getenv("CACHE_PREFIX") or "svc"
|
|
42
|
+
derived_version = version or os.getenv("CACHE_VERSION") or "v1"
|
|
43
|
+
return derived_url, derived_prefix, derived_version
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def add_cache(
|
|
47
|
+
app: Any | None = None,
|
|
48
|
+
*,
|
|
49
|
+
url: str | None = None,
|
|
50
|
+
prefix: str | None = None,
|
|
51
|
+
version: str | None = None,
|
|
52
|
+
readiness_timeout: float | None = None,
|
|
53
|
+
expose_state: bool = True,
|
|
54
|
+
state_key: str = "cache",
|
|
55
|
+
) -> Callable[[], None]:
|
|
56
|
+
"""Wire cache initialization and lifecycle into the ASGI app.
|
|
57
|
+
|
|
58
|
+
If an app is provided, registers startup/shutdown handlers. Otherwise performs
|
|
59
|
+
immediate initialization (best-effort) without awaiting readiness.
|
|
60
|
+
|
|
61
|
+
Returns a no-op shutdown callable for API symmetry with other helpers.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
# Compute effective settings
|
|
65
|
+
eff_url, eff_prefix, eff_version = _derive_settings(url, prefix, version)
|
|
66
|
+
|
|
67
|
+
# If no app provided, do a simple init and return
|
|
68
|
+
if app is None:
|
|
69
|
+
try:
|
|
70
|
+
_setup_cache(url=eff_url, prefix=eff_prefix, version=eff_version)
|
|
71
|
+
logger.info(
|
|
72
|
+
"Cache initialized (no app wiring): backend=%s namespace=%s",
|
|
73
|
+
eff_url,
|
|
74
|
+
f"{eff_prefix}:{eff_version}",
|
|
75
|
+
)
|
|
76
|
+
except Exception:
|
|
77
|
+
logger.exception("Cache initialization failed (no app wiring)")
|
|
78
|
+
return lambda: None
|
|
79
|
+
|
|
80
|
+
# Idempotence: avoid duplicate wiring
|
|
81
|
+
try:
|
|
82
|
+
state = getattr(app, "state", None)
|
|
83
|
+
already = bool(getattr(state, "_svc_cache_wired", False))
|
|
84
|
+
except Exception:
|
|
85
|
+
state = None
|
|
86
|
+
already = False
|
|
87
|
+
|
|
88
|
+
if already:
|
|
89
|
+
logger.debug("add_cache: app already wired; skipping re-registration")
|
|
90
|
+
return lambda: None
|
|
91
|
+
|
|
92
|
+
# Define lifecycle handlers
|
|
93
|
+
async def _startup():
|
|
94
|
+
_setup_cache(url=eff_url, prefix=eff_prefix, version=eff_version)
|
|
95
|
+
try:
|
|
96
|
+
await _wait_ready(timeout=readiness_timeout or DEFAULT_READINESS_TIMEOUT)
|
|
97
|
+
except Exception:
|
|
98
|
+
# Bubble up to fail fast on startup; tests and prod prefer visibility
|
|
99
|
+
logger.exception("Cache readiness probe failed during startup")
|
|
100
|
+
raise
|
|
101
|
+
# Expose cache instance for convenience
|
|
102
|
+
if expose_state and hasattr(app, "state"):
|
|
103
|
+
try:
|
|
104
|
+
setattr(app.state, state_key, _instance())
|
|
105
|
+
except Exception:
|
|
106
|
+
logger.debug("Unable to expose cache instance on app.state", exc_info=True)
|
|
107
|
+
|
|
108
|
+
async def _shutdown():
|
|
109
|
+
try:
|
|
110
|
+
await _shutdown_cache()
|
|
111
|
+
except Exception:
|
|
112
|
+
# Best-effort; shutdown should not crash the app
|
|
113
|
+
logger.debug("Cache shutdown encountered errors (ignored)", exc_info=True)
|
|
114
|
+
|
|
115
|
+
# Register event handlers when supported
|
|
116
|
+
register_ok = False
|
|
117
|
+
try:
|
|
118
|
+
if hasattr(app, "add_event_handler"):
|
|
119
|
+
app.add_event_handler("startup", _startup)
|
|
120
|
+
app.add_event_handler("shutdown", _shutdown)
|
|
121
|
+
register_ok = True
|
|
122
|
+
except Exception:
|
|
123
|
+
register_ok = False
|
|
124
|
+
|
|
125
|
+
if not register_ok:
|
|
126
|
+
# Fallback: attempt FastAPI/Starlette .on_event decorators dynamically
|
|
127
|
+
try:
|
|
128
|
+
on_event = getattr(app, "on_event", None)
|
|
129
|
+
if callable(on_event):
|
|
130
|
+
on_event("startup")(_startup) # type: ignore[misc]
|
|
131
|
+
on_event("shutdown")(_shutdown) # type: ignore[misc]
|
|
132
|
+
register_ok = True
|
|
133
|
+
except Exception:
|
|
134
|
+
register_ok = False
|
|
135
|
+
|
|
136
|
+
# Mark wired and expose state immediately if desired
|
|
137
|
+
if hasattr(app, "state"):
|
|
138
|
+
try:
|
|
139
|
+
setattr(app.state, "_svc_cache_wired", True)
|
|
140
|
+
if expose_state and not hasattr(app.state, state_key):
|
|
141
|
+
setattr(app.state, state_key, _instance())
|
|
142
|
+
except Exception:
|
|
143
|
+
pass
|
|
144
|
+
|
|
145
|
+
if register_ok:
|
|
146
|
+
logger.info("Cache wired: url=%s namespace=%s", eff_url, f"{eff_prefix}:{eff_version}")
|
|
147
|
+
else:
|
|
148
|
+
# If we cannot register handlers, at least initialize now
|
|
149
|
+
try:
|
|
150
|
+
_setup_cache(url=eff_url, prefix=eff_prefix, version=eff_version)
|
|
151
|
+
except Exception:
|
|
152
|
+
logger.exception("Cache initialization failed (no event registration)")
|
|
153
|
+
|
|
154
|
+
# Return a simple shutdown handle for symmetry with other add_* helpers
|
|
155
|
+
return lambda: None
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
__all__ = ["add_cache"]
|
svc_infra/docs/cache.md
CHANGED
|
@@ -16,3 +16,61 @@ async def get_user(user_id: int):
|
|
|
16
16
|
|
|
17
17
|
- `CACHE_PREFIX`, `CACHE_VERSION` – change the namespace alias used by the decorators. 【F:src/svc_infra/cache/README.md†L20-L173】
|
|
18
18
|
- `CACHE_TTL_DEFAULT`, `CACHE_TTL_SHORT`, `CACHE_TTL_LONG` – override canonical TTL buckets. 【F:src/svc_infra/cache/ttl.py†L26-L55】
|
|
19
|
+
|
|
20
|
+
## Easy integration: add_cache
|
|
21
|
+
|
|
22
|
+
Use the one-liner helper to wire cache initialization into your ASGI app lifecycle with sensible defaults. This doesn’t replace the decorators; it standardizes init/readiness/shutdown and exposes a handle for convenience.
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from fastapi import FastAPI
|
|
26
|
+
from svc_infra.cache import add_cache, cache_read, cache_write, resource
|
|
27
|
+
|
|
28
|
+
app = FastAPI()
|
|
29
|
+
|
|
30
|
+
# Wires startup (init + readiness) and shutdown (graceful close). Idempotent.
|
|
31
|
+
add_cache(app)
|
|
32
|
+
|
|
33
|
+
user = resource("user", "user_id")
|
|
34
|
+
|
|
35
|
+
@user.cache_read(suffix="profile", ttl=300)
|
|
36
|
+
async def get_user_profile(user_id: int):
|
|
37
|
+
...
|
|
38
|
+
|
|
39
|
+
@user.cache_write()
|
|
40
|
+
async def update_user_profile(user_id: int, payload):
|
|
41
|
+
...
|
|
42
|
+
|
|
43
|
+
# Optional: direct cache instance for advanced scenarios
|
|
44
|
+
# available after startup when using add_cache(app)
|
|
45
|
+
# app.state.cache -> cashews cache instance
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Env-driven defaults
|
|
49
|
+
|
|
50
|
+
- URL: `CACHE_URL` → `REDIS_URL` → `mem://`
|
|
51
|
+
- Prefix: `CACHE_PREFIX` (default `svc`)
|
|
52
|
+
- Version: `CACHE_VERSION` (default `v1`)
|
|
53
|
+
|
|
54
|
+
You can override explicitly:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
add_cache(app, url="redis://localhost:6379/0", prefix="myapp", version="v2")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Behavior
|
|
61
|
+
|
|
62
|
+
- Idempotent: multiple calls won’t duplicate handlers.
|
|
63
|
+
- Startup/shutdown hooks: registered when supported by the app; startup performs a readiness probe. Startup is optional for correctness, but recommended for production reliability.
|
|
64
|
+
- app.state exposure: by default, exposes `app.state.cache` to access the underlying cashews instance.
|
|
65
|
+
|
|
66
|
+
### No-app usage
|
|
67
|
+
|
|
68
|
+
If you’re not wiring an app (e.g., a script), you can initialize without startup hooks:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from svc_infra.cache import add_cache
|
|
72
|
+
|
|
73
|
+
shutdown = add_cache(None) # immediate init (best-effort)
|
|
74
|
+
# ... do work ...
|
|
75
|
+
# call shutdown() is a no-op placeholder for symmetry
|
|
76
|
+
```
|
|
@@ -118,7 +118,7 @@ svc_infra/app/logging/formats.py,sha256=65eHUMWj7aD1RG7lCYIBSkFa_B748TutrZsta0OS
|
|
|
118
118
|
svc_infra/app/root.py,sha256=344EWBMJCduwzJ1BBo0yGAu15TkryuvOW4qBZ6Gk-8w,1635
|
|
119
119
|
svc_infra/billing/__init__.py,sha256=AdVxgBWibsz0xWk-Z91B7HecA-EhPMSRrXWIYPBgtMA,365
|
|
120
120
|
svc_infra/billing/async_service.py,sha256=afJR3vqkKFm3pIrYURR0E3xUmnRcxIx2TWfeDlLV2js,4680
|
|
121
|
-
svc_infra/billing/jobs.py,sha256=
|
|
121
|
+
svc_infra/billing/jobs.py,sha256=pS6f8pDIarR935rUzXJs6lWoPN5yElbiB_2R46BwX7U,8469
|
|
122
122
|
svc_infra/billing/models.py,sha256=bnCGPKfnK__6x0f0bwKYQsG2GwXjJFi3YRXnq5JYs7c,6083
|
|
123
123
|
svc_infra/billing/quotas.py,sha256=hreWT1ZI4f7uckAA19wlIC6JqgwBWJYrmaGsp0uqa1M,3469
|
|
124
124
|
svc_infra/billing/schemas.py,sha256=fjONpVnI4s4lwlcY8EBr6qHCA5GQ7vRVrxObkoECQJ8,898
|
|
@@ -127,7 +127,8 @@ svc_infra/bundled_docs/README.md,sha256=FqTieL4ADODxTnig8yehV2KdHX9bASDega52bjp5
|
|
|
127
127
|
svc_infra/bundled_docs/__init__.py,sha256=8_jF4fM-3Wf6j_mE4000_9AHcJ3tYZXO9hJY-pBEepM,63
|
|
128
128
|
svc_infra/bundled_docs/getting-started.md,sha256=JaMOgRUK_ajaX4SCtiE3GrhQ81wMwng6y46t0032ftU,210
|
|
129
129
|
svc_infra/cache/README.md,sha256=ZgIpmE0UVlGktp2nXUYv6FKJATCdkR_01v-GGxHN6Ao,10795
|
|
130
|
-
svc_infra/cache/__init__.py,sha256=
|
|
130
|
+
svc_infra/cache/__init__.py,sha256=5hlTZNdV3DA4SUecAmTy2waAG5eTuNWhc9XCdPT85M4,1031
|
|
131
|
+
svc_infra/cache/add.py,sha256=8JHkcXKj6CP8ioxb6RyR1gkM1hvHLbxjmpW-Kb1SO9k,5848
|
|
131
132
|
svc_infra/cache/backend.py,sha256=-dbZ2qkhebzbKosQqgvBNb01A-2_jGt6_0WmJhPoHy8,4418
|
|
132
133
|
svc_infra/cache/decorators.py,sha256=hsXTcdGo-Q1RKMrqQB0ROmFuce9A1JTt9sJAcWaHvMc,8330
|
|
133
134
|
svc_infra/cache/demo.py,sha256=MX8LK-4Ju1xAxZtBh-p_Weh8yPNWmmPnDkOW5wAiGKI,2507
|
|
@@ -238,7 +239,7 @@ svc_infra/docs/adr/0010-timeouts-and-resource-limits.md,sha256=tpOTjncKJAjTsDN8j
|
|
|
238
239
|
svc_infra/docs/api.md,sha256=AlPL9kBS6_dM0NrOteDQ9WqalSfKf_p9_zdy1CtGJdU,2384
|
|
239
240
|
svc_infra/docs/auth.md,sha256=PRl9G4UW78cT_7c4koVh5NDlheNAr02CpJT2YFbEXto,1333
|
|
240
241
|
svc_infra/docs/billing.md,sha256=MArKbKhzFwMLaOMABNDRtT_2D0zGgyFZ2r54o-99v68,7884
|
|
241
|
-
svc_infra/docs/cache.md,sha256=
|
|
242
|
+
svc_infra/docs/cache.md,sha256=mwObz4F_9KwGO2ftcYSvWfieYekJs1dva3UzaIHFI64,2454
|
|
242
243
|
svc_infra/docs/cli.md,sha256=w5og4SWrLyizlJAJiFgcWu2jDSc1Wj3NCYqzbbvg8VE,1702
|
|
243
244
|
svc_infra/docs/contributing.md,sha256=a0PhmzCLFw8S3odxFbI3p5_FOiPMZLrxk15Ujrk7ao4,1175
|
|
244
245
|
svc_infra/docs/data-lifecycle.md,sha256=_XFZCj9qiYgEiN9jO_lq7RcpVIeLVN7REaFziiNCnEI,2684
|
|
@@ -339,7 +340,7 @@ svc_infra/webhooks/fastapi.py,sha256=BCNvGNxukf6dC2a4i-6en-PrjBGV19YvCWOot5lXWsA
|
|
|
339
340
|
svc_infra/webhooks/router.py,sha256=6JvAVPMEth_xxHX-IsIOcyMgHX7g1H0OVxVXKLuMp9w,1596
|
|
340
341
|
svc_infra/webhooks/service.py,sha256=hh-rw0otc00vipZ998XaV5mHsk0IDGYqon0FnhaGr60,2229
|
|
341
342
|
svc_infra/webhooks/signing.py,sha256=NCwdZzmravUe7HVIK_uXK0qqf12FG-_MVsgPvOw6lsM,784
|
|
342
|
-
svc_infra-0.1.
|
|
343
|
-
svc_infra-0.1.
|
|
344
|
-
svc_infra-0.1.
|
|
345
|
-
svc_infra-0.1.
|
|
343
|
+
svc_infra-0.1.631.dist-info/METADATA,sha256=B-Klim62FKQqNr_a9lfHuJ3jsuWSf5HF032_SfiDDxM,8748
|
|
344
|
+
svc_infra-0.1.631.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
|
|
345
|
+
svc_infra-0.1.631.dist-info/entry_points.txt,sha256=6x_nZOsjvn6hRZsMgZLgTasaCSKCgAjsGhACe_CiP0U,48
|
|
346
|
+
svc_infra-0.1.631.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|