2017-04-20 09:35:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
type Job interface {
|
|
|
|
Schedule()
|
|
|
|
Stop()
|
|
|
|
Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
type jobIdentifier int
|
|
|
|
|
|
|
|
const (
|
|
|
|
JobIdCloseDecisions jobIdentifier = iota
|
2017-04-20 18:58:22 +00:00
|
|
|
JobIdRemindVotersJob
|
2017-04-20 09:35:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var rescheduleChannel = make(chan jobIdentifier, 1)
|
|
|
|
|
|
|
|
func JobScheduler(quitChannel chan int) {
|
|
|
|
var jobs = map[jobIdentifier]Job{
|
2017-04-20 18:58:22 +00:00
|
|
|
JobIdCloseDecisions: NewCloseDecisionsJob(),
|
|
|
|
JobIdRemindVotersJob: NewRemindVotersJob(),
|
2017-04-20 09:35:33 +00:00
|
|
|
}
|
|
|
|
logger.Println("INFO started job scheduler")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case jobId := <-rescheduleChannel:
|
|
|
|
job := jobs[jobId]
|
|
|
|
logger.Println("INFO reschedule job", job)
|
|
|
|
job.Schedule()
|
|
|
|
case <-quitChannel:
|
|
|
|
for _, job := range jobs {
|
|
|
|
job.Stop()
|
|
|
|
}
|
|
|
|
logger.Println("INFO stop job scheduler")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CloseDecisionsJob struct {
|
|
|
|
timer *time.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCloseDecisionsJob() *CloseDecisionsJob {
|
|
|
|
job := &CloseDecisionsJob{}
|
|
|
|
job.Schedule()
|
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *CloseDecisionsJob) Schedule() {
|
|
|
|
var nextDue *time.Time
|
|
|
|
nextDue, err := GetNextPendingDecisionDue()
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("ERROR Could not get next pending due date")
|
|
|
|
if j.timer != nil {
|
|
|
|
j.timer.Stop()
|
|
|
|
j.timer = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if nextDue == nil {
|
2017-04-20 18:58:22 +00:00
|
|
|
logger.Println("INFO no next planned execution of CloseDecisionsJob")
|
|
|
|
j.Stop()
|
2017-04-20 09:35:33 +00:00
|
|
|
} else {
|
2017-04-20 18:58:22 +00:00
|
|
|
nextDue := nextDue.Add(time.Minute)
|
2017-04-20 09:35:33 +00:00
|
|
|
logger.Println("INFO scheduling CloseDecisionsJob for", nextDue)
|
|
|
|
when := nextDue.Sub(time.Now())
|
|
|
|
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() {
|
|
|
|
logger.Println("INFO running CloseDecisionsJob")
|
|
|
|
err := CloseDecisions()
|
|
|
|
if err != nil {
|
|
|
|
logger.Println("ERROR closing decisions", err)
|
|
|
|
}
|
|
|
|
rescheduleChannel <- JobIdCloseDecisions
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *RemindVotersJob) Schedule() {
|
|
|
|
year, month, day := time.Now().UTC().Date()
|
|
|
|
nextExecution := time.Date(year, month, day, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 3)
|
|
|
|
logger.Println("INFO scheduling RemindVotersJob for", nextExecution)
|
|
|
|
when := nextExecution.Sub(time.Now())
|
|
|
|
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() {
|
|
|
|
logger.Println("INFO running RemindVotersJob")
|
|
|
|
defer func() { rescheduleChannel <- JobIdRemindVotersJob }()
|
|
|
|
|
|
|
|
voters, err := GetReminderVoters()
|
|
|
|
if err != nil {
|
|
|
|
logger.Println("ERROR problem getting voters", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, voter := range *voters {
|
|
|
|
decisions, err := FindUnvotedDecisionsForVoter(&voter)
|
|
|
|
if err != nil {
|
|
|
|
logger.Println("ERROR problem getting unvoted decisions")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(*decisions) > 0 {
|
2017-04-21 00:25:49 +00:00
|
|
|
NotifyMailChannel <- &RemindVoterNotification{voter: voter, decisions: *decisions}
|
2017-04-20 18:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|