Make remind voter job useful

This commit is contained in:
Jan Dittberner 2022-06-04 19:30:07 +02:00
parent f966cbd62f
commit be77e5f05d
2 changed files with 37 additions and 12 deletions

View file

@ -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")

View file

@ -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 {