Poll dispatch_due in the worker so scheduled posts fire.
CI / test (pull_request) Successful in 16s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-24 10:04:24 -05:00
co-authored by Cursor
parent 96741bc0c8
commit e79577811e
7 changed files with 113 additions and 8 deletions
+34 -2
View File
@@ -1,3 +1,5 @@
import time
from django.core.management.base import BaseCommand
from django.utils import timezone
@@ -10,10 +12,37 @@ from social.tasks import publish_social_post
class Command(BaseCommand):
help = (
"Enqueue due scheduled campaign messages and social posts. "
"Optional when the task backend supports run_after defer; useful as a safety net."
"Pass --loop to poll until stopped (worker entrypoint)."
)
def add_arguments(self, parser):
parser.add_argument(
"--loop",
action="store_true",
help="Poll forever until SIGINT/SIGTERM.",
)
parser.add_argument(
"--interval",
type=int,
default=15,
help="Seconds between polls when --loop is set (default 15).",
)
def handle(self, *args, **options):
self.verbosity = int(options.get("verbosity", 1))
if options["loop"]:
interval = max(1, int(options["interval"] or 15))
self.stdout.write(f"Polling due scheduled work every {interval}s.")
try:
while True:
self._dispatch_once()
time.sleep(interval)
except KeyboardInterrupt:
self.stdout.write("dispatch_due loop stopped.")
return
self._dispatch_once()
def _dispatch_once(self):
now = timezone.now()
enqueued = 0
@@ -35,4 +64,7 @@ class Command(BaseCommand):
publish_social_post.enqueue(post_id=str(post.pk))
enqueued += 1
self.stdout.write(self.style.SUCCESS(f"Enqueued {enqueued} due item(s)."))
if enqueued:
self.stdout.write(self.style.SUCCESS(f"Enqueued {enqueued} due item(s)."))
elif self.verbosity >= 2:
self.stdout.write("No due items.")
+48
View File
@@ -1,5 +1,13 @@
from datetime import timedelta
from io import StringIO
from unittest.mock import patch
from django.core.management import call_command
from django.test import Client, TestCase, override_settings
from django.urls import reverse
from django.utils import timezone
from social.models import SocialPost
class HealthzTests(TestCase):
@@ -27,3 +35,43 @@ class PublicSmokeTests(TestCase):
client = Client()
self.assertEqual(client.get(reverse("public:about")).status_code, 200)
self.assertEqual(client.get(reverse("public:contact")).status_code, 200)
class DispatchDueTests(TestCase):
def test_enqueues_past_scheduled_social_post(self):
post = SocialPost.objects.create(
body="Open house",
status=SocialPost.Status.SCHEDULED,
scheduled_for=timezone.now() - timedelta(minutes=5),
)
mock_task = patch(
"core.management.commands.dispatch_due.publish_social_post"
).start()
self.addCleanup(patch.stopall)
out = StringIO()
call_command("dispatch_due", stdout=out)
post.refresh_from_db()
self.assertEqual(post.status, SocialPost.Status.QUEUED)
mock_task.enqueue.assert_called_once_with(post_id=str(post.pk))
self.assertIn("Enqueued 1 due item(s).", out.getvalue())
def test_skips_future_scheduled_social_post(self):
post = SocialPost.objects.create(
body="Later",
status=SocialPost.Status.SCHEDULED,
scheduled_for=timezone.now() + timedelta(hours=1),
)
mock_task = patch(
"core.management.commands.dispatch_due.publish_social_post"
).start()
self.addCleanup(patch.stopall)
call_command("dispatch_due", stdout=StringIO())
post.refresh_from_db()
self.assertEqual(post.status, SocialPost.Status.SCHEDULED)
mock_task.enqueue.assert_not_called()
def test_quiet_when_nothing_due(self):
out = StringIO()
call_command("dispatch_due", stdout=out)
self.assertEqual(out.getvalue(), "")