forked from timjwright/godoit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobset.go
97 lines (84 loc) · 1.99 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
package main
import (
"log"
"github.com/robfig/cron"
"path/filepath"
"io/ioutil"
)
type JobSet struct {
executor JobExecutor
directory string
jobs map [string]Job
cron *cron.Cron
}
func NewJobSet(executor JobExecutor, directory string) *JobSet {
return &JobSet{executor, directory, make(map[string]Job), nil}
}
func (jobSet *JobSet) Stop() {
if ( jobSet.cron != nil ) {
log.Printf(" Stopping jobs in directory, %s", jobSet.directory)
jobSet.cron.Stop()
}
}
func (jobSet *JobSet) Scan() bool {
updated := false
// Scan for any new jobs
files, _ := ioutil.ReadDir(jobSet.directory)
filenames := make(map[string]bool)
for _,file := range files {
if ( ! file.IsDir() ) {
filename := file.Name()
job := ParseJobFile(jobSet.directory, filename)
if job != nil {
filenames[file.Name()] = true
if _,ok := jobSet.jobs[filename]; !ok {
updated = true
jobSet.jobs[filename] = *job
}
}
}
}
// Remove any old jobs
for filename,_ := range jobSet.jobs {
if _,ok := filenames[filename]; ! ok {
updated = true
delete(jobSet.jobs,filename)
}
}
// Setup the cron
if updated {
jobSet.setupCron()
}
return updated
}
func (jobSet *JobSet) setupCron() {
if ( jobSet.cron != nil ) {
jobSet.cron.Stop()
jobSet.cron = nil
}
if len(jobSet.jobs ) != 0 {
log.Printf(" Starting cron for %s", jobSet.directory)
jobSet.cron = cron.New()
for _,job := range jobSet.jobs {
addJob(jobSet.cron, jobSet.executor, job)
}
jobSet.cron.Start()
}
}
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)", job.Name, filepath.Dir(job.Filepath))
executor(job.Name, job.Filepath)
}
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", job.Spec, job.Name)
}
log.Printf("")
}