11 type jobIdentifier int
14 JobIdCloseDecisions jobIdentifier = iota
18 var rescheduleChannel = make(chan jobIdentifier, 1)
20 func JobScheduler(quitChannel chan int) {
21 var jobs = map[jobIdentifier]Job{
22 JobIdCloseDecisions: NewCloseDecisionsJob(),
23 JobIdRemindVotersJob: NewRemindVotersJob(),
25 log.Info("started job scheduler")
29 case jobId := <-rescheduleChannel:
31 log.Infof("reschedule job %s", job)
34 for _, job := range jobs {
37 log.Info("stop job scheduler")
43 type CloseDecisionsJob struct {
47 func NewCloseDecisionsJob() *CloseDecisionsJob {
48 job := &CloseDecisionsJob{}
53 func (j *CloseDecisionsJob) Schedule() {
54 var nextDue *time.Time
55 nextDue, err := GetNextPendingDecisionDue()
57 log.Error("Could not get next pending due date")
65 log.Info("no next planned execution of CloseDecisionsJob")
68 nextDue := nextDue.Add(time.Second)
69 log.Infof("scheduling CloseDecisionsJob for %s", nextDue)
70 when := nextDue.Sub(time.Now())
74 j.timer = time.AfterFunc(when, j.Run)
79 func (j *CloseDecisionsJob) Stop() {
86 func (j *CloseDecisionsJob) Run() {
87 log.Debug("running CloseDecisionsJob")
88 err := CloseDecisions()
90 log.Errorf("closing decisions %v", err)
92 rescheduleChannel <- JobIdCloseDecisions
95 func (j *CloseDecisionsJob) String() string {
96 return "CloseDecisionsJob"
99 type RemindVotersJob struct {
103 func NewRemindVotersJob() *RemindVotersJob {
104 job := &RemindVotersJob{}
109 func (j *RemindVotersJob) Schedule() {
110 year, month, day := time.Now().UTC().Date()
111 nextExecution := time.Date(year, month, day, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 3)
112 log.Infof("scheduling RemindVotersJob for %s", nextExecution)
113 when := nextExecution.Sub(time.Now())
117 j.timer = time.AfterFunc(when, j.Run)
121 func (j *RemindVotersJob) Stop() {
128 func (j *RemindVotersJob) Run() {
129 log.Info("running RemindVotersJob")
130 defer func() { rescheduleChannel <- JobIdRemindVotersJob }()
132 voters, err := GetReminderVoters()
134 log.Errorf("problem getting voters %v", err)
138 for _, voter := range *voters {
139 decisions, err := FindUnvotedDecisionsForVoter(&voter)
141 log.Errorf("problem getting unvoted decisions: %v", err)
144 if len(*decisions) > 0 {
145 NotifyMailChannel <- &RemindVoterNotification{voter: voter, decisions: *decisions}