feat: image generation overhaul (model-aware text, dim clamping, \.30 pricing), event-driven dashboard cache invalidation, SEO insights (AI visibility, GSC, keyword gap), YouTube OAuth/publish, blog writer & content planning improvements, scheduler monitoring updates

This commit is contained in:
ajaysi
2026-05-30 07:58:22 +05:30
parent aaf94049da
commit 64f1f88cdd
129 changed files with 8796 additions and 8755 deletions

View File

@@ -1,10 +1,9 @@
"""
Interval Manager
Handles intelligent scheduling interval adjustment based on active strategies.
Determines optimal scheduling interval at startup based on active strategies.
"""
from typing import TYPE_CHECKING
from datetime import datetime
from sqlalchemy.orm import Session
from services.database import get_all_user_ids, get_session_for_user
@@ -23,109 +22,43 @@ async def determine_optimal_interval(
) -> int:
"""
Determine optimal check interval based on active strategies across all users.
Only one strategy can be active per user at a time, so this is a simple
exists/not-exists check: does any user have an active strategy?
Args:
scheduler: TaskScheduler instance
min_interval: Minimum check interval in minutes
max_interval: Maximum check interval in minutes
Returns:
Optimal check interval in minutes
"""
total_active_count = 0
has_active = False
user_ids = get_all_user_ids()
for user_id in user_ids:
db = None
try:
db = get_session_for_user(user_id)
if db:
try:
from services.active_strategy_service import ActiveStrategyService
active_strategy_service = ActiveStrategyService(db_session=db)
user_active_count = active_strategy_service.count_active_strategies_with_tasks()
total_active_count += user_active_count
# Optimization: If we found at least one active strategy, we can stop and return min_interval
# (unless we want accurate stats)
# For stats accuracy, we should continue.
except Exception as e:
logger.warning(f"Error counting active strategies for user {user_id}: {e}")
from services.active_strategy_service import ActiveStrategyService
active_strategy_service = ActiveStrategyService(db_session=db)
if active_strategy_service.has_active_strategies_with_tasks():
has_active = True
break
except Exception as e:
logger.warning(f"Error checking user {user_id} for strategies: {e}")
logger.warning(f"Error checking active strategies for user {user_id}: {e}")
finally:
if db:
db.close()
scheduler.stats['active_strategies_count'] = total_active_count
if total_active_count > 0:
logger.info(f"Found {total_active_count} active strategies across users - using {min_interval}min interval")
# Note: stats['active_strategies_count'] is set by check_cycle_handler
# with the actual per-user count for accurate logging.
if has_active:
logger.info(f"Active strategies found - using {min_interval}min interval")
return min_interval
else:
logger.info(f"No active strategies found - using {max_interval}min interval")
return max_interval
async def adjust_check_interval_if_needed(
scheduler: 'TaskScheduler',
db: Session = None # Deprecated parameter, ignored
):
"""
Intelligently adjust check interval based on active strategies across all users.
If there are active strategies with tasks, check more frequently.
If there are no active strategies, check less frequently.
Args:
scheduler: TaskScheduler instance
db: Deprecated/Ignored
"""
total_active_count = 0
user_ids = get_all_user_ids()
for user_id in user_ids:
user_db = None
try:
user_db = get_session_for_user(user_id)
if user_db:
try:
from services.active_strategy_service import ActiveStrategyService
active_strategy_service = ActiveStrategyService(db_session=user_db)
user_active_count = active_strategy_service.count_active_strategies_with_tasks()
total_active_count += user_active_count
except Exception as e:
logger.warning(f"Error counting active strategies for user {user_id}: {e}")
except Exception as e:
logger.warning(f"Error checking user {user_id} for strategies: {e}")
finally:
if user_db:
user_db.close()
scheduler.stats['active_strategies_count'] = total_active_count
# Determine optimal interval
if total_active_count > 0:
optimal_interval = scheduler.min_check_interval_minutes
else:
optimal_interval = scheduler.max_check_interval_minutes
# Only reschedule if interval needs to change
if optimal_interval != scheduler.current_check_interval_minutes:
interval_message = (
f"[Scheduler] ⚙️ Adjusting Check Interval\n"
f" ├─ Current: {scheduler.current_check_interval_minutes}min\n"
f" ├─ Optimal: {optimal_interval}min\n"
f" ├─ Active Strategies: {total_active_count}\n"
f" └─ Reason: {'Active strategies detected' if total_active_count > 0 else 'No active strategies'}"
)
logger.warning(interval_message)
# Reschedule the job with new interval
scheduler.scheduler.modify_job(
job_id='check_due_tasks', # Fixed job_id from check_cycle to check_due_tasks to match scheduler.py
trigger=scheduler._get_trigger_for_interval(optimal_interval)
)
scheduler.current_check_interval_minutes = optimal_interval
scheduler.stats['last_interval_adjustment'] = datetime.utcnow().isoformat()