summaryrefslogtreecommitdiff
path: root/jobs.go
diff options
context:
space:
mode:
authorJan Dittberner <jan@dittberner.info>2017-04-20 11:35:33 +0200
committerJan Dittberner <jan@dittberner.info>2017-04-22 00:12:38 +0200
commitdcdd5f715f4800d02b04841054342ea2e44d950e (patch)
treee082c9ad6ec2e9964a9f3c6e6f694264ab76d329 /jobs.go
parent2de96dc13dc5e2843ebe204a7e1dfe6197ab7de0 (diff)
downloadcacert-boardvoting-dcdd5f715f4800d02b04841054342ea2e44d950e.tar.gz
cacert-boardvoting-dcdd5f715f4800d02b04841054342ea2e44d950e.tar.xz
cacert-boardvoting-dcdd5f715f4800d02b04841054342ea2e44d950e.zip
Implement decision closing job
Diffstat (limited to 'jobs.go')
-rw-r--r--jobs.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/jobs.go b/jobs.go
new file mode 100644
index 0000000..02683c8
--- /dev/null
+++ b/jobs.go
@@ -0,0 +1,95 @@
+package main
+
+import "time"
+
+type Job interface {
+ Schedule()
+ Stop()
+ Run()
+}
+
+type jobIdentifier int
+
+const (
+ JobIdCloseDecisions jobIdentifier = iota
+)
+
+var rescheduleChannel = make(chan jobIdentifier, 1)
+
+func JobScheduler(quitChannel chan int) {
+ var jobs = map[jobIdentifier]Job{
+ JobIdCloseDecisions: NewCloseDecisionsJob(),
+ }
+ 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 {
+ if j.timer != nil {
+ j.timer.Stop()
+ j.timer = nil
+ }
+ } else {
+ 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()
+ }
+}
+
+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"
+}