diff --git a/internal/handler/booking_handler.go b/internal/handler/booking_handler.go index f1ebed6f..351efb69 100644 --- a/internal/handler/booking_handler.go +++ b/internal/handler/booking_handler.go @@ -13,6 +13,7 @@ import ( "net/url" "strconv" "strings" + "sync" "time" "github.com/calnode/calnode/internal/booking" @@ -1100,7 +1101,7 @@ func (h *Handler) hostBookingData(ctx context.Context, base mailer.BookingData, // manual link. LiveKit always mints an instance-hosted room (no per-host connection). Zoom and // LiveKit resolve here and mutate b/bData in place; a Meet/Teams link is instead captured // later from the calendar event Google/Microsoft actually creates (see -// createHostEventsAndNotify), since only the calendar API call itself produces it. +// createHostEvents), since only the calendar API call itself produces it. func (h *Handler) mintMeetingLink(ctx context.Context, b *booking.Booking, in bookingConfirmationInput, bData *mailer.BookingData, hosts []assignedHost) (meetURL string, autoGenMeet bool, livekitHostURL string) { gc := h.getCal() if in.LocationType == "phone" { @@ -1218,75 +1219,106 @@ func hostEventLocation(meetURL, livekitHostURL, attendeeLocationValue string) st return meetURL } -// createHostEventsAndNotify creates a calendar event on each assigned host's connected -// calendar (Group bookings put several hosts on the meeting; round-robin/Normal has one) and -// emails each host that has host-booking notifications on. For the primary host, a -// successfully-created Google Meet/Teams link is captured into meetURL/bData.LocationValue — -// the only place such a link becomes available, since minting it is a side effect of the -// calendar API call itself (see mintMeetingLink's doc comment). Returns the primary host's -// notification prefs, for the caller's single attendee-confirmation send. -func (h *Handler) createHostEventsAndNotify(ctx context.Context, b *booking.Booking, in bookingConfirmationInput, bData *mailer.BookingData, hosts []assignedHost, meetURL string, autoGenMeet bool, livekitHostURL string) (hostPrefs, bool) { +// createHostEvents creates a calendar event on each assigned host's connected calendar +// (Group bookings put several hosts on the meeting; round-robin/Normal has one) and +// records each event's id. The primary goes first: a Google Meet/Teams link is minted as +// a side effect of the primary's calendar API call (see mintMeetingLink's doc comment), +// and it is captured into bData.LocationValue and the booking row so every other host's +// event, and the emails sent afterwards, carry it. The remaining hosts' events are then +// written concurrently - one after another, a collective's last hosts could run out of +// dispatch budget before their turn and be left without an event until the reconciler. +func (h *Handler) createHostEvents(ctx context.Context, b *booking.Booking, in bookingConfirmationInput, bData *mailer.BookingData, hosts []assignedHost, meetURL string, autoGenMeet bool, livekitHostURL string) { gc := h.getCal() - primaryPrefs := allOnPrefs - confirmFailed := false + if gc == nil { + return + } + create := func(host assignedHost, location string) (link string) { + eventID, link, calID, provider, err := gc.CreateEvent(ctx, host.UserID, calendar.CreateEventParams{ + // The attendee is added as a calendar invitee below, so this text reaches + // them via the provider's own native invite (Google/Outlook/CalDAV) — + // follows their locale like the confirmation email, not English/host-fixed. + Summary: bData.Tf("calendar_event_summary", in.EventTypeName, in.OrganizerName), + Description: bData.Tf("calendar_event_booking_id", b.ID), + Location: location, + Start: b.StartAt, + End: b.EndAt, + OrganizerName: in.OrganizerName, + OrganizerEmail: calendarInvitee(b.InviteDelivery, in.OrganizerEmail), + AddMeet: autoGenMeet && host.IsPrimary, + }) + if err != nil { + h.logger.Error("create gcal event", "error", err, "booking_id", b.ID, "host", host.UserID) + h.nudgeCalendarReconcile() // heal this missing event on a later sweep + return "" + } + if eventID == "" { + return "" + } + // Record WHICH calendar too. Reschedule and cancel use it instead of + // re-resolving the host's destination, so changing that later cannot orphan + // this event (see migration 00055). + if _, err := h.db.ExecContext(ctx, + `UPDATE booking_hosts SET external_event_id = ?, external_calendar_id = ?, + external_provider = ? + WHERE booking_id = ? AND user_id = ?`, + eventID, calID, provider, b.ID, host.UserID); err != nil { + h.logger.Error("save host gcal event id", "error", err, "booking_id", b.ID) + } + if host.IsPrimary { + if _, err := h.db.ExecContext(ctx, + `UPDATE bookings SET external_event_id = ? WHERE id = ?`, eventID, b.ID); err != nil { + h.logger.Error("save gcal event id", "error", err, "booking_id", b.ID) + } + } + return link + } + for _, host := range hosts { - // Create a calendar event on each host's connected calendar and record - // the per-host event ID so it can be cancelled later. The primary's id - // also lives on the booking row for back-compat. - if gc != nil { - eventID, link, calID, provider, err := gc.CreateEvent(ctx, host.UserID, calendar.CreateEventParams{ - // The attendee is added as a calendar invitee below, so this text reaches - // them via the provider's own native invite (Google/Outlook/CalDAV) — - // follows their locale like the confirmation email, not English/host-fixed. - Summary: bData.Tf("calendar_event_summary", in.EventTypeName, in.OrganizerName), - Description: bData.Tf("calendar_event_booking_id", b.ID), - Location: hostEventLocation(meetURL, livekitHostURL, bData.LocationValue), // empty until the primary creates it; secondary hosts get the link - Start: b.StartAt, - End: b.EndAt, - OrganizerName: in.OrganizerName, - OrganizerEmail: calendarInvitee(b.InviteDelivery, in.OrganizerEmail), - AddMeet: autoGenMeet && host.IsPrimary, - }) - if err != nil { - h.logger.Error("create gcal event", "error", err, "booking_id", b.ID, "host", host.UserID) - h.nudgeCalendarReconcile() // heal this missing event on a later sweep - } else if eventID != "" { - // Record WHICH calendar too. Reschedule and cancel use it instead of - // re-resolving the host's destination, so changing that later cannot orphan - // this event (see migration 00055). - if _, err := h.db.ExecContext(ctx, - `UPDATE booking_hosts SET external_event_id = ?, external_calendar_id = ?, - external_provider = ? - WHERE booking_id = ? AND user_id = ?`, - eventID, calID, provider, b.ID, host.UserID); err != nil { - h.logger.Error("save host gcal event id", "error", err, "booking_id", b.ID) - } - if host.IsPrimary { - if _, err := h.db.ExecContext(ctx, - `UPDATE bookings SET external_event_id = ? WHERE id = ?`, eventID, b.ID); err != nil { - h.logger.Error("save gcal event id", "error", err, "booking_id", b.ID) - } - // Persist the generated Meet link so the confirmation emails (sent - // below), the booking record, and the manage page show it. bData - // feeds the emails; updating it here reaches every send that follows. - if link != "" { - meetURL = link - bData.LocationValue = link - if _, err := h.db.ExecContext(ctx, - `UPDATE bookings SET location_value = ? WHERE id = ?`, link, b.ID); err != nil { - h.logger.Error("save meet link", "error", err, "booking_id", b.ID) - } - } - } + if !host.IsPrimary { + continue + } + // meetURL is empty until the primary's event auto-generates it. + if link := create(host, hostEventLocation(meetURL, livekitHostURL, bData.LocationValue)); link != "" { + // Persist the generated Meet link so the confirmation emails (sent after + // this), the booking record, and the manage page show it. bData feeds the + // emails; updating it here reaches every send that follows. + meetURL = link + bData.LocationValue = link + if _, err := h.db.ExecContext(ctx, + `UPDATE bookings SET location_value = ? WHERE id = ?`, link, b.ID); err != nil { + h.logger.Error("save meet link", "error", err, "booking_id", b.ID) } } + break + } + location := hostEventLocation(meetURL, livekitHostURL, bData.LocationValue) + var wg sync.WaitGroup + for _, host := range hosts { + if host.IsPrimary { + continue + } + wg.Add(1) + go func(host assignedHost) { + defer wg.Done() + create(host, location) + }(host) + } + wg.Wait() +} +// notifyHosts emails each host that has host-booking notifications on. Returns the +// primary host's notification prefs, for the caller's single attendee-confirmation send, +// and whether any host email failed. +func (h *Handler) notifyHosts(ctx context.Context, b *booking.Booking, bData mailer.BookingData, hosts []assignedHost, livekitHostURL string) (hostPrefs, bool) { + primaryPrefs := allOnPrefs + confirmFailed := false + for _, host := range hosts { prefs := h.hostPrefsOrDefault(ctx, b.ID, host.UserID) if host.IsPrimary { primaryPrefs = prefs } if prefs.NotifyHostBooking { - hd := h.hostBookingData(ctx, *bData, host, b.InviteDelivery, b.UpdatedAt) + hd := h.hostBookingData(ctx, bData, host, b.InviteDelivery, b.UpdatedAt) if livekitHostURL != "" { hd.LocationValue = livekitHostURL // host email gets the controls-enabled link } @@ -1323,15 +1355,31 @@ func sendWithRetry(ctx context.Context, logger *slog.Logger, bookingID, who stri } } +// dispatchCalendarBudget bounds the calendar phase of dispatchBookingConfirmation (link +// minting plus every host's event); dispatchMailBudget bounds the email phase. They are +// separate so that a stalled SMTP server (its dial alone may take defaultSMTPTimeout, +// 30s, per attempt) can no longer starve the hosts' calendar events, the webhook or the +// reminders, which all used to share one 30s deadline and ran after the emails. +var ( + dispatchCalendarBudget = 30 * time.Second + dispatchMailBudget = 2 * time.Minute +) + // dispatchBookingConfirmation runs the post-create side effects for a booking: -// calendar events on each assigned host's connected calendar (auto-generating a -// Meet/Teams link only when the host's provider natively matches the platform), -// confirmation emails to attendee and hosts, the booking.created webhook, and -// reminder scheduling. Intended to run in its own goroutine; every failure is logged, +// reminder scheduling, calendar events on each assigned host's connected calendar +// (auto-generating a Meet/Teams link only when the host's provider natively matches the +// platform), the booking.created webhook, and confirmation emails to attendee and hosts. +// The DB-only, durable steps run before the slow external ones so they cannot be lost to +// a provider or SMTP stall. Intended to run in its own goroutine; every failure is logged, // never fatal. Shared by the REST CreateBooking handler and the MCP create_booking tool. func (h *Handler) dispatchBookingConfirmation(b *booking.Booking, in bookingConfirmationInput) { - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), dispatchCalendarBudget) defer cancel() + // Reminder jobs are durable and need nothing from the calendar phase; queue them + // before anything that can stall. + if err := h.enqueueBookingReminders(ctx, b.EventTypeID, b.ID, b.StartAt); err != nil { + h.logger.Error("enqueue reminders", "error", err, "booking_id", b.ID) + } bData := mailer.BookingData{ BookingID: b.ID, EventTypeName: in.EventTypeName, @@ -1372,38 +1420,18 @@ func (h *Handler) dispatchBookingConfirmation(b *booking.Booking, in bookingConf } meetURL, autoGenMeet, livekitHostURL := h.mintMeetingLink(ctx, b, in, &bData, hosts) - primaryPrefs, hostFailed := h.createHostEventsAndNotify(ctx, b, in, &bData, hosts, meetURL, autoGenMeet, livekitHostURL) - - // Attendee confirmation, once. "With:" names the primary host; gated on the - // primary host's notification preference (matches prior behaviour). - bData.HostName, bData.HostEmail = primaryHost(hosts).Name, primaryHost(hosts).Email - h.applyInviteDelivery(ctx, &bData, b.InviteDelivery, b.HostID) - bData.ICSSequence = int(b.UpdatedAt.Unix()) - confirmFailed := hostFailed - if primaryPrefs.NotifyConfirmation { - if err := sendWithRetry(ctx, h.logger, b.ID, "attendee", func() error { - return mailer.SendConfirmationToAttendee(ctx, h.mailer, bData) - }); err != nil { - confirmFailed = true - } - } - if confirmFailed { - // Operator-visible: the booking happened, but at least one confirmation - // never arrived. Surfaced on the booking JSON for follow-up. - if _, err := h.db.ExecContext(ctx, - `UPDATE bookings SET confirm_failed = 1 WHERE id = ?`, b.ID); err != nil { - h.logger.Error("booking confirmation: flag failure", "error", err, "booking_id", b.ID) - } - } + h.createHostEvents(ctx, b, in, &bData, hosts, meetURL, autoGenMeet, livekitHostURL) + // After the calendar phase, so the payload carries a minted Meet/Teams link. Not + // bound to ctx: the calendar phase may have spent it, and this is one local insert. if h.webhookSvc != nil { - if err := h.webhookSvc.Enqueue(ctx, "booking.created", webhook.BookingPayload{ + if err := h.webhookSvc.Enqueue(context.WithoutCancel(ctx), "booking.created", webhook.BookingPayload{ ID: b.ID, EventTypeSlug: in.EventTypeSlug, HostID: b.HostID, StartAt: b.StartAt.UTC().Format(time.RFC3339), EndAt: b.EndAt.UTC().Format(time.RFC3339), Status: b.Status, - LocationValue: b.LocationValue, + LocationValue: bData.LocationValue, CreatedAt: b.CreatedAt.UTC().Format(time.RFC3339), PaymentStatus: paymentStatusForWebhook(b.PaymentStatus), AmountPaidCents: b.AmountPaidCents, @@ -1412,8 +1440,31 @@ func (h *Handler) dispatchBookingConfirmation(b *booking.Booking, in bookingConf h.logger.Error("enqueue booking.created webhook", "error", err, "booking_id", b.ID) } } - if err := h.enqueueBookingReminders(ctx, b.EventTypeID, b.ID, b.StartAt); err != nil { - h.logger.Error("enqueue reminders", "error", err, "booking_id", b.ID) + + mailCtx, cancelMail := context.WithTimeout(context.Background(), dispatchMailBudget) + defer cancelMail() + primaryPrefs, confirmFailed := h.notifyHosts(mailCtx, b, bData, hosts, livekitHostURL) + + // Attendee confirmation, once. "With:" names the primary host; gated on the + // primary host's notification preference (matches prior behaviour). + bData.HostName, bData.HostEmail = primaryHost(hosts).Name, primaryHost(hosts).Email + h.applyInviteDelivery(mailCtx, &bData, b.InviteDelivery, b.HostID) + bData.ICSSequence = int(b.UpdatedAt.Unix()) + if primaryPrefs.NotifyConfirmation { + if err := sendWithRetry(mailCtx, h.logger, b.ID, "attendee", func() error { + return mailer.SendConfirmationToAttendee(mailCtx, h.mailer, bData) + }); err != nil { + confirmFailed = true + } + } + if confirmFailed { + // Operator-visible: the booking happened, but at least one confirmation + // never arrived. Surfaced on the booking JSON for follow-up. Not mailCtx: the + // sends that failed may have used it up. + if _, err := h.db.ExecContext(context.WithoutCancel(mailCtx), + `UPDATE bookings SET confirm_failed = 1 WHERE id = ?`, b.ID); err != nil { + h.logger.Error("booking confirmation: flag failure", "error", err, "booking_id", b.ID) + } } } diff --git a/internal/handler/dispatch_budget_internal_test.go b/internal/handler/dispatch_budget_internal_test.go new file mode 100644 index 00000000..b5e4591a --- /dev/null +++ b/internal/handler/dispatch_budget_internal_test.go @@ -0,0 +1,87 @@ +package handler + +import ( + "context" + "testing" + "time" + + "github.com/calnode/calnode/internal/calendar" + "github.com/calnode/calnode/internal/mailer" +) + +// stallMailer models an SMTP server that drops packets: every send hangs until its +// context gives up. +type stallMailer struct{} + +func (stallMailer) Send(ctx context.Context, _ mailer.Message) error { + <-ctx.Done() + return ctx.Err() +} + +// ctxProvider is inviteProvider honouring its context, as the real HTTP providers do. +type ctxProvider struct{ *inviteProvider } + +func (p ctxProvider) CreateEvent(ctx context.Context, userID string, params calendar.CreateEventParams) (string, string, string, error) { + if err := ctx.Err(); err != nil { + return "", "", "", err + } + return p.inviteProvider.CreateEvent(ctx, userID, params) +} + +// A stalled mail server used to spend the single dispatch deadline on the primary host's +// email, so the next host's calendar event, the reminders and the webhook - all queued +// after it - were silently dropped. Every host's event and the reminder must now land +// while mail is still hanging, and the lost confirmation is flagged on the booking. +func TestDispatch_stalledMailDoesNotStarveCalendarOrReminders(t *testing.T) { + origMail := dispatchMailBudget + dispatchMailBudget = 300 * time.Millisecond + t.Cleanup(func() { dispatchMailBudget = origMail }) + + f := newInviteFixture(t, false) + f.h.SetMailer(stallMailer{}, "https://book.team.example") + svc := calendar.NewService(f.h.db) + svc.Register(ctxProvider{f.cal}) + f.h.SetCalendar(svc) + + if rec := f.createEventType(t, "intro", "calendar"); rec.Code != 201 { + t.Fatalf("create event type: %d — %s", rec.Code, rec.Body.String()) + } + mustExec := func(q string, args ...any) { + t.Helper() + if _, err := f.h.db.Exec(q, args...); err != nil { + t.Fatalf("%s: %v", q, err) + } + } + mustExec(`UPDATE event_types SET routing_mode = 'collective' WHERE slug = 'intro'`) + mustExec(`INSERT INTO users (id, email, name, iana_timezone) VALUES ('host-2', 'second@host.example', 'Second Host', 'UTC')`) + for day := 0; day < 7; day++ { + mustExec(`INSERT INTO availability_rules (id, user_id, day_of_week, start_time, end_time) VALUES (?, 'host-2', ?, '00:00', '23:59')`, + "rule-h2-"+string(rune('0'+day)), day) + } + mustExec(`INSERT INTO calendar_connections + (id, user_id, provider, access_token_enc, calendar_id, check_conflicts, is_destination, created_at) + VALUES ('conn-2', 'host-2', 'google', 'e', 'primary', 1, 1, '2026-01-01T00:00:00Z')`) + mustExec(`INSERT INTO event_type_hosts (id, event_type_id, user_id, role, priority) + SELECT 'seat-2', id, 'host-2', 'required', 1 FROM event_types WHERE slug = 'intro'`) + + start := time.Now().UTC().Add(72 * time.Hour).Truncate(24 * time.Hour).Add(10 * time.Hour) + req := `{"event_type_slug":"intro","start_at":"` + start.Format(time.RFC3339) + `","name":"Guest","email":"` + inviteBookerEmail + `","timezone":"UTC"}` + rec := f.do(t, f.h.CreateBooking, "POST", "/v1/bookings", req) + if rec.Code != 201 { + t.Fatalf("create booking: %d — %s", rec.Code, rec.Body.String()) + } + + var events, reminders, flagged int + deadline := time.Now().Add(5 * time.Second) + for time.Now().Before(deadline) { + _ = f.h.db.QueryRow(`SELECT COUNT(*) FROM booking_hosts WHERE COALESCE(external_event_id, '') != ''`).Scan(&events) + _ = f.h.db.QueryRow(`SELECT COUNT(*) FROM jobs WHERE type = 'reminder.send'`).Scan(&reminders) + _ = f.h.db.QueryRow(`SELECT COUNT(*) FROM bookings WHERE confirm_failed = 1`).Scan(&flagged) + if events == 2 && reminders > 0 && flagged == 1 { + return + } + time.Sleep(20 * time.Millisecond) + } + t.Fatalf("host events = %d (want 2), reminder jobs = %d (want > 0), confirm_failed bookings = %d (want 1)", + events, reminders, flagged) +}