Spaces:
Running
Running
CassiopeiaCode
commited on
Commit
·
2e6c81a
1
Parent(s):
e2e9f4b
v2: auto-refresh when last_refresh_time is empty or invalid; always persist last_refresh_time/status on refresh result
Browse files
app.py
CHANGED
|
@@ -122,12 +122,23 @@ def _refresh_stale_tokens():
|
|
| 122 |
rows = conn.execute("SELECT id, last_refresh_time FROM accounts WHERE enabled=1").fetchall()
|
| 123 |
for row in rows:
|
| 124 |
acc_id, last_refresh = row[0], row[1]
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
try:
|
| 127 |
last_time = time.mktime(time.strptime(last_refresh, "%Y-%m-%dT%H:%M:%S"))
|
| 128 |
if now - last_time > 1500: # 25 minutes
|
| 129 |
-
|
| 130 |
except Exception:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
pass
|
| 132 |
except Exception:
|
| 133 |
pass
|
|
@@ -272,6 +283,20 @@ def refresh_access_token_in_db(account_id: str) -> Dict[str, Any]:
|
|
| 272 |
)
|
| 273 |
conn.commit()
|
| 274 |
raise HTTPException(status_code=502, detail=f"Token refresh failed: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
conn.execute(
|
| 277 |
"""
|
|
|
|
| 122 |
rows = conn.execute("SELECT id, last_refresh_time FROM accounts WHERE enabled=1").fetchall()
|
| 123 |
for row in rows:
|
| 124 |
acc_id, last_refresh = row[0], row[1]
|
| 125 |
+
should_refresh = False
|
| 126 |
+
if not last_refresh or last_refresh == "never":
|
| 127 |
+
should_refresh = True
|
| 128 |
+
else:
|
| 129 |
try:
|
| 130 |
last_time = time.mktime(time.strptime(last_refresh, "%Y-%m-%dT%H:%M:%S"))
|
| 131 |
if now - last_time > 1500: # 25 minutes
|
| 132 |
+
should_refresh = True
|
| 133 |
except Exception:
|
| 134 |
+
# Malformed or unparsable timestamp; force refresh
|
| 135 |
+
should_refresh = True
|
| 136 |
+
|
| 137 |
+
if should_refresh:
|
| 138 |
+
try:
|
| 139 |
+
refresh_access_token_in_db(acc_id)
|
| 140 |
+
except Exception:
|
| 141 |
+
# Ignore per-account refresh failure; timestamp/status are recorded inside
|
| 142 |
pass
|
| 143 |
except Exception:
|
| 144 |
pass
|
|
|
|
| 283 |
)
|
| 284 |
conn.commit()
|
| 285 |
raise HTTPException(status_code=502, detail=f"Token refresh failed: {str(e)}")
|
| 286 |
+
except Exception as e:
|
| 287 |
+
# Ensure last_refresh_time is recorded even on unexpected errors
|
| 288 |
+
now = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime())
|
| 289 |
+
status = "failed"
|
| 290 |
+
conn.execute(
|
| 291 |
+
"""
|
| 292 |
+
UPDATE accounts
|
| 293 |
+
SET last_refresh_time=?, last_refresh_status=?, updated_at=?
|
| 294 |
+
WHERE id=?
|
| 295 |
+
""",
|
| 296 |
+
(now, status, now, account_id),
|
| 297 |
+
)
|
| 298 |
+
conn.commit()
|
| 299 |
+
raise
|
| 300 |
|
| 301 |
conn.execute(
|
| 302 |
"""
|