|
|
@ -24,18 +24,6 @@ import (
|
|
|
|
"git.cacert.org/cacert-boardvoting/internal/models"
|
|
|
|
"git.cacert.org/cacert-boardvoting/internal/models"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (app *application) SetupJobs() {
|
|
|
|
|
|
|
|
quitChannel := make(chan struct{})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
go app.jobScheduler.Schedule(quitChannel)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for range app.ctx.Done() {
|
|
|
|
|
|
|
|
quitChannel <- struct{}{}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Job interface {
|
|
|
|
type Job interface {
|
|
|
|
Schedule()
|
|
|
|
Schedule()
|
|
|
|
Run()
|
|
|
|
Run()
|
|
|
@ -92,7 +80,9 @@ func (r *RemindVotersJob) Run() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, voter := range voters {
|
|
|
|
for _, voter := range voters {
|
|
|
|
decisions, err = r.decisions.FindUnVotedDecisionsForVoter(&voter)
|
|
|
|
v := voter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
decisions, err = r.decisions.FindUnVotedDecisionsForVoter(&v)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
r.errorLog.Printf("problem getting unvoted decisions: %v", err)
|
|
|
|
r.errorLog.Printf("problem getting unvoted decisions: %v", err)
|
|
|
|
|
|
|
|
|
|
|
@ -126,7 +116,7 @@ func (app *application) NewRemindVotersJob(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type closeDecisionsJob struct {
|
|
|
|
type CloseDecisionsJob struct {
|
|
|
|
timer *time.Timer
|
|
|
|
timer *time.Timer
|
|
|
|
infoLog *log.Logger
|
|
|
|
infoLog *log.Logger
|
|
|
|
errorLog *log.Logger
|
|
|
|
errorLog *log.Logger
|
|
|
@ -135,7 +125,7 @@ type closeDecisionsJob struct {
|
|
|
|
notify chan NotificationMail
|
|
|
|
notify chan NotificationMail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *closeDecisionsJob) Schedule() {
|
|
|
|
func (c *CloseDecisionsJob) Schedule() {
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
nextDue *time.Time
|
|
|
|
nextDue *time.Time
|
|
|
|
err error
|
|
|
|
err error
|
|
|
@ -169,7 +159,7 @@ func (c *closeDecisionsJob) Schedule() {
|
|
|
|
c.timer.Reset(when)
|
|
|
|
c.timer.Reset(when)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *closeDecisionsJob) Run() {
|
|
|
|
func (c *CloseDecisionsJob) Run() {
|
|
|
|
c.infoLog.Printf("running CloseDecisionsJob")
|
|
|
|
c.infoLog.Printf("running CloseDecisionsJob")
|
|
|
|
|
|
|
|
|
|
|
|
results, err := c.decisions.CloseDecisions()
|
|
|
|
results, err := c.decisions.CloseDecisions()
|
|
|
@ -184,7 +174,7 @@ func (c *closeDecisionsJob) Run() {
|
|
|
|
c.reschedule <- c
|
|
|
|
c.reschedule <- c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *closeDecisionsJob) Stop() {
|
|
|
|
func (c *CloseDecisionsJob) Stop() {
|
|
|
|
if c.timer != nil {
|
|
|
|
if c.timer != nil {
|
|
|
|
c.timer.Stop()
|
|
|
|
c.timer.Stop()
|
|
|
|
c.timer = nil
|
|
|
|
c.timer = nil
|
|
|
@ -194,7 +184,7 @@ func (c *closeDecisionsJob) Stop() {
|
|
|
|
func (app *application) NewCloseDecisionsJob(
|
|
|
|
func (app *application) NewCloseDecisionsJob(
|
|
|
|
rescheduleChannel chan Job,
|
|
|
|
rescheduleChannel chan Job,
|
|
|
|
) Job {
|
|
|
|
) Job {
|
|
|
|
return &closeDecisionsJob{
|
|
|
|
return &CloseDecisionsJob{
|
|
|
|
infoLog: app.infoLog,
|
|
|
|
infoLog: app.infoLog,
|
|
|
|
errorLog: app.errorLog,
|
|
|
|
errorLog: app.errorLog,
|
|
|
|
decisions: app.decisions,
|
|
|
|
decisions: app.decisions,
|
|
|
@ -208,6 +198,7 @@ type JobScheduler struct {
|
|
|
|
errorLogger *log.Logger
|
|
|
|
errorLogger *log.Logger
|
|
|
|
jobs []Job
|
|
|
|
jobs []Job
|
|
|
|
rescheduleChannel chan Job
|
|
|
|
rescheduleChannel chan Job
|
|
|
|
|
|
|
|
quitChannel chan struct{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (app *application) NewJobScheduler() {
|
|
|
|
func (app *application) NewJobScheduler() {
|
|
|
@ -218,26 +209,23 @@ func (app *application) NewJobScheduler() {
|
|
|
|
errorLogger: app.errorLog,
|
|
|
|
errorLogger: app.errorLog,
|
|
|
|
jobs: make([]Job, 0, 2),
|
|
|
|
jobs: make([]Job, 0, 2),
|
|
|
|
rescheduleChannel: rescheduleChannel,
|
|
|
|
rescheduleChannel: rescheduleChannel,
|
|
|
|
|
|
|
|
quitChannel: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app.jobScheduler.addJob(app.NewCloseDecisionsJob(rescheduleChannel))
|
|
|
|
app.jobScheduler.addJob(app.NewCloseDecisionsJob(rescheduleChannel))
|
|
|
|
app.jobScheduler.addJob(app.NewRemindVotersJob(rescheduleChannel))
|
|
|
|
app.jobScheduler.addJob(app.NewRemindVotersJob(rescheduleChannel))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (js *JobScheduler) Schedule(quitChannel chan struct{}) {
|
|
|
|
func (js *JobScheduler) Schedule() {
|
|
|
|
for _, job := range js.jobs {
|
|
|
|
for _, job := range js.jobs {
|
|
|
|
js.infoLogger.Printf("schedule job %v", job)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
job.Schedule()
|
|
|
|
job.Schedule()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case job := <-js.rescheduleChannel:
|
|
|
|
case job := <-js.rescheduleChannel:
|
|
|
|
js.infoLogger.Printf("reschedule job %v", job)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
job.Schedule()
|
|
|
|
job.Schedule()
|
|
|
|
case <-quitChannel:
|
|
|
|
case <-js.quitChannel:
|
|
|
|
for _, job := range js.jobs {
|
|
|
|
for _, job := range js.jobs {
|
|
|
|
job.Stop()
|
|
|
|
job.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -252,3 +240,7 @@ func (js *JobScheduler) Schedule(quitChannel chan struct{}) {
|
|
|
|
func (js *JobScheduler) addJob(job Job) {
|
|
|
|
func (js *JobScheduler) addJob(job Job) {
|
|
|
|
js.jobs = append(js.jobs, job)
|
|
|
|
js.jobs = append(js.jobs, job)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (js *JobScheduler) Quit() {
|
|
|
|
|
|
|
|
js.quitChannel <- struct{}{}
|
|
|
|
|
|
|
|
}
|
|
|
|