-
Notifications
You must be signed in to change notification settings - Fork 2
/
jobset.go
141 lines (121 loc) · 3.07 KB
/
jobset.go
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
package main
import (
"log"
"github.com/robfig/cron"
"path/filepath"
"io/ioutil"
"time"
"os"
"strings"
)
type JobSet struct {
executor JobExecutor
directory string
jobs map [string]Job
crons map [string]*cron.Cron
}
func NewJobSet(executor JobExecutor, directory string) *JobSet {
return &JobSet{executor, directory, make(map[string]Job), make(map[string]*cron.Cron)}
}
func (jobSet *JobSet) Stop() {
for key, cron := range jobSet.crons {
log.Printf(" Stopping jobs in directory, %s", jobSet.directory)
cron.Stop()
delete(jobSet.crons, key)
}
}
func (jobSet *JobSet) Scan() bool {
updated := false
// Scan for any new jobs
files, _ := ioutil.ReadDir(jobSet.directory)
foundFiles := make(map[string]bool)
for _,file := range files {
filename := file.Name()
foundFiles[filename] = true
if isGodoitFile(file) && shouldParseJob(file, jobSet) {
job := ParseJobFile(jobSet.directory, filename)
if job != nil {
updated = true
jobSet.jobs[filename] = *job
}
}
}
// Remove any old jobs
for filename,_ := range jobSet.jobs {
if _,ok := foundFiles[filename]; ! ok {
updated = true
delete(jobSet.jobs,filename)
}
}
// Setup the cron
if updated {
jobSet.setupCron()
}
return updated
}
func isGodoitFile(file os.FileInfo) bool {
return ! file.IsDir() && strings.HasSuffix(file.Name(), GodoitFileSuffix)
}
func shouldParseJob(file os.FileInfo, jobSet *JobSet) bool {
job,hasJob := jobSet.jobs[file.Name()]
if hasJob {
// If the job is know - parse if the modification time has changed
return job.UpdateTime != file.ModTime()
} else {
// If the job is not known - parse it
return true
}
}
func (jobSet *JobSet) setupCron() {
jobSet.Stop()
log.Printf(" Starting crons for %s", jobSet.directory)
for _,job := range jobSet.jobs {
if job.Enabled {
addJob(jobSet.cronForLocation(job.Timezone), jobSet.executor, job)
}
}
for timezone, cron := range jobSet.crons {
log.Printf(" Timezone: %s", timezone)
cron.Start()
}
}
func (jobSet *JobSet) cronForLocation(location *time.Location) *cron.Cron {
timezoneName := location.String()
if locationCron, ok := jobSet.crons[timezoneName]; ok {
return locationCron
} else {
var newCron = cron.NewWithLocation(location)
jobSet.crons[timezoneName] = newCron
return newCron
}
}
func addJob(cron *cron.Cron, executor JobExecutor, job Job) {
cron.AddFunc(job.Spec, func() {runJob(executor, job)})
}
func runJob(executor JobExecutor, job Job) {
log.Printf("Running job %s (%s) Timeout: %s", job.Name, filepath.Dir(job.Filepath), timeoutString(job.Timeout))
executor(job.Name, job.Filepath, job.Timeout)
}
func (jobSet *JobSet) printJobs() {
log.Printf("Jobs in %s", jobSet.directory)
if len(jobSet.jobs) == 0 {
log.Printf(" -- no jobs --")
}
for _,job := range jobSet.jobs {
log.Printf(
" %s (%s): %s (Timeout: %s, Enabled: %t)",
job.Spec,
job.Timezone.String(),
job.Name,
timeoutString(job.Timeout),
job.Enabled)
}
log.Printf("")
}
func timeoutString(timeout time.Duration) string {
if timeout == 0 {
return "None"
} else {
return timeout.String()
}
}