
- Set up FastAPI application structure - Implement SQLite database with SQLAlchemy - Create WhatsApp webhook endpoints - Implement message storage and analysis - Integrate Gemini 2.5 Pro for message analysis - Add email delivery of insights - Configure APScheduler for weekend analysis - Add linting with Ruff
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
"""
|
|
Scheduler module for running periodic tasks.
|
|
"""
|
|
import logging
|
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
from apscheduler.triggers.cron import CronTrigger
|
|
|
|
from app.core.config import settings
|
|
from app.services.analysis_service import analyze_messages_and_send_insights
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create scheduler
|
|
scheduler = AsyncIOScheduler()
|
|
|
|
|
|
def start_scheduler():
|
|
"""
|
|
Start the scheduler with the message analysis cron job.
|
|
"""
|
|
if not scheduler.running:
|
|
# Schedule the message analysis job to run on weekends (Sunday by default)
|
|
scheduler.add_job(
|
|
analyze_messages_and_send_insights,
|
|
trigger=CronTrigger(
|
|
day_of_week=settings.ANALYSIS_CRON_DAY_OF_WEEK,
|
|
hour=settings.ANALYSIS_CRON_HOUR,
|
|
minute=settings.ANALYSIS_CRON_MINUTE,
|
|
),
|
|
id="message_analysis_job",
|
|
name="Analyze WhatsApp messages and send insights via email",
|
|
replace_existing=True,
|
|
)
|
|
|
|
# Log the next run time
|
|
job = scheduler.get_job("message_analysis_job")
|
|
if job:
|
|
next_run_time = job.next_run_time
|
|
if next_run_time:
|
|
logger.info(
|
|
"Message analysis job scheduled. Next run: %s",
|
|
next_run_time.strftime("%Y-%m-%d %H:%M:%S"),
|
|
)
|
|
|
|
# Start the scheduler
|
|
scheduler.start()
|
|
logger.info("Scheduler started successfully")
|
|
else:
|
|
logger.info("Scheduler is already running")
|