bornhack-website/src/program/management/commands/notification_worker.py

55 lines
2.2 KiB
Python
Raw Normal View History

from django.core.management.base import BaseCommand
from django.conf import settings
from django.utils import timezone
from time import sleep
import irc3, sys, asyncio
from ircbot.models import OutgoingIrcMessage
from camps.utils import get_current_camp
from django.utils import timezone
from program.models import EventInstance
from datetime import timedelta
2017-03-23 17:32:13 +00:00
import logging
2019-06-16 12:32:24 +00:00
logger = logging.getLogger("bornhack.%s" % __name__)
class Command(BaseCommand):
2019-06-16 12:32:24 +00:00
args = "none"
help = "Queue notifications for channels and users for upcoming event instances."
def output(self, message):
2019-06-16 12:32:24 +00:00
self.stdout.write(
"%s: %s" % (timezone.now().strftime("%Y-%m-%d %H:%M:%S"), message)
)
def handle(self, *args, **options):
2019-06-16 12:32:24 +00:00
self.output("Schedule notification worker running...")
while True:
camp = get_current_camp()
if camp:
# a camp is currently going on, check if we need to send out any notifications
for ei in EventInstance.objects.filter(
event__camp=camp,
event__event_type__notifications=True,
notifications_sent=False,
2019-06-16 12:32:24 +00:00
when__startswith__lt=timezone.now()
+ timedelta(
minutes=settings.SCHEDULE_EVENT_NOTIFICATION_MINUTES
), # start of event is less than X minutes away
when__startswith__gt=timezone.now(), # but event has not started yet
):
# this event is less than settings.SCHEDULE_EVENT_NOTIFICATION_MINUTES minutes from starting, queue an IRC notificatio
2017-02-02 13:40:26 +00:00
oim = OutgoingIrcMessage.objects.create(
target=settings.IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL,
message="starting soon: %s" % ei,
2019-06-16 12:32:24 +00:00
timeout=ei.when.lower,
)
2019-06-16 12:32:24 +00:00
logger.info(
"added irc message id %s for eventinstance %s" % (oim.id, ei)
)
ei.notifications_sent = True
ei.save()
# check once per minute
sleep(60)