summaryrefslogtreecommitdiff
path: root/jobs.go
blob: 9f43902145e7fb1eca4d99bde5ec3563b0ac47b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright 2017-2021 Jan Dittberner

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this program 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 (
	"time"

	log "github.com/sirupsen/logrus"
)

type Job interface {
	Schedule()
	Stop()
	Run()
}

type jobIdentifier int

const (
	JobIDCloseDecisions jobIdentifier = iota
	JobIDRemindVotersJob
)

var rescheduleChannel = make(chan jobIdentifier, 1)

func JobScheduler(quitChannel chan int) {
	var jobs = map[jobIdentifier]Job{
		JobIDCloseDecisions:  NewCloseDecisionsJob(),
		JobIDRemindVotersJob: NewRemindVotersJob(),
	}

	log.Info("started job scheduler")

	for {
		select {
		case jobID := <-rescheduleChannel:
			job := jobs[jobID]

			log.Infof("reschedule job %s", job)
			job.Schedule()
		case <-quitChannel:
			for _, job := range jobs {
				job.Stop()
			}

			log.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
		err     error
	)

	nextDue, err = GetNextPendingDecisionDue()
	if err != nil {
		log.Error("Could not get next pending due date")

		if j.timer != nil {
			j.timer.Stop()
			j.timer = nil
		}

		return
	}

	if nextDue == nil {
		log.Info("no next planned execution of CloseDecisionsJob")
		j.Stop()
	} else {
		nextDue := nextDue.Add(time.Second)
		log.Infof("scheduling CloseDecisionsJob for %s", nextDue)
		when := time.Until(nextDue)
		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()
		j.timer = nil
	}
}

func (j *CloseDecisionsJob) Run() {
	log.Debug("running CloseDecisionsJob")

	err := CloseDecisions()
	if err != nil {
		log.Errorf("closing decisions %v", err)
	}

	rescheduleChannel <- JobIDCloseDecisions
}

func (j *CloseDecisionsJob) String() string {
	return "CloseDecisionsJob"
}

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)

	log.Infof("scheduling RemindVotersJob for %s", nextExecution)

	when := time.Until(nextExecution)

	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() {
	log.Info("running RemindVotersJob")

	defer func() { rescheduleChannel <- JobIDRemindVotersJob }()

	var (
		voters []Voter
		err    error
	)

	voters, err = GetReminderVoters()
	if err != nil {
		log.Errorf("problem getting voters %v", err)

		return
	}

	var decisions []Decision

	for i := range voters {
		decisions, err = FindUnVotedDecisionsForVoter(&voters[i])
		if err != nil {
			log.Errorf("problem getting unvoted decisions: %v", err)

			return
		}

		if len(decisions) > 0 {
			NotifyMailChannel <- &RemindVoterNotification{voter: voters[i], decisions: decisions}
		}
	}
}