mirror of
https://github.com/ramvignesh-b/pi-ku.git
synced 2026-05-04 08:56:52 +00:00
feat: introduce AP scheduler for polling vault
This commit is contained in:
@@ -1,5 +1,19 @@
|
||||
import os
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class LettersConfig(AppConfig):
|
||||
name = "letters"
|
||||
|
||||
def ready(self):
|
||||
"""
|
||||
Start the scheduler only when the server is starting.
|
||||
NOTE: If we don't check for RUN_MAIN, the scheduler triggers for all django operations (migration, test etc.)
|
||||
"""
|
||||
|
||||
if not (os.environ.get("RUN_MAIN") == "true" or os.environ.get("WERKZEUG_RUN_MAIN") == "true"):
|
||||
return
|
||||
from .tasks import start_scheduler
|
||||
|
||||
start_scheduler()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import logging
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from django.core.mail import send_mail
|
||||
|
||||
from config import settings
|
||||
@@ -13,8 +14,7 @@ def get_vault_letters_to_notify():
|
||||
"""
|
||||
Identifies the vault letters that have been recently unlocked and not notified
|
||||
"""
|
||||
letters = Letter.objects.filter(unlock_at__lt=datetime.now(UTC), notified_at=None)
|
||||
return letters
|
||||
return Letter.objects.filter(unlock_at__lt=datetime.now(UTC), notified_at=None)
|
||||
|
||||
|
||||
def notify_unlocked_letter(letter):
|
||||
@@ -31,8 +31,25 @@ def notify_unlocked_letter(letter):
|
||||
|
||||
|
||||
def vault_unlock_notification_polling_scheduler():
|
||||
logger.info("Starting vault_unlock_notification_polling_scheduler")
|
||||
"""
|
||||
Orchestrates the vault polling logic.
|
||||
"""
|
||||
letters_to_notify = get_vault_letters_to_notify()
|
||||
print("letters_to_notify", letters_to_notify)
|
||||
for letter in letters_to_notify:
|
||||
notify_unlocked_letter(letter)
|
||||
|
||||
|
||||
def start_scheduler():
|
||||
"""
|
||||
Starts the background scheduler for polling and notifying vault letters.
|
||||
"""
|
||||
logger.info("Starting vault polling scheduler...")
|
||||
scheduler = BackgroundScheduler()
|
||||
scheduler.add_job(
|
||||
vault_unlock_notification_polling_scheduler,
|
||||
trigger="interval",
|
||||
minutes=1,
|
||||
id="letter_polling",
|
||||
replace_existing=True,
|
||||
)
|
||||
scheduler.start()
|
||||
|
||||
@@ -327,7 +327,7 @@ class LetterTaskTest(TestCase):
|
||||
letter_to_notify1 = Letter.objects.create(
|
||||
user=self.user, type="VAULT", status="SEALED", unlock_at=datetime.now(UTC), notified_at=None
|
||||
)
|
||||
with patch("tasks.send_mail") as mock_send_mail:
|
||||
with patch("letters.tasks.send_mail") as mock_send_mail:
|
||||
notify_unlocked_letter(letter_to_notify1)
|
||||
|
||||
mock_send_mail.assert_called_with(
|
||||
@@ -342,7 +342,7 @@ class LetterTaskTest(TestCase):
|
||||
letter_to_notify2 = Letter.objects.create(
|
||||
user=self.user, type="VAULT", status="SEALED", unlock_at=datetime.now(UTC), notified_at=None
|
||||
)
|
||||
with patch("tasks.send_mail") as mock_send_mail:
|
||||
with patch("letters.tasks.send_mail") as mock_send_mail:
|
||||
mock_send_mail.side_effect = Exception()
|
||||
|
||||
notify_unlocked_letter(letter_to_notify2)
|
||||
|
||||
Reference in New Issue
Block a user