-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjob.go
135 lines (120 loc) · 4.35 KB
/
job.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
package main
import (
"path"
"path/filepath"
"strings"
"regexp"
"time"
"os"
"bufio"
"log"
"github.com/robfig/cron"
"fmt"
)
type Job struct {
Filepath string
Spec string
Timezone *time.Location
Name string
Timeout time.Duration
Enabled bool
Errors []string
UpdateTime time.Time
}
var cronSpecRegex,_ = regexp.Compile(`\s*($|#|\w+\s*=|(x|\*|(?:[0-5]?\d)(?:(?:-|%|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|%|\,)(?:[0-5]?\d))?)*)\s+(x|\*|(?:[0-5]?\d)(?:(?:-|%|\,)(?:[0-5]?\d))?(?:,(?:[0-5]?\d)(?:(?:-|%|\,)(?:[0-5]?\d))?)*)\s+(x|\*|(?:[01]?\d|2[0-3])(?:(?:-|%|\,)(?:[01]?\d|2[0-3]))?(?:,(?:[01]?\d|2[0-3])(?:(?:-|%|\,)(?:[01]?\d|2[0-3]))?)*)\s+(x|\*|(?:0?[1-9]|[12]\d|3[01])(?:(?:-|%|\,)(?:0?[1-9]|[12]\d|3[01]))?(?:,(?:0?[1-9]|[12]\d|3[01])(?:(?:-|%|\,)(?:0?[1-9]|[12]\d|3[01]))?)*)\s+(x|\*|(?:[1-9]|1[012])(?:(?:-|%|\,)(?:[1-9]|1[012]))?(?:L|W)?(?:,(?:[1-9]|1[012])(?:(?:-|%|\,)(?:[1-9]|1[012]))?(?:L|W)?)*|x|\*|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*)\s+(x|\*|(?:[0-6])(?:(?:-|%|\,|#)(?:[0-6]))?(?:L)?(?:,(?:[0-6])(?:(?:-|%|\,|#)(?:[0-6]))?(?:L)?)*|x|\*|(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?(?:,(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?)*)(|\s)+(x|\*|(?:|\d{4})(?:(?:-|%|\,)(?:|\d{4}))?(?:,(?:|\d{4})(?:(?:-|%|\,)(?:|\d{4}))?)*)) (.*)\.godoit`)
var noTimeout = time.Second * 0
var GodoitFileSuffix = ".godoit"
var godoitCommentPrefix = "#:godoit "
func ParseJobFile(directory, filename string) *Job {
jobPath := path.Join(directory, filename)
var cronspec string
var name string
enabled :=
!strings.HasPrefix(filename, "--") &&
!strings.HasPrefix(filename, "#")
if result := cronSpecRegex.FindStringSubmatch(filename); result != nil {
cronspec = strings.Replace(result[1], "x", "*", -1)
cronspec = strings.Replace(cronspec, "%", "/", -1)
name = strings.TrimSpace(result[10])
} else if strings.HasSuffix(filename, GodoitFileSuffix) {
name = strings.TrimSuffix(filename, GodoitFileSuffix)
} else {
return nil
}
cronspec, timeout, timezone, errors, updateTime := parseJobParameters(jobPath, cronspec)
if cronspec == "" {
errors = append(errors, "Missing cronspec")
}
if len(errors) > 0 {
enabled = false
log.Printf("Errors parsing job %s: %v", jobPath, errors)
}
return &Job{
filepath.Join(directory, filename),
cronspec,
timezone,
name,
timeout,
enabled,
errors,
updateTime}
}
func parseJobParameters(jobPath, cronspec string) (string, time.Duration, *time.Location, []string, time.Time) {
timeout := 0 * time.Second
timezone := time.UTC
var updateTime time.Time
errors := make ([]string,0,10)
if file, err := os.Open(jobPath); err == nil {
defer file.Close()
if info, err := file.Stat() ; err == nil {
updateTime = info.ModTime()
}
// create a new scanner and read the file line by line
scanner := bufio.NewScanner(file)
i := 0
for scanner.Scan() {
// Only scan start of file for params...
i++
if i > 10 {
break
}
line := scanner.Text()
if strings.HasPrefix(line, godoitCommentPrefix) {
line = strings.TrimPrefix(line, godoitCommentPrefix)
line = strings.TrimSpace(line)
parts := strings.SplitN(line," ",2)
if len(parts) == 2 {
param := parts[0]
value := parts[1]
if param == "cronspec" {
if cronspec != "" {
errors = append(errors, "Cronspec in filename and as comment")
}
if _, err := cron.Parse(value); err == nil {
cronspec = value
} else {
errors = append(errors, fmt.Sprintf("Invalid cronspec: '%s'", value))
}
} else if param == "timeout" {
if d, err := time.ParseDuration(value); err == nil {
timeout = d
} else {
errors = append(errors, fmt.Sprintf("Invalid timeout: '%s'", value))
}
} else if param == "timezone" {
if l, err := time.LoadLocation(value); err == nil {
timezone = l
} else {
errors = append(errors, fmt.Sprintf("Invalid timezone: '%s'", value))
}
}
} else {
errors = append(errors, fmt.Sprintf("Invalid parameter '%s'", line))
}
}
}
} else {
errors = append(errors, "Unable to open file to parse parameters")
}
return cronspec, timeout, timezone, errors, updateTime
}