fix: Add app_context() wrapper for thread-safe DB access

This commit is contained in:
2026-01-02 16:45:11 +09:00
parent 87259d4c7c
commit f6f69f2136
3 changed files with 42 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
title: "애니 다운로더"
version: "0.4.5"
version: "0.4.6"
package_name: "anime_downloader"
developer: "projectdx"
description: "anime downloader"

View File

@@ -1325,19 +1325,21 @@ class AniLifeQueueEntity(FfmpegQueueEntity):
# Call parent's download_completed first (handles file move)
super().download_completed()
# Update DB status
db_entity = ModelAniLifeItem.get_by_anilife_id(self.info["_id"])
if db_entity is not None:
db_entity.status = "completed"
db_entity.completed_time = datetime.now()
# 메타데이터 동기화
db_entity.filename = self.filename
db_entity.save_fullpath = getattr(self, 'save_fullpath', None)
db_entity.filesize = getattr(self, 'filesize', None)
db_entity.duration = getattr(self, 'duration', None)
db_entity.quality = getattr(self, 'quality', None)
db_entity.save()
logger.info(f"[Anilife] DB status updated to 'completed': {self.info.get('title', 'Unknown')}")
# Update DB status - wrap in app context since this runs in a thread
from framework import app
with app.app_context():
db_entity = ModelAniLifeItem.get_by_anilife_id(self.info["_id"])
if db_entity is not None:
db_entity.status = "completed"
db_entity.completed_time = datetime.now()
# 메타데이터 동기화
db_entity.filename = self.filename
db_entity.save_fullpath = getattr(self, 'save_fullpath', None)
db_entity.filesize = getattr(self, 'filesize', None)
db_entity.duration = getattr(self, 'duration', None)
db_entity.quality = getattr(self, 'quality', None)
db_entity.save()
logger.info(f"[Anilife] DB status updated to 'completed': {self.info.get('title', 'Unknown')}")
def prepare_extra(self):
"""

View File

@@ -136,30 +136,32 @@ class AnimeQueueEntity(FfmpegQueueEntity):
def _update_db_status(self):
"""Update DB status to completed - generic method for all sites."""
try:
# Get the web_list_model from module_logic
model_class = getattr(self.module_logic, 'web_list_model', None)
if model_class is None:
return
from framework import app
with app.app_context():
# Get the web_list_model from module_logic
model_class = getattr(self.module_logic, 'web_list_model', None)
if model_class is None:
return
# Try to find the DB entity
db_entity = None
info = getattr(self, 'info', {})
# Try to find the DB entity
db_entity = None
info = getattr(self, 'info', {})
# Anilife uses _id
if hasattr(model_class, 'get_by_anilife_id') and info.get('_id'):
db_entity = model_class.get_by_anilife_id(info['_id'])
# Linkkf/Ohli24 might use different identifiers
elif hasattr(model_class, 'get_by_id') and info.get('db_id'):
db_entity = model_class.get_by_id(info['db_id'])
elif hasattr(model_class, 'get_by_content_code') and info.get('content_code'):
db_entity = model_class.query.filter_by(content_code=info['content_code'], episode_no=info.get('episode_no')).first()
# Anilife uses _id
if hasattr(model_class, 'get_by_anilife_id') and info.get('_id'):
db_entity = model_class.get_by_anilife_id(info['_id'])
# Linkkf/Ohli24 might use different identifiers
elif hasattr(model_class, 'get_by_id') and info.get('db_id'):
db_entity = model_class.get_by_id(info['db_id'])
elif hasattr(model_class, 'get_by_content_code') and info.get('content_code'):
db_entity = model_class.query.filter_by(content_code=info['content_code'], episode_no=info.get('episode_no')).first()
if db_entity is not None:
db_entity.status = "completed"
db_entity.completed_time = datetime.now()
db_entity.filename = getattr(self, 'filename', None)
db_entity.save()
logger.info(f"[{self.module_logic.name}] DB status updated to 'completed': {info.get('title', 'Unknown')}")
if db_entity is not None:
db_entity.status = "completed"
db_entity.completed_time = datetime.now()
db_entity.filename = getattr(self, 'filename', None)
db_entity.save()
logger.info(f"[{self.module_logic.name}] DB status updated to 'completed': {info.get('title', 'Unknown')}")
except Exception as e:
logger.error(f"Failed to update DB status: {e}")