Step 5: Validate account_id is required and exists in tasks.py
- Make account_id parameter required (remove default None) - Validate account_id is provided before proceeding - Validate account exists in database - Return proper error responses with error_type - Update function docstring to reflect required parameter
This commit is contained in:
@@ -10,7 +10,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@shared_task(bind=True, max_retries=3)
|
||||
def run_ai_task(self, function_name: str, payload: dict, account_id: int = None):
|
||||
def run_ai_task(self, function_name: str, payload: dict, account_id: int):
|
||||
"""
|
||||
Single Celery entrypoint for all AI functions.
|
||||
Dynamically loads and executes the requested function.
|
||||
@@ -18,7 +18,10 @@ def run_ai_task(self, function_name: str, payload: dict, account_id: int = None)
|
||||
Args:
|
||||
function_name: Name of the AI function (e.g., 'auto_cluster')
|
||||
payload: Function-specific payload
|
||||
account_id: Account ID for account isolation
|
||||
account_id: Account ID for account isolation (required)
|
||||
|
||||
Raises:
|
||||
Returns error dict if account_id not provided or account not found
|
||||
"""
|
||||
logger.info("=" * 80)
|
||||
logger.info(f"run_ai_task STARTED: {function_name}")
|
||||
@@ -29,14 +32,28 @@ def run_ai_task(self, function_name: str, payload: dict, account_id: int = None)
|
||||
logger.info("=" * 80)
|
||||
|
||||
try:
|
||||
# Get account
|
||||
account = None
|
||||
if account_id:
|
||||
from igny8_core.auth.models import Account
|
||||
try:
|
||||
account = Account.objects.get(id=account_id)
|
||||
except Account.DoesNotExist:
|
||||
logger.warning(f"Account {account_id} not found")
|
||||
# Validate account_id is provided
|
||||
if not account_id:
|
||||
error_msg = "account_id is required for AI task execution"
|
||||
logger.error(f"[run_ai_task] {error_msg}")
|
||||
return {
|
||||
'success': False,
|
||||
'error': error_msg,
|
||||
'error_type': 'ConfigurationError'
|
||||
}
|
||||
|
||||
# Get account and validate it exists
|
||||
from igny8_core.auth.models import Account
|
||||
try:
|
||||
account = Account.objects.get(id=account_id)
|
||||
except Account.DoesNotExist:
|
||||
error_msg = f"Account {account_id} not found"
|
||||
logger.error(f"[run_ai_task] {error_msg}")
|
||||
return {
|
||||
'success': False,
|
||||
'error': error_msg,
|
||||
'error_type': 'AccountNotFound'
|
||||
}
|
||||
|
||||
# Get function from registry
|
||||
fn = get_function_instance(function_name)
|
||||
|
||||
Reference in New Issue
Block a user