Files
RosterChief/events/tasks.py
Bernard Siebens 17b1c0220a Add a periodic deadline-reminder job for events, including recurring series
New events.tasks.send_deadline_reminders (daily beat schedule, registered
in features.jobs.JOB_REGISTRY for the control panel's Jobs tab), the gap
notify_new_event's own docstring flagged: a recurring series' occurrences
never go through that on-creation path (they're bulk-generated by
extend_event_series, and notifying per-occurrence there would flood
everyone), so they never got any "you need to answer this" nudge at all.

This sweeps every upcoming event once, one week before whichever cutoff
matters -- the event's own answer deadline, or its start when none is
set -- and notifies whoever's still NO_RESPONSE, using the same
notify_members() -> Notification -> push chain notify_new_event already
uses. New Event.deadline_reminder_sent_at makes it idempotent: each
event's window opens once, is marked processed regardless of whether
anyone needed notifying, and a missed run still catches anything whose
window hasn't fully closed by the time the job next runs.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ECGMEwrc2k4D8VQuwjstj9
2026-08-21 17:33:33 +02:00

116 lines
5.6 KiB
Python

"""Celery tasks for events -- the `extend-event-series` beat schedule entry
(see rosterchief/settings.CELERY_BEAT_SCHEDULE and features/jobs.py), and
notifying members when a new event needs their RSVP.
"""
import datetime
from celery import shared_task
from django.utils import timezone
from django.utils.translation import gettext as _
from events.models import Attendance, Event, EventSeries
from events.services import generate_occurrences, horizon
from features.models import Maintenance
from members.models import Member
from notifications.services import notify_members
#: How long before an event's answer deadline (or, when it has none, its own
#: start) send_deadline_reminders below nudges whoever still hasn't answered.
DEADLINE_REMINDER_LEAD_TIME = datetime.timedelta(days=7)
@shared_task(name="events.tasks.extend_event_series")
def extend_event_series():
if Maintenance.is_on():
# Loud, not silent: a job that quietly skips itself while the platform is closed is
# how a rolling horizon quietly runs dry. Raising here is what turns it into a
# Failure on the control panel's Jobs tab instead of nothing happening at all.
raise RuntimeError("Platform is in maintenance mode; this job stood down.")
until = horizon()
total = 0
for series in EventSeries.objects.all():
total += len(generate_occurrences(series, until))
return f"Generated {total} occurrence(s) across {EventSeries.objects.count()} series."
@shared_task(name="events.tasks.notify_new_event")
def notify_new_event(event_id):
"""Scheduled from management.views.EventCreateView.form_valid -- a staff
member deliberately planning one new event, not every Event row that
happens to get created (a recurring series' rolling-horizon extension
via extend_event_series above, or a bulk fixture import, would otherwise
flood everyone with one push per occurrence; those are intentionally not
wired to this).
Notifies only whoever is still NO_RESPONSE -- right after creation every
invited member starts there (events.services.attendance.sync_event_attendances
just ran via the Event post_save signal), so this is exactly "everyone
who needs to respond", not the full invited list.
"""
event = Event.objects.filter(pk=event_id, cancelled=False).select_related("club").first()
if event is None:
return "Skipped: event no longer exists or was cancelled."
member_ids = Attendance.objects.filter(event=event, status=Attendance.AttendanceStatus.NO_RESPONSE).values_list("member_id", flat=True)
members = Member.objects.filter(id__in=member_ids)
if not members:
return "Skipped: no one to notify."
when = timezone.localtime(event.start).strftime("%a %d %b, %H:%M")
body = _("New %(kind)s: %(when)s. Let us know if you can make it.") % {"kind": event.get_kind_display(), "when": when}
notifications = notify_members(members, club=event.club, title=event.title, body=body, source=event)
return f"Notified {len(notifications)} member(s)."
@shared_task(name="events.tasks.send_deadline_reminders")
def send_deadline_reminders():
"""One reminder push per event, DEADLINE_REMINDER_LEAD_TIME before whichever
cutoff matters -- the event's own answer deadline, or its start when no
deadline is set -- to whoever still hasn't answered.
Unlike notify_new_event above (fired once, on-demand, from a staff
member manually planning a single event), this is the periodic sweep
that also catches a recurring series' occurrences, which never go
through that on-creation path at all -- bulk-generating a season's
worth of practices in one go and notifying everyone about each
individually would be exactly the flood notify_new_event's own
docstring says to avoid. Idempotent via Event.deadline_reminder_sent_at:
safe to run as often as CELERY_BEAT_SCHEDULE likes without
double-notifying, and if a run is ever missed, the next one still
catches anything whose window hasn't fully closed yet.
"""
if Maintenance.is_on():
raise RuntimeError("Platform is in maintenance mode; this job stood down.")
now = timezone.now()
events_reminded = 0
members_notified = 0
candidates = Event.objects.filter(cancelled=False, deadline_reminder_sent_at__isnull=True, start__gt=now).select_related("club")
for event in candidates:
cutoff = event.deadline or event.start
reminder_at = cutoff - DEADLINE_REMINDER_LEAD_TIME
if not (reminder_at <= now < cutoff):
continue
member_ids = Attendance.objects.filter(event=event, status=Attendance.AttendanceStatus.NO_RESPONSE).values_list("member_id", flat=True)
members = Member.objects.filter(id__in=member_ids)
if members:
when = timezone.localtime(event.start).strftime("%a %d %b, %H:%M")
body = _("Reminder: %(kind)s on %(when)s still needs your answer.") % {"kind": event.get_kind_display(), "when": when}
notify_members(members, club=event.club, title=event.title, body=body, source=event)
members_notified += len(members)
# Marked processed even when nobody was NO_RESPONSE at the time -- the
# window only opens once per event, not "keep checking until someone
# answers" (that would just mean it fires the moment they stop being
# NO_RESPONSE for an unrelated reason, e.g. answering after the window).
event.deadline_reminder_sent_at = now
event.save(update_fields=["deadline_reminder_sent_at", "modified"])
events_reminded += 1
return f"Reminded {members_notified} member(s) across {events_reminded} event(s)."