From 9832af0293faea99fd5bffd88c369099245dce77 Mon Sep 17 00:00:00 2001
From: Jonas Braathen <jonas.braathen@usit.uio.no>
Date: Thu, 6 Apr 2023 13:56:53 +0200
Subject: [PATCH] Add support for running a one-off role end notification, for
 debugging

---
 greg/management/commands/role_end_notifier.py | 37 +++++++++++++++----
 1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/greg/management/commands/role_end_notifier.py b/greg/management/commands/role_end_notifier.py
index c26d3a73..569cae3a 100644
--- a/greg/management/commands/role_end_notifier.py
+++ b/greg/management/commands/role_end_notifier.py
@@ -1,28 +1,51 @@
 """
-Command for scheduling the django-q task for notifying sponsors of
+Command for scheduling or running the task for notifying sponsors of
 expiring roles.
-
-Instead of having a task that gets scheduled, we could have had a
-management command that was run as a cronjob. However, that would mean
-another cronjob
 """
 
 import logging
 import logging.config
 
 from django.conf import settings
-from django.core.management.base import BaseCommand
+from django.core.management.base import BaseCommand, CommandParser
+from django.db import transaction
 from django_q.tasks import schedule
 
+from greg.tasks import notify_sponsors_roles_ending
 
 logging.config.dictConfig(settings.LOGGING)
 logger = logging.getLogger(__name__)
 
 
 class Command(BaseCommand):
-    help = "Schedule notification of sponsors"
+    help = __doc__
+
+    def add_arguments(self, parser: CommandParser) -> None:
+        parser.add_argument(
+            "--schedule",
+            default=False,
+            action="store_true",
+            help="Add a scheduled task for running the role end notifier",
+        )
+        parser.add_argument(
+            "--run-once",
+            default=False,
+            action="store_true",
+            help="Run the import once",
+        )
 
     def handle(self, *args, **options):
+        if options["schedule"]:
+            self.schedule()
+            return
+
+        if options["run_once"]:
+            notify_sponsors_roles_ending()
+            return
+
+        logger.info("Nothing done")
+
+    def schedule(self, *args, **options):
         logger.info("Scheduling role end notifier task...")
         schedule(
             func="greg.tasks.notify_sponsors_roles_ending",
-- 
GitLab