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:
IGNY8 VPS (Salman)
2025-11-16 09:22:33 +00:00
parent 91d31ece31
commit 25b1aa39b0

View File

@@ -10,7 +10,7 @@ logger = logging.getLogger(__name__)
@shared_task(bind=True, max_retries=3) @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. Single Celery entrypoint for all AI functions.
Dynamically loads and executes the requested function. 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: Args:
function_name: Name of the AI function (e.g., 'auto_cluster') function_name: Name of the AI function (e.g., 'auto_cluster')
payload: Function-specific payload 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("=" * 80)
logger.info(f"run_ai_task STARTED: {function_name}") 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) logger.info("=" * 80)
try: try:
# Get account # Validate account_id is provided
account = None if not account_id:
if account_id: error_msg = "account_id is required for AI task execution"
from igny8_core.auth.models import Account logger.error(f"[run_ai_task] {error_msg}")
try: return {
account = Account.objects.get(id=account_id) 'success': False,
except Account.DoesNotExist: 'error': error_msg,
logger.warning(f"Account {account_id} not found") '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 # Get function from registry
fn = get_function_instance(function_name) fn = get_function_instance(function_name)