Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions backend/app/core/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from apscheduler.schedulers.background import BackgroundScheduler

from app.core.database import SessionLocal
Expand Down Expand Up @@ -42,8 +44,13 @@ def start_scheduler_with_interval():
replace_existing=True,
)
if not scheduler.running:
# Run the job immediately once
job()
scheduler.add_job(
job,
"date",
run_date=datetime.now(),
id="initial_email_check",
replace_existing=True,
)
scheduler.start()
logger.info("Scheduler started.")
else:
Expand Down
14 changes: 11 additions & 3 deletions backend/app/tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from unittest.mock import ANY, MagicMock, patch

from sqlalchemy.orm import Session
Expand Down Expand Up @@ -101,10 +102,14 @@ def test_process_emails(mock_imap, db_session: Session):
@patch("app.core.scheduler.job")
@patch("app.core.scheduler.SessionLocal")
@patch("app.core.scheduler.scheduler")
@patch("app.core.scheduler.datetime")
def test_start_scheduler_with_interval(
mock_scheduler, mock_session_local, mock_job, db_session: Session
mock_datetime, mock_scheduler, mock_session_local, mock_job, db_session: Session
):
"""Test starting the scheduler with an interval."""
fixed_now = datetime(2025, 10, 20, 12, 0, 0)
mock_datetime.now.return_value = fixed_now

mock_session_local.return_value = db_session
mock_scheduler.running = False
settings_data = SettingsCreate(
Expand All @@ -120,10 +125,13 @@ def test_start_scheduler_with_interval(
start_scheduler_with_interval()

mock_scheduler.add_job.assert_called_with(
ANY, "interval", minutes=30, id="email_check_job", replace_existing=True
ANY,
"date",
run_date=fixed_now,
id="initial_email_check",
replace_existing=True,
)
mock_scheduler.start.assert_called_once()
mock_job.assert_called_once()


@patch("app.core.scheduler.SessionLocal")
Expand Down