2022-05-15 18:10:49 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017-2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-21 12:27:46 +00:00
|
|
|
"context"
|
2022-05-15 18:10:49 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.cacert.org/cacert-boardvoting/internal/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Job interface {
|
|
|
|
Schedule()
|
|
|
|
Run()
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemindVotersJob struct {
|
|
|
|
infoLog, errorLog *log.Logger
|
|
|
|
timer *time.Timer
|
2022-05-26 13:27:25 +00:00
|
|
|
voters *models.UserModel
|
2022-05-21 17:18:17 +00:00
|
|
|
decisions *models.MotionModel
|
2022-05-15 18:10:49 +00:00
|
|
|
notify chan NotificationMail
|
|
|
|
reschedule chan Job
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemindVotersJob) Schedule() {
|
|
|
|
const reminderDays = 3
|
|
|
|
|
|
|
|
year, month, day := time.Now().UTC().Date()
|
|
|
|
|
|
|
|
nextExecution := time.Date(
|
|
|
|
year, month, day, 0, 0, 0, 0, time.UTC,
|
|
|
|
).AddDate(0, 0, reminderDays)
|
|
|
|
|
|
|
|
r.infoLog.Printf("scheduling RemindVotersJob for %s", nextExecution)
|
|
|
|
|
|
|
|
when := time.Until(nextExecution)
|
|
|
|
|
|
|
|
if r.timer != nil {
|
|
|
|
r.timer.Reset(when)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.timer = time.AfterFunc(when, r.Run)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemindVotersJob) Run() {
|
|
|
|
r.infoLog.Print("running RemindVotersJob")
|
|
|
|
|
|
|
|
defer func(r *RemindVotersJob) { r.reschedule <- r }(r)
|
|
|
|
|
|
|
|
var (
|
2022-05-26 13:27:25 +00:00
|
|
|
voters []*models.User
|
2022-05-22 19:15:54 +00:00
|
|
|
decisions []*models.Motion
|
2022-05-15 18:10:49 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-05-21 12:27:46 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
voters, err = r.voters.GetReminderVoters(ctx)
|
2022-05-15 18:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
r.errorLog.Printf("problem getting voters: %v", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, voter := range voters {
|
2022-05-21 12:09:19 +00:00
|
|
|
v := voter
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
decisions, err = r.decisions.UnVotedDecisionsForVoter(ctx, v)
|
2022-05-15 18:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
r.errorLog.Printf("problem getting unvoted decisions: %v", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(decisions) > 0 {
|
|
|
|
r.notify <- &RemindVoterNotification{voter: voter, decisions: decisions}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RemindVotersJob) Stop() {
|
|
|
|
if r.timer != nil {
|
|
|
|
r.timer.Stop()
|
|
|
|
|
|
|
|
r.timer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) NewRemindVotersJob(
|
|
|
|
rescheduleChannel chan Job,
|
|
|
|
) Job {
|
|
|
|
return &RemindVotersJob{
|
|
|
|
infoLog: app.infoLog,
|
|
|
|
errorLog: app.errorLog,
|
2022-05-26 13:27:25 +00:00
|
|
|
voters: app.users,
|
2022-05-21 17:18:17 +00:00
|
|
|
decisions: app.motions,
|
2022-05-15 18:10:49 +00:00
|
|
|
reschedule: rescheduleChannel,
|
2022-05-21 11:51:17 +00:00
|
|
|
notify: app.mailNotifier.notifyChannel,
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 12:09:19 +00:00
|
|
|
type CloseDecisionsJob struct {
|
2022-05-21 11:51:17 +00:00
|
|
|
timer *time.Timer
|
|
|
|
infoLog *log.Logger
|
|
|
|
errorLog *log.Logger
|
2022-05-21 17:18:17 +00:00
|
|
|
decisions *models.MotionModel
|
2022-05-21 11:51:17 +00:00
|
|
|
reschedule chan Job
|
|
|
|
notify chan NotificationMail
|
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
|
2022-05-21 12:09:19 +00:00
|
|
|
func (c *CloseDecisionsJob) Schedule() {
|
2022-05-21 11:51:17 +00:00
|
|
|
var (
|
|
|
|
nextDue *time.Time
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2022-05-21 12:27:46 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
nextDue, err = c.decisions.NextPendingDecisionDue(ctx)
|
2022-05-21 11:51:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.errorLog.Printf("could not get next pending due date")
|
|
|
|
|
|
|
|
c.Stop()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if nextDue == nil {
|
|
|
|
c.infoLog.Printf("no next planned execution of CloseDecisionsJob")
|
|
|
|
c.Stop()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.infoLog.Printf("scheduling CloseDecisionsJob for %s", nextDue)
|
|
|
|
when := time.Until(nextDue.Add(time.Second))
|
|
|
|
|
|
|
|
if c.timer == nil {
|
|
|
|
c.timer = time.AfterFunc(when, c.Run)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.timer.Reset(when)
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 12:09:19 +00:00
|
|
|
func (c *CloseDecisionsJob) Run() {
|
2022-05-21 11:51:17 +00:00
|
|
|
c.infoLog.Printf("running CloseDecisionsJob")
|
|
|
|
|
2022-05-21 12:27:46 +00:00
|
|
|
results, err := c.decisions.CloseDecisions(context.Background())
|
2022-05-21 11:51:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.errorLog.Printf("closing decisions failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, res := range results {
|
2022-05-27 15:39:54 +00:00
|
|
|
c.notify <- &ClosedDecisionNotification{Decision: res}
|
2022-05-21 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.reschedule <- c
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 12:09:19 +00:00
|
|
|
func (c *CloseDecisionsJob) Stop() {
|
2022-05-21 11:51:17 +00:00
|
|
|
if c.timer != nil {
|
|
|
|
c.timer.Stop()
|
|
|
|
c.timer = nil
|
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 11:51:17 +00:00
|
|
|
func (app *application) NewCloseDecisionsJob(
|
|
|
|
rescheduleChannel chan Job,
|
|
|
|
) Job {
|
2022-05-21 12:09:19 +00:00
|
|
|
return &CloseDecisionsJob{
|
2022-05-21 11:51:17 +00:00
|
|
|
infoLog: app.infoLog,
|
|
|
|
errorLog: app.errorLog,
|
2022-05-21 17:18:17 +00:00
|
|
|
decisions: app.motions,
|
2022-05-21 11:51:17 +00:00
|
|
|
reschedule: rescheduleChannel,
|
|
|
|
notify: app.mailNotifier.notifyChannel,
|
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type JobScheduler struct {
|
|
|
|
infoLogger *log.Logger
|
|
|
|
errorLogger *log.Logger
|
|
|
|
jobs []Job
|
|
|
|
rescheduleChannel chan Job
|
2022-05-21 12:09:19 +00:00
|
|
|
quitChannel chan struct{}
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) NewJobScheduler() {
|
|
|
|
rescheduleChannel := make(chan Job, 1)
|
|
|
|
|
|
|
|
app.jobScheduler = &JobScheduler{
|
|
|
|
infoLogger: app.infoLog,
|
|
|
|
errorLogger: app.errorLog,
|
|
|
|
jobs: make([]Job, 0, 2),
|
|
|
|
rescheduleChannel: rescheduleChannel,
|
2022-05-21 12:09:19 +00:00
|
|
|
quitChannel: make(chan struct{}),
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 11:51:17 +00:00
|
|
|
app.jobScheduler.addJob(app.NewCloseDecisionsJob(rescheduleChannel))
|
|
|
|
app.jobScheduler.addJob(app.NewRemindVotersJob(rescheduleChannel))
|
2022-05-15 18:10:49 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 12:09:19 +00:00
|
|
|
func (js *JobScheduler) Schedule() {
|
2022-05-15 18:10:49 +00:00
|
|
|
for _, job := range js.jobs {
|
|
|
|
job.Schedule()
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case job := <-js.rescheduleChannel:
|
|
|
|
job.Schedule()
|
2022-05-21 12:09:19 +00:00
|
|
|
case <-js.quitChannel:
|
2022-05-15 18:10:49 +00:00
|
|
|
for _, job := range js.jobs {
|
|
|
|
job.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
js.infoLogger.Print("stop job scheduler")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (js *JobScheduler) addJob(job Job) {
|
|
|
|
js.jobs = append(js.jobs, job)
|
|
|
|
}
|
2022-05-21 12:09:19 +00:00
|
|
|
|
|
|
|
func (js *JobScheduler) Quit() {
|
|
|
|
js.quitChannel <- struct{}{}
|
|
|
|
}
|