fix: 修复时区问题和翻译状态更新问题
- 使用上海时区(UTC+8)判断每日次数重置,而不是UTC时间 - 翻译任务完成后更新数据库Translation记录的状态和进度 - 传入translation_id和app到TranslationTask以支持数据库状态同步
This commit is contained in:
42
services.py
42
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()
|
||||
|
||||
Reference in New Issue
Block a user