Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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.")
|
||||
|
||||
Reference in New Issue
Block a user