2019-07-31 15:30:58 +00:00
|
|
|
/*
|
2022-03-21 12:39:00 +00:00
|
|
|
Copyright 2017-2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2019-07-31 15:30:58 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
2022-03-21 12:39:00 +00:00
|
|
|
you may not use this file except in compliance with the License.
|
2021-01-09 14:49:19 +00:00
|
|
|
You may obtain a copy of the License at
|
2019-07-31 15:30:58 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2019-07-31 15:30:58 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
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.
|
2019-07-31 15:30:58 +00:00
|
|
|
*/
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 09:35:33 +00:00
|
|
|
package main
|
|
|
|
|
2019-07-31 12:14:21 +00:00
|
|
|
import (
|
|
|
|
"time"
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2019-07-31 12:14:21 +00:00
|
|
|
)
|
2017-04-20 09:35:33 +00:00
|
|
|
|
|
|
|
type Job interface {
|
|
|
|
Schedule()
|
|
|
|
Stop()
|
|
|
|
Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
type jobIdentifier int
|
|
|
|
|
|
|
|
const (
|
2021-01-09 14:49:19 +00:00
|
|
|
JobIDCloseDecisions jobIdentifier = iota
|
|
|
|
JobIDRemindVotersJob
|
2022-03-21 11:16:24 +00:00
|
|
|
reminderDays = 3
|
2017-04-20 09:35:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var rescheduleChannel = make(chan jobIdentifier, 1)
|
|
|
|
|
|
|
|
func JobScheduler(quitChannel chan int) {
|
|
|
|
var jobs = map[jobIdentifier]Job{
|
2021-01-09 14:49:19 +00:00
|
|
|
JobIDCloseDecisions: NewCloseDecisionsJob(),
|
|
|
|
JobIDRemindVotersJob: NewRemindVotersJob(),
|
2017-04-20 09:35:33 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Info("started job scheduler")
|
2017-04-20 09:35:33 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2021-01-09 14:49:19 +00:00
|
|
|
case jobID := <-rescheduleChannel:
|
|
|
|
job := jobs[jobID]
|
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Infof("reschedule job %s", job)
|
2017-04-20 09:35:33 +00:00
|
|
|
job.Schedule()
|
|
|
|
case <-quitChannel:
|
|
|
|
for _, job := range jobs {
|
|
|
|
job.Stop()
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Info("stop job scheduler")
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 09:35:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CloseDecisionsJob struct {
|
|
|
|
timer *time.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCloseDecisionsJob() *CloseDecisionsJob {
|
|
|
|
job := &CloseDecisionsJob{}
|
|
|
|
job.Schedule()
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 09:35:33 +00:00
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *CloseDecisionsJob) Schedule() {
|
2021-01-09 14:49:19 +00:00
|
|
|
var (
|
|
|
|
nextDue *time.Time
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
nextDue, err = GetNextPendingDecisionDue()
|
2017-04-20 09:35:33 +00:00
|
|
|
if err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Error("Could not get next pending due date")
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 09:35:33 +00:00
|
|
|
if j.timer != nil {
|
|
|
|
j.timer.Stop()
|
|
|
|
j.timer = nil
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 09:35:33 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 09:35:33 +00:00
|
|
|
if nextDue == nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Info("no next planned execution of CloseDecisionsJob")
|
2017-04-20 18:58:22 +00:00
|
|
|
j.Stop()
|
2017-04-20 09:35:33 +00:00
|
|
|
} else {
|
2017-04-22 18:07:39 +00:00
|
|
|
nextDue := nextDue.Add(time.Second)
|
|
|
|
log.Infof("scheduling CloseDecisionsJob for %s", nextDue)
|
2021-01-09 14:49:19 +00:00
|
|
|
when := time.Until(nextDue)
|
2017-04-20 09:35:33 +00:00
|
|
|
if j.timer != nil {
|
|
|
|
j.timer.Reset(when)
|
|
|
|
} else {
|
|
|
|
j.timer = time.AfterFunc(when, j.Run)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *CloseDecisionsJob) Stop() {
|
|
|
|
if j.timer != nil {
|
|
|
|
j.timer.Stop()
|
2017-04-20 18:58:22 +00:00
|
|
|
j.timer = nil
|
2017-04-20 09:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *CloseDecisionsJob) Run() {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Debug("running CloseDecisionsJob")
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 09:35:33 +00:00
|
|
|
err := CloseDecisions()
|
|
|
|
if err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Errorf("closing decisions %v", err)
|
2017-04-20 09:35:33 +00:00
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
rescheduleChannel <- JobIDCloseDecisions
|
2017-04-20 09:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *CloseDecisionsJob) String() string {
|
|
|
|
return "CloseDecisionsJob"
|
|
|
|
}
|
2017-04-20 18:58:22 +00:00
|
|
|
|
|
|
|
type RemindVotersJob struct {
|
|
|
|
timer *time.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemindVotersJob() *RemindVotersJob {
|
|
|
|
job := &RemindVotersJob{}
|
|
|
|
job.Schedule()
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 18:58:22 +00:00
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *RemindVotersJob) Schedule() {
|
|
|
|
year, month, day := time.Now().UTC().Date()
|
2022-03-21 11:16:24 +00:00
|
|
|
nextExecution := time.Date(year, month, day, 0, 0, 0, 0, time.UTC).AddDate(0, 0, reminderDays)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Infof("scheduling RemindVotersJob for %s", nextExecution)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
when := time.Until(nextExecution)
|
|
|
|
|
2017-04-20 18:58:22 +00:00
|
|
|
if j.timer != nil {
|
|
|
|
j.timer.Reset(when)
|
|
|
|
} else {
|
|
|
|
j.timer = time.AfterFunc(when, j.Run)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *RemindVotersJob) Stop() {
|
|
|
|
if j.timer != nil {
|
|
|
|
j.timer.Stop()
|
|
|
|
j.timer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *RemindVotersJob) Run() {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Info("running RemindVotersJob")
|
2017-04-20 18:58:22 +00:00
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
defer func() { rescheduleChannel <- JobIDRemindVotersJob }()
|
|
|
|
|
|
|
|
var (
|
|
|
|
voters []Voter
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
voters, err = GetReminderVoters()
|
2017-04-20 18:58:22 +00:00
|
|
|
if err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Errorf("problem getting voters %v", err)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 18:58:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-09 14:49:19 +00:00
|
|
|
var decisions []Decision
|
|
|
|
|
|
|
|
for i := range voters {
|
|
|
|
decisions, err = FindUnVotedDecisionsForVoter(&voters[i])
|
2017-04-20 18:58:22 +00:00
|
|
|
if err != nil {
|
2017-04-22 18:07:39 +00:00
|
|
|
log.Errorf("problem getting unvoted decisions: %v", err)
|
2021-01-09 14:49:19 +00:00
|
|
|
|
2017-04-20 18:58:22 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 14:49:19 +00:00
|
|
|
|
|
|
|
if len(decisions) > 0 {
|
|
|
|
NotifyMailChannel <- &RemindVoterNotification{voter: voters[i], decisions: decisions}
|
2017-04-20 18:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|