Claim goal notifications before sending them
Test and publish / test (push) Successful in 17s
Test and publish / publish (push) Failing after 23s

The check read "already sent?" and recorded the send afterwards, so two
overlapping checks could both notify. Claim the (goal, day) row first
with ON CONFLICT DO NOTHING and release it if ntfy fails, so a later
check retries. The handler now returns a typed result instead of a
map[string]any, and shares one ntfy client. Fix two comments that still
said nginx.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-26 15:15:31 +00:00
co-authored by Claude Opus 5.5
parent c85e6dfbb0
commit b20ea0adf9
3 changed files with 67 additions and 50 deletions
+12 -9
View File
@@ -574,17 +574,20 @@ func (s *Store) UpdateNotificationSettings(ctx context.Context, settings Notific
return settings, err
}
func (s *Store) GoalNotificationSent(ctx context.Context, goalID int64, date time.Time) (bool, error) {
var sent bool
err := s.pool.QueryRow(ctx, `SELECT EXISTS (
SELECT 1 FROM goal_notifications WHERE goal_id = $1 AND notification_date = $2::date
)`, goalID, date.Format(time.DateOnly)).Scan(&sent)
return sent, err
// ClaimGoalNotification records today's notification for a goal before it is
// sent, so concurrent checks send it at most once. It reports whether this
// call made the claim.
func (s *Store) ClaimGoalNotification(ctx context.Context, goalID int64, date time.Time) (bool, error) {
tag, err := s.pool.Exec(ctx, `INSERT INTO goal_notifications (goal_id, notification_date)
VALUES ($1, $2::date) ON CONFLICT (goal_id, notification_date) DO NOTHING`, goalID, date.Format(time.DateOnly))
return tag.RowsAffected() == 1, err
}
func (s *Store) RecordGoalNotification(ctx context.Context, goalID int64, date time.Time) error {
_, err := s.pool.Exec(ctx, `INSERT INTO goal_notifications (goal_id, notification_date)
VALUES ($1, $2::date) ON CONFLICT (goal_id, notification_date) DO NOTHING`, goalID, date.Format(time.DateOnly))
// ReleaseGoalNotification undoes a claim whose send failed, so a later check
// retries it.
func (s *Store) ReleaseGoalNotification(ctx context.Context, goalID int64, date time.Time) error {
_, err := s.pool.Exec(ctx, `DELETE FROM goal_notifications
WHERE goal_id = $1 AND notification_date = $2::date`, goalID, date.Format(time.DateOnly))
return err
}