fix: remove stale _decimal_str ref, add context manager to HTTP clients
- UpstreamClient & Sub2ApiWebsiteClient: add __enter__/__exit__ - Convert all call sites to `with Client(...) as c:` pattern - Remove unused `upstream_name`/`upstream_base_url` locals in scheduler - Fix stale _decimal_str→decimal_string in _rate_from_group
This commit is contained in:
@@ -37,7 +37,6 @@ def _check_upstream(upstream_id: int) -> None:
|
||||
settings = get_settings()
|
||||
# ── Phase 1: upstream check + DB write ──────────────────────────
|
||||
db: Session = SessionLocal()
|
||||
client = None
|
||||
try:
|
||||
upstream = db.query(Upstream).filter(Upstream.id == upstream_id).first()
|
||||
if not upstream or not upstream.enabled:
|
||||
@@ -45,46 +44,45 @@ def _check_upstream(upstream_id: int) -> None:
|
||||
return
|
||||
|
||||
auth_config = json.loads(upstream.auth_config_json or "{}")
|
||||
client = UpstreamClient(
|
||||
was_unhealthy = upstream.last_status == "unhealthy"
|
||||
snapshot = None
|
||||
changes = None
|
||||
|
||||
with UpstreamClient(
|
||||
base_url=upstream.base_url,
|
||||
api_prefix=upstream.api_prefix,
|
||||
auth_type=upstream.auth_type,
|
||||
auth_config=auth_config,
|
||||
timeout=float(upstream.timeout_seconds),
|
||||
)
|
||||
) as client:
|
||||
try:
|
||||
client.login()
|
||||
groups = client.get_available_groups(upstream.groups_endpoint)
|
||||
raw_rates = client.get_group_rates(upstream.rate_endpoint)
|
||||
snapshot = build_snapshot(
|
||||
upstream.id, upstream.base_url, upstream.api_prefix, groups, raw_rates
|
||||
)
|
||||
except Exception as exc:
|
||||
# failure path
|
||||
upstream.consecutive_failures = (upstream.consecutive_failures or 0) + 1
|
||||
upstream.last_error = str(exc)
|
||||
upstream.last_checked_at = datetime.now(timezone.utc)
|
||||
threshold = settings.unhealthy_threshold
|
||||
became_unhealthy = (
|
||||
upstream.consecutive_failures >= threshold
|
||||
and upstream.last_status != "unhealthy"
|
||||
)
|
||||
if became_unhealthy:
|
||||
upstream.last_status = "unhealthy"
|
||||
db.commit()
|
||||
logger.warning("upstream %s check failed: %s", upstream.name, exc)
|
||||
# Phase 2: notify unhealthy in a fresh session
|
||||
if became_unhealthy:
|
||||
_notify_status(upstream.id, upstream.name, upstream.base_url,
|
||||
"upstream_unhealthy", str(exc))
|
||||
return
|
||||
|
||||
was_unhealthy = upstream.last_status == "unhealthy"
|
||||
snapshot = None
|
||||
changes = None
|
||||
|
||||
try:
|
||||
client.login()
|
||||
groups = client.get_available_groups(upstream.groups_endpoint)
|
||||
raw_rates = client.get_group_rates(upstream.rate_endpoint)
|
||||
snapshot = build_snapshot(
|
||||
upstream.id, upstream.base_url, upstream.api_prefix, groups, raw_rates
|
||||
)
|
||||
except Exception as exc:
|
||||
# failure path
|
||||
upstream.consecutive_failures = (upstream.consecutive_failures or 0) + 1
|
||||
upstream.last_error = str(exc)
|
||||
upstream.last_checked_at = datetime.now(timezone.utc)
|
||||
threshold = settings.unhealthy_threshold
|
||||
became_unhealthy = (
|
||||
upstream.consecutive_failures >= threshold
|
||||
and upstream.last_status != "unhealthy"
|
||||
)
|
||||
if became_unhealthy:
|
||||
upstream.last_status = "unhealthy"
|
||||
db.commit()
|
||||
logger.warning("upstream %s check failed: %s", upstream.name, exc)
|
||||
# Phase 2: notify unhealthy in a fresh session
|
||||
if became_unhealthy:
|
||||
_notify_status(upstream.id, upstream.name, upstream.base_url,
|
||||
"upstream_unhealthy", str(exc))
|
||||
return
|
||||
|
||||
# success path
|
||||
# success path (client auto-closed by `with`)
|
||||
prev_snapshot_row = (
|
||||
db.query(UpstreamRateSnapshot)
|
||||
.filter(UpstreamRateSnapshot.upstream_id == upstream_id)
|
||||
@@ -116,7 +114,6 @@ def _check_upstream(upstream_id: int) -> None:
|
||||
)
|
||||
|
||||
finally:
|
||||
client.close()
|
||||
db.close()
|
||||
|
||||
# ── Phase 2: notifications (independent sessions) ──────────────
|
||||
|
||||
Reference in New Issue
Block a user