From e5ba13980fc365e890279d1b56a73cdf6e901fc5 Mon Sep 17 00:00:00 2001 From: coder Date: Fri, 10 Apr 2026 18:26:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=B6=E5=8C=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=92=8C=E7=BF=BB=E8=AF=91=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用上海时区(UTC+8)判断每日次数重置,而不是UTC时间 - 翻译任务完成后更新数据库Translation记录的状态和进度 - 传入translation_id和app到TranslationTask以支持数据库状态同步 --- app.py | 21 +++++++++++++++------ models.py | 10 +++++++--- services.py | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 2c9f661..a8cca29 100644 --- a/app.py +++ b/app.py @@ -229,13 +229,8 @@ def upload_pdf(): os.makedirs(output_dir, exist_ok=True) output_path = os.path.join(output_dir, f"{filename}_translated.md") - # 创建异步翻译任务 + # 创建异步翻译任务(先不创建,等translation_id生成后) task_id = str(uuid.uuid4()) - TranslationTask.create_task( - task_id, upload_path, output_path, - {'LLM_CONFIG': LLM_CONFIG}, - instruction - ) # 创建翻译记录 translation = Translation( @@ -252,6 +247,20 @@ def upload_pdf(): ) db.session.add(translation) + # 预先提交获取 translation_id + if not from_cache: + db.session.flush() # 获取 ID 但不提交完整事务 + + # 创建异步翻译任务(需要翻译时) + if not from_cache: + TranslationTask.create_task( + task_id, upload_path, output_path, + {'LLM_CONFIG': LLM_CONFIG}, + instruction, + translation_id=translation.id, + app=app + ) + # 更新用户/访客计数 if user: user.increment_count() diff --git a/models.py b/models.py index 5e1898a..a4002b7 100644 --- a/models.py +++ b/models.py @@ -67,8 +67,10 @@ class User(db.Model): if max_pages > 0 and pages > max_pages: return False, f"PDF页数超出限制(最大{max_pages}页)" - # 检查每日次数限制 - today = datetime.utcnow().date() + # 检查每日次数限制 - 使用上海时间(UTC+8) + from datetime import timezone, timedelta + shanghai_tz = timezone(timedelta(hours=8)) + today = datetime.now(shanghai_tz).date() if self.last_translate_date != today: self.daily_count = 0 self.last_translate_date = today @@ -81,7 +83,9 @@ class User(db.Model): def increment_count(self): """增加翻译计数""" - today = datetime.utcnow().date() + from datetime import timezone, timedelta + shanghai_tz = timezone(timedelta(hours=8)) + today = datetime.now(shanghai_tz).date() if self.last_translate_date != today: self.daily_count = 0 self.last_translate_date = today diff --git a/services.py b/services.py index cb2af14..4b6a2ba 100644 --- a/services.py +++ b/services.py @@ -255,7 +255,7 @@ class TranslationTask: lock = threading.Lock() @classmethod - def create_task(cls, task_id, pdf_path, output_path, config, instruction=None): + def create_task(cls, task_id, pdf_path, output_path, config, instruction=None, translation_id=None, app=None): """创建翻译任务""" task = { 'id': task_id, @@ -266,6 +266,7 @@ class TranslationTask: 'error': None, 'started_at': None, 'completed_at': None, + 'translation_id': translation_id, } with cls.lock: @@ -277,10 +278,28 @@ class TranslationTask: task['status'] = 'processing' task['started_at'] = datetime.now().isoformat() + # 更新数据库状态为 processing + if app and translation_id: + with app.app_context(): + from models import db, Translation + trans = Translation.query.get(translation_id) + if trans: + trans.status = 'processing' + db.session.commit() + def progress_callback(progress, total, message): with cls.lock: task['progress'] = progress task['message'] = message + + # 更新数据库进度 + if app and translation_id: + with app.app_context(): + from models import db, Translation + trans = Translation.query.get(translation_id) + if trans: + trans.progress = progress + db.session.commit() try: result = service.translate_pdf( @@ -291,11 +310,32 @@ class TranslationTask: task['message'] = '翻译完成' task['completed_at'] = datetime.now().isoformat() task['result'] = result + + # 更新数据库状态为 completed + if app and translation_id: + with app.app_context(): + from models import db, Translation + trans = Translation.query.get(translation_id) + if trans: + trans.status = 'completed' + trans.progress = 100 + trans.completed_at = datetime.now() + db.session.commit() except Exception as e: task['status'] = 'failed' task['error'] = str(e) task['message'] = f'翻译失败: {e}' + + # 更新数据库状态为 failed + if app and translation_id: + with app.app_context(): + from models import db, Translation + trans = Translation.query.get(translation_id) + if trans: + trans.status = 'failed' + trans.error_message = str(e) + db.session.commit() thread = threading.Thread(target=run_translation) thread.start()