Se corrigio bug en las tareas
This commit is contained in:
@@ -210,36 +210,52 @@ def sync_from_hub_task():
|
||||
|
||||
# 3. Aplicar actualizaciones
|
||||
for art_data in articles_data:
|
||||
# Convertir string a datetime
|
||||
server_updated_at = datetime.fromisoformat(art_data['updated_at'])
|
||||
try:
|
||||
# Logic similar to sync_article but simpler (Force Update from Hub)
|
||||
# We assume Hub is Truth in this Polling flow
|
||||
|
||||
# Try to find by UUID
|
||||
local_article = db.query(HelpArticle).filter(HelpArticle.uuid == art_data['uuid']).first()
|
||||
|
||||
# Fallback: find by Slug if UUID doesn't match
|
||||
if not local_article:
|
||||
local_article = db.query(HelpArticle).filter(HelpArticle.slug == art_data['slug']).first()
|
||||
|
||||
server_updated_at = datetime.fromisoformat(art_data['updated_at'])
|
||||
if server_updated_at.tzinfo is None:
|
||||
server_updated_at = server_updated_at.replace(tzinfo=timezone.utc)
|
||||
|
||||
if not local_article:
|
||||
new_article = HelpArticle(
|
||||
uuid=art_data['uuid'],
|
||||
slug=art_data['slug'],
|
||||
title=art_data['title'],
|
||||
content=art_data['content'],
|
||||
updated_at=server_updated_at,
|
||||
last_editor=art_data['last_editor'],
|
||||
category=art_data.get('category', "General"),
|
||||
order=art_data.get('order', 0)
|
||||
)
|
||||
db.add(new_article)
|
||||
logger.info(f"Created new article: {art_data['slug']}")
|
||||
else:
|
||||
# Update existing article
|
||||
# If UUID changed in Hub but slug is the same, we update UUID too
|
||||
local_article.uuid = art_data['uuid']
|
||||
local_article.slug = art_data['slug']
|
||||
local_article.title = art_data['title']
|
||||
local_article.content = art_data['content']
|
||||
local_article.updated_at = server_updated_at
|
||||
local_article.last_editor = art_data['last_editor']
|
||||
local_article.category = art_data.get('category', "General")
|
||||
local_article.order = art_data.get('order', 0)
|
||||
logger.info(f"Updated article: {art_data['slug']}")
|
||||
|
||||
# Logic similar to sync_article but simpler (Force Update from Hub)
|
||||
# We assume Hub is Truth in this Polling flow
|
||||
local_article = db.query(HelpArticle).filter(HelpArticle.uuid == art_data['uuid']).first()
|
||||
|
||||
if not local_article:
|
||||
new_article = HelpArticle(
|
||||
uuid=art_data['uuid'],
|
||||
slug=art_data['slug'],
|
||||
title=art_data['title'],
|
||||
content=art_data['content'],
|
||||
updated_at=server_updated_at,
|
||||
last_editor=art_data['last_editor'],
|
||||
category=art_data.get('category', "General"),
|
||||
order=art_data.get('order', 0)
|
||||
)
|
||||
db.add(new_article)
|
||||
else:
|
||||
if server_updated_at > local_article.updated_at.replace(tzinfo=timezone.utc):
|
||||
local_article.slug = art_data['slug']
|
||||
local_article.title = art_data['title']
|
||||
local_article.content = art_data['content']
|
||||
local_article.updated_at = server_updated_at
|
||||
local_article.last_editor = art_data['last_editor']
|
||||
local_article.category = art_data.get('category', "General")
|
||||
local_article.order = art_data.get('order', 0)
|
||||
|
||||
db.commit()
|
||||
db.commit() # Commit each article to avoid bulk failure
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error syncing article {art_data.get('slug', 'unknown')}: {e}")
|
||||
|
||||
|
||||
# Download assets after bulk update (Polling)
|
||||
from .utils import download_file_from_hub, sync_assets_from_content
|
||||
|
||||
Reference in New Issue
Block a user