/
schedule.go
72 lines (59 loc) · 1.33 KB
/
schedule.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
package schedule
import (
"fmt"
"strings"
yaml "gopkg.in/yaml.v2"
)
type Schedule interface {
Start()
Channel() chan bool
}
type Config struct {
Type string `yaml:"type"`
Properties map[string]interface{} `yaml:"properties"`
}
const (
typeUnknown int = iota
typeConstant
typeCron
)
func New(scheduleType string, conf map[string]interface{}) (Schedule, error) {
properties, err := yaml.Marshal(&conf)
if err != nil {
panic("Could not re-marshal into YAML")
}
t := resolveType(strings.ToLower(scheduleType))
if t == typeUnknown {
return nil, fmt.Errorf("Unrecognized schedule type (%s)", scheduleType)
}
var c interface{}
switch t {
case typeConstant:
c = &ConstantConfig{}
err = yaml.Unmarshal(properties, c.(*ConstantConfig))
case typeCron:
c = &CronConfig{}
err = yaml.Unmarshal(properties, c.(*CronConfig))
}
if err != nil {
return nil, fmt.Errorf("Error when parsing backend config: %s", err)
}
var schedule Schedule
switch t {
case typeConstant:
schedule, err = newConstantSchedule(*c.(*ConstantConfig))
case typeCron:
schedule, err = newCronSchedule(*c.(*CronConfig))
}
return schedule, err
}
func resolveType(t string) int {
switch t {
case "constant", "interval":
return typeConstant
case "cron", "cronspec":
return typeCron
default:
return typeUnknown
}
}