fix: 一次保存学习配置,改代理后重开任务

This commit is contained in:
2026-11-16 11:13:45 +08:00
parent 556121a265
commit 7381b49e4c
11 changed files with 214 additions and 99 deletions
+8 -69
View File
@@ -17,20 +17,16 @@ from admin.users import (
list_users,
read_credentials,
read_proxy,
read_course_workers,
read_study_start,
set_course_workers,
set_paused,
set_proxy,
set_study_action,
set_study_settings,
set_study_start,
)
from euniv.courses import course_page, request_course_sync_now
from euniv.db import get_db
from euniv.proxy_pool import ProxyProbeError
from euniv.proxy_pool import page as proxy_page
from euniv.proxy_pool import probe_ids as probe_proxy_ids
from euniv.proxy_pool import probe_state as proxy_probe_state
from euniv.proxy_pool import request_probe as request_proxy_probe
from euniv.proxy_pool import probe_one as probe_proxy_one
from euniv.proxy_pool import request_refresh as request_proxy_refresh
@@ -57,32 +53,10 @@ class PauseBody(BaseModel):
paused: bool
class AdminProxyBody(BaseModel):
employee_ids: list[str]
scheme: str
host: str = ""
port: int | None = None
username: str = ""
password: str | None = None
class AdminProxyClearBody(BaseModel):
employee_ids: list[str]
class AdminStudyStartBody(BaseModel):
employee_ids: list[str]
day: int
hour: int
minute: int
second: int
class AdminCourseWorkersBody(BaseModel):
employee_ids: list[str]
workers: int
class AdminStudyActionBody(BaseModel):
employee_ids: list[str]
action: str
@@ -185,10 +159,12 @@ def post_proxies_refresh(_: str = Depends(require_admin)) -> dict:
@admin_router.post("/proxies/probe")
def post_proxies_probe(body: AdminProxyProbeBody, _: str = Depends(require_admin)) -> dict:
try:
return probe_proxy_ids(body.ids)
except ProxyProbeError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
return request_proxy_probe(body.ids)
@admin_router.get("/proxies/probe")
def get_proxies_probe(_: str = Depends(require_admin)) -> dict:
return proxy_probe_state()
@admin_router.post("/proxies/{proxy_id}/probe")
@@ -224,19 +200,6 @@ def post_refresh(
return set_paused(db, employee_id, body.paused)
@admin_router.put("/users/proxy")
def put_users_proxy(body: AdminProxyBody, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
return set_proxy(
db,
body.employee_ids,
body.scheme,
body.host,
body.port,
body.username,
body.password,
)
@admin_router.post("/users/proxy/clear")
def post_users_proxy_clear(
body: AdminProxyClearBody,
@@ -251,30 +214,6 @@ def get_user_proxy(employee_id: str, db: Session = Depends(get_db), _: str = Dep
return read_proxy(db, employee_id)
@admin_router.put("/users/study-start")
def put_users_study_start(
body: AdminStudyStartBody, db: Session = Depends(get_db), _: str = Depends(require_admin)
) -> dict:
return set_study_start(db, body.employee_ids, body.day, body.hour, body.minute, body.second)
@admin_router.get("/users/{employee_id}/study-start")
def get_user_study_start(employee_id: str, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
return read_study_start(db, employee_id)
@admin_router.put("/users/course-workers")
def put_users_course_workers(
body: AdminCourseWorkersBody, db: Session = Depends(get_db), _: str = Depends(require_admin)
) -> dict:
return set_course_workers(db, body.employee_ids, body.workers)
@admin_router.get("/users/{employee_id}/course-workers")
def get_user_course_workers(employee_id: str, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
return read_course_workers(db, employee_id)
@admin_router.post("/users/study")
def post_users_study(
body: AdminStudyActionBody, db: Session = Depends(get_db), _: str = Depends(require_admin)
+36 -7
View File
@@ -113,20 +113,38 @@ def _require(db: Session, employee_id: str) -> Account:
def check_employee(db: Session, employee_id: str) -> dict:
from euniv.proxy import bind_url, is_proxy_failure, url_of, uses_pool
from euniv.proxy_pool import MAX_SWITCHES, mark_failed
account = _require(db, employee_id)
if not account.credential_cipher:
return {"employee_id": employee_id, "result": "未保存"}
before_password = account.password_cipher
before_credential = account.credential_cipher
password = decrypt_secret(account.password_cipher) if account.password_cipher else ""
credential = json.loads(decrypt_secret(account.credential_cipher))
pooled = uses_pool(account)
db.commit()
client = None
result = "检查失败"
valid: bool | None = None
try:
client = _open_client(employee_id, decrypt_secret(account.password_cipher), url_of(account))
client.restore(json.loads(decrypt_secret(account.credential_cipher)))
alive = bool(client.credential_alive())
result = "有效" if alive else "已失效"
valid = alive
for _ in range(MAX_SWITCHES + 1):
proxy = bind_url(account) if pooled else url_of(account)
client = _open_client(employee_id, password, proxy)
try:
client.restore(credential)
alive = bool(client.credential_alive())
result = "有效" if alive else "已失效"
valid = alive
break
except Exception as exc:
if pooled and proxy and is_proxy_failure(exc):
mark_failed(employee_id, proxy, str(exc)[:200])
client.close()
client = None
continue
raise
except Exception:
db.rollback()
account = db.get(Account, employee_id)
@@ -239,6 +257,10 @@ def set_proxy(
db.rollback()
raise HTTPException(status_code=400, detail=str(exc)) from exc
db.commit()
from euniv.autostudy import restart_work
for account in accounts:
restart_work(account.employee_id)
return {"ok": True, "count": len(accounts)}
@@ -247,6 +269,10 @@ def clear_proxy(db: Session, employee_ids: list[str]) -> dict:
for account in accounts:
clear(account)
db.commit()
from euniv.autostudy import restart_work
for account in accounts:
restart_work(account.employee_id)
return {"ok": True, "count": len(accounts)}
@@ -348,10 +374,13 @@ def set_study_settings(
db.rollback()
raise HTTPException(status_code=400, detail=str(exc)) from exc
db.commit()
from euniv.autostudy import sync_work
from euniv.autostudy import restart_work, sync_work
for account in accounts:
sync_work(account.employee_id)
if scheme is not None:
restart_work(account.employee_id)
else:
sync_work(account.employee_id)
return {"ok": True, "count": len(accounts)}