This commit is contained in:
alorig
2025-11-29 15:35:41 +05:00
parent 0549dea124
commit 9e6868fe69
5 changed files with 99 additions and 8 deletions

View File

@@ -31,19 +31,24 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
from igny8_core.business.content.models import Content
from igny8_core.business.integration.models import SiteIntegration
logger.info(f"[publish_content_to_wordpress] 🎯 Celery task started: content_id={content_id}, site_integration_id={site_integration_id}")
# Get content and site integration
try:
content = Content.objects.get(id=content_id)
logger.info(f"[publish_content_to_wordpress] 📄 Content loaded: title='{content.title}'")
site_integration = SiteIntegration.objects.get(id=site_integration_id)
logger.info(f"[publish_content_to_wordpress] 🔌 Integration loaded: platform={site_integration.platform}, site={site_integration.site.name}")
except (Content.DoesNotExist, SiteIntegration.DoesNotExist) as e:
logger.error(f"Content or site integration not found: {e}")
logger.error(f"[publish_content_to_wordpress] ❌ Content or site integration not found: {e}")
return {"success": False, "error": str(e)}
# Check if content is already published
if content.external_id:
logger.info(f"Content {content_id} already published to WordPress")
logger.info(f"[publish_content_to_wordpress] ⚠️ Content {content_id} already published: external_id={content.external_id}")
return {"success": True, "message": "Already published", "external_id": content.external_id}
logger.info(f"[publish_content_to_wordpress] 📦 Preparing content payload...")
# Prepare content data for WordPress
# Generate excerpt from content_html (Content model has no 'brief' field)
excerpt = ''
@@ -88,16 +93,20 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
'X-IGNY8-API-KEY': site_integration.api_key,
}
logger.info(f"[publish_content_to_wordpress] 🚀 POSTing to WordPress: {wordpress_url}")
response = requests.post(
wordpress_url,
json=content_data,
headers=headers,
timeout=30
)
logger.info(f"[publish_content_to_wordpress] 📬 WordPress response: status={response.status_code}")
if response.status_code == 201:
# Success
wp_data = response.json().get('data', {})
logger.info(f"[publish_content_to_wordpress] ✅ WordPress post created successfully: post_id={wp_data.get('post_id')}")
# Update external_id and external_url for unified Content model
content.external_id = wp_data.get('post_id')
content.external_url = wp_data.get('post_url')
@@ -105,8 +114,9 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
content.save(update_fields=[
'external_id', 'external_url', 'status', 'updated_at'
])
logger.info(f"[publish_content_to_wordpress] 💾 Content model updated: external_id={content.external_id}, status=published")
logger.info(f"Successfully published content {content_id} to WordPress post {content.external_id}")
logger.info(f"[publish_content_to_wordpress] 🎉 Successfully published content {content_id} to WordPress post {content.external_id}")
return {
"success": True,
"external_id": content.external_id,
@@ -129,19 +139,21 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
else:
# Error
error_msg = f"WordPress API error: {response.status_code} - {response.text}"
logger.error(error_msg)
logger.error(f"[publish_content_to_wordpress] ❌ {error_msg}")
# Retry logic
if self.request.retries < self.max_retries:
# Exponential backoff: 1min, 5min, 15min
countdown = 60 * (5 ** self.request.retries)
logger.warning(f"[publish_content_to_wordpress] 🔄 Retrying (attempt {self.request.retries + 1}/{self.max_retries}) in {countdown}s")
raise self.retry(countdown=countdown, exc=Exception(error_msg))
else:
# Max retries reached - mark as failed
logger.error(f"[publish_content_to_wordpress] ❌ Max retries reached, giving up")
return {"success": False, "error": error_msg}
except Exception as e:
logger.error(f"Error publishing content {content_id}: {str(e)}", exc_info=True)
logger.error(f"[publish_content_to_wordpress] ❌ Exception during publish: {str(e)}", exc_info=True)
return {"success": False, "error": str(e)}