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: "애니 다운로더" title: "애니 다운로더"
version: "0.4.5" version: "0.4.6"
package_name: "anime_downloader" package_name: "anime_downloader"
developer: "projectdx" developer: "projectdx"
description: "anime downloader" description: "anime downloader"

View File

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

View File

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