| import os |
| import time |
| from server.celery_app import celery_app |
| from server.db import SessionLocal, Action |
| from datetime import datetime |
|
|
| @celery_app.task(name="server.executor.execute_content_task", bind=True, max_retries=3) |
| def execute_content_task(self, action_id: int): |
| """Executes a content-related task (e.g. Generate Article).""" |
| db = SessionLocal() |
| try: |
| action = db.query(Action).filter(Action.id == action_id).first() |
| if not action: |
| return {"status": "error", "message": "Action not found"} |
| |
| action.status = "executing" |
| db.commit() |
| |
| |
| from server import impact_engine |
| impact_engine.record_initial_metrics(action_id) |
| |
| |
| print(f"Executing content task: {action.task}") |
| time.sleep(3) |
| |
| |
| action.status = "done" |
| action.result = { |
| "message": "Content generated successfully", |
| "traffic_gain_est": "15%", |
| "words": 1200 |
| } |
| |
| |
| from server import impact_engine |
| impact_engine.update_action_impact(action_id) |
| |
| db.commit() |
| return {"status": "success", "action_id": action_id} |
| except Exception as e: |
|
|
| db.rollback() |
| raise self.retry(exc=e, countdown=5) |
| finally: |
| db.close() |
|
|
| @celery_app.task(name="server.executor.execute_technical_task", bind=True, max_retries=3) |
| def execute_technical_task(self, action_id: int): |
| """Executes SEO/technical/outreach task.""" |
| db = SessionLocal() |
| try: |
| action = db.query(Action).filter(Action.id == action_id).first() |
| if not action: |
| return {"status": "error", "message": "Action not found"} |
| |
| action.status = "executing" |
| db.commit() |
| |
| |
| print(f"Executing technical task: {action.task}") |
| time.sleep(2) |
| |
| action.status = "done" |
| action.result = { |
| "message": "Task processed successfully", |
| "impact_est": "High" |
| } |
| |
| |
| from server import impact_engine |
| impact_engine.update_action_impact(action_id) |
| |
| |
| if action.type == 'outreach': |
|
|
| from server import outreach_engine |
| |
| |
| print("Processing outreach campaign step...") |
| |
| db.commit() |
| return {"status": "success"} |
| except Exception as e: |
|
|
| db.rollback() |
| raise self.retry(exc=e, countdown=5) |
| finally: |
| db.close() |
|
|
| def dispatch_action(action_id: int, action_type: str): |
| """Dispatches the action to the right executor based on type.""" |
| |
| act_type = str(action_type).lower() |
| if act_type == 'content': |
| execute_content_task.delay(action_id) |
| else: |
| execute_technical_task.delay(action_id) |
| return True |
|
|