From be77e5f05dcdd2d5ee8a8db465a64823356fa632 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 4 Jun 2022 19:30:07 +0200 Subject: [PATCH] Make remind voter job useful --- cmd/boardvoting/jobs.go | 43 ++++++++++++++++++++++++++++++-------- internal/models/motions.go | 6 +++--- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/cmd/boardvoting/jobs.go b/cmd/boardvoting/jobs.go index f3c0b08..26461f8 100644 --- a/cmd/boardvoting/jobs.go +++ b/cmd/boardvoting/jobs.go @@ -40,19 +40,44 @@ type RemindVotersJob struct { } func (r *RemindVotersJob) Schedule() { - // TODO: check logic. It would make more sense to remind at a specific interval before the next pending - // decision is closed const reminderDays = 3 - year, month, day := time.Now().UTC().Date() + now := time.Now().UTC() - nextExecution := time.Date( - year, month, day, 0, 0, 0, 0, time.UTC, - ).AddDate(0, 0, reminderDays) + year, month, day := now.Date() - r.infoLog.Printf("scheduling RemindVotersJob for %s", nextExecution) + nextPotentialRun := time.Date(year, month, day+1, 0, 0, 0, 0, time.UTC) + nextPotentialRun.Add(hoursInDay * time.Hour) - when := time.Until(nextExecution) + relevantDue := nextPotentialRun.Add(reminderDays * hoursInDay * time.Hour) + + due, err := r.decisions.NextPendingDue(context.Background(), relevantDue) + if err != nil { + r.errorLog.Printf("could not fetch next due date: %v", err) + } + + if due == nil { + r.infoLog.Printf("no due motions after relevant due date %s, not scheduling ReminderJob", relevantDue) + + return + } + + remindNext := due.Add(-reminderDays * hoursInDay * time.Hour).UTC() + + year, month, day = remindNext.Date() + + potentialRun := time.Date(year, month, day, 0, 0, 0, 0, time.UTC) + + if potentialRun.Before(time.Now().UTC()) { + + r.infoLog.Printf("potential reminder time %s is in the past, not scheduling ReminderJob", potentialRun) + + return + } + + r.infoLog.Printf("scheduling RemindVotersJob for %s", potentialRun) + + when := time.Until(potentialRun) if r.timer != nil { r.timer.Reset(when) @@ -133,7 +158,7 @@ func (c *CloseDecisionsJob) Schedule() { ctx := context.Background() - nextDue, err = c.decisions.NextPendingDue(ctx) + nextDue, err = c.decisions.NextPendingDue(ctx, time.Now().UTC()) if err != nil { c.errorLog.Printf("could not get next pending due date") diff --git a/internal/models/motions.go b/internal/models/motions.go index 524b2ad..0d269fc 100644 --- a/internal/models/motions.go +++ b/internal/models/motions.go @@ -477,11 +477,11 @@ func sumsForDecision(ctx context.Context, tx *sqlx.Tx, d *Motion) (*VoteSums, er return sums, nil } -func (m *MotionModel) NextPendingDue(ctx context.Context) (*time.Time, error) { +func (m *MotionModel) NextPendingDue(ctx context.Context, relativeTo time.Time) (*time.Time, error) { row := m.DB.QueryRowContext( ctx, - `SELECT due FROM decisions WHERE status=0 ORDER BY due LIMIT 1`, - nil, + `SELECT due FROM decisions WHERE status=0 AND due >= ? ORDER BY due LIMIT 1`, + relativeTo.UTC(), ) if row == nil {