Preserve login routes across rotation and keep rejected migrations read-only

This commit is contained in:
Codex
2026-10-11 19:15:05 +08:00
parent 915241a2fd
commit ab4e4a4f79
6 changed files with 68 additions and 12 deletions
+1 -1
View File
@@ -187,7 +187,7 @@ class AuthService:
raise ValueError("请输入学信网账号和密码")
if self.browser_cancel:
self.browser_cancel.set()
with self.check_lock:
with self.check_lock, self.transport.router.hold_route():
response = self.transport.request("GET", LOGIN_URL)
soup = BeautifulSoup(response.text, "html.parser")
form = {node.get("name"): node.get("value", "") for node in soup.select("input[type=hidden][name]")}
+5 -4
View File
@@ -72,10 +72,11 @@ class CatalogClient:
raise
# A CAS cookie may still be valid while the catalogue session has expired.
# Refresh once, then retry exactly the same page; never skip its records.
status = self.auth.check()
if not status["valid"]:
raise LoginRequired(status["message"], reason="captcha" if status["state"] == "captcha_required" else "login") from None
return self._fetch_page(endpoint, payload)
with self.transport.router.hold_route():
status = self.auth.check()
if not status["valid"]:
raise LoginRequired(status["message"], reason="captcha" if status["state"] == "captcha_required" else "login") from None
return self._fetch_page(endpoint, payload)
def pages(self, endpoint, payload, page_size=10):
seen, start = set(), 0
+1 -1
View File
@@ -67,7 +67,7 @@ def main():
parser.add_argument('--config', type=Path, required=True)
args = parser.parse_args()
config = json.loads(args.config.read_text())
print(json.dumps(migrate_sqlite(args.snapshot, MySQLDatabase(config)), ensure_ascii=False))
print(json.dumps(migrate_sqlite(args.snapshot, MySQLDatabase(config, recover_jobs=False)), ensure_ascii=False))
if __name__ == '__main__':
+5 -4
View File
@@ -96,7 +96,7 @@ class Connection:
class MySQLDatabase(Database):
def __init__(self, config):
def __init__(self, config, recover_jobs=True):
self.config = {key: value for key, value in config.items() if key in ('host', 'port', 'user', 'password', 'database', 'unix_socket')}
if not self.config.get('database'):
raise ValueError('数据库配置缺少 database')
@@ -106,9 +106,10 @@ class MySQLDatabase(Database):
conn.execute(statement + ' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin')
if not conn.execute("SHOW COLUMNS FROM jobs LIKE 'resume_pending'").fetchone():
conn.execute('ALTER TABLE jobs ADD COLUMN resume_pending TINYINT DEFAULT 0')
recovery = "status IN ('running','queued') OR (status='paused' AND resume_pending=1)"
self.recovered_job_ids = [row['id'] for row in conn.execute('SELECT id FROM jobs WHERE ' + recovery)]
conn.execute("UPDATE jobs SET status='paused',resume_pending=0,message='服务重启,任务可从断点恢复' WHERE " + recovery)
if recover_jobs:
recovery = "status IN ('running','queued') OR (status='paused' AND resume_pending=1)"
self.recovered_job_ids = [row['id'] for row in conn.execute('SELECT id FROM jobs WHERE ' + recovery)]
conn.execute("UPDATE jobs SET status='paused',resume_pending=0,message='服务重启,任务可从断点恢复' WHERE " + recovery)
@contextmanager
def connection(self):