Make remind voter job useful

main
Jan Dittberner 2 years ago
parent f966cbd62f
commit be77e5f05d

@ -40,19 +40,44 @@ type RemindVotersJob struct {
} }
func (r *RemindVotersJob) Schedule() { 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 const reminderDays = 3
year, month, day := time.Now().UTC().Date() now := time.Now().UTC()
nextExecution := time.Date( year, month, day := now.Date()
year, month, day, 0, 0, 0, 0, time.UTC,
).AddDate(0, 0, reminderDays)
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 { if r.timer != nil {
r.timer.Reset(when) r.timer.Reset(when)
@ -133,7 +158,7 @@ func (c *CloseDecisionsJob) Schedule() {
ctx := context.Background() ctx := context.Background()
nextDue, err = c.decisions.NextPendingDue(ctx) nextDue, err = c.decisions.NextPendingDue(ctx, time.Now().UTC())
if err != nil { if err != nil {
c.errorLog.Printf("could not get next pending due date") c.errorLog.Printf("could not get next pending due date")

@ -477,11 +477,11 @@ func sumsForDecision(ctx context.Context, tx *sqlx.Tx, d *Motion) (*VoteSums, er
return sums, nil 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( row := m.DB.QueryRowContext(
ctx, ctx,
`SELECT due FROM decisions WHERE status=0 ORDER BY due LIMIT 1`, `SELECT due FROM decisions WHERE status=0 AND due >= ? ORDER BY due LIMIT 1`,
nil, relativeTo.UTC(),
) )
if row == nil { if row == nil {

Loading…
Cancel
Save