2020-02-12 12:10:41 +00:00
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
|
|
|
from time import sleep
|
|
|
|
|
2017-02-02 13:37:17 +00:00
|
|
|
from django.conf import settings
|
2020-02-12 12:10:41 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
2017-02-02 13:37:17 +00:00
|
|
|
from django.utils import timezone
|
2020-02-12 12:10:41 +00:00
|
|
|
|
2017-02-02 13:37:17 +00:00
|
|
|
from camps.utils import get_current_camp
|
2020-02-12 12:10:41 +00:00
|
|
|
from ircbot.models import OutgoingIrcMessage
|
2017-02-02 13:37:17 +00:00
|
|
|
from program.models import EventInstance
|
2017-03-23 17:32:13 +00:00
|
|
|
|
2019-06-16 12:32:24 +00:00
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
2017-02-02 13:37:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2019-06-16 12:32:24 +00:00
|
|
|
args = "none"
|
|
|
|
help = "Queue notifications for channels and users for upcoming event instances."
|
2017-02-02 13:37:17 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
2017-02-02 13:37:17 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2019-06-16 12:32:24 +00:00
|
|
|
self.output("Schedule notification worker running...")
|
2017-02-02 13:37:17 +00:00
|
|
|
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
|
2017-02-02 13:37:17 +00:00
|
|
|
):
|
|
|
|
# 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(
|
2017-02-02 13:37:17 +00:00
|
|
|
target=settings.IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL,
|
|
|
|
message="starting soon: %s" % ei,
|
2019-06-16 12:32:24 +00:00
|
|
|
timeout=ei.when.lower,
|
2017-02-02 13:37:17 +00:00
|
|
|
)
|
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
|
2017-02-02 13:37:17 +00:00
|
|
|
ei.save()
|
|
|
|
|
|
|
|
# check once per minute
|
|
|
|
sleep(60)
|