-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
43 lines (37 loc) · 1021 Bytes
/
task.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
package scheduler
import (
"fmt"
"time"
)
type Task interface {
Name() string
Run()
Init(map[string]interface{}) error
}
type TaskStatus struct {
Next time.Time `json:"next"`
Prev time.Time `json:"prev"`
}
type TaskConfig struct {
Name string `json:"name,omitempty"`
Cron string `json:"cron,omitempty"`
Interval int64 `json:"interval,omitempty"`
Params map[string]interface{} `json:"params,omitempty"`
}
func (cfg *TaskConfig) Check() (err error) {
switch {
case cfg.Name == "":
err = fmt.Errorf("task name not found")
case cfg.Cron == "" && cfg.Interval == 0:
err = fmt.Errorf("task[%s]execute parameter cron or interval is not specified", cfg.Name)
case cfg.Cron != "" && cfg.Interval > 0:
err = fmt.Errorf("task[%s]can not specify both cron and interval", cfg.Name)
}
return
}
func (cfg *TaskConfig)AddParam(key string,value interface{}){
if cfg.Params==nil{
cfg.Params=make(map[string]interface{})
}
cfg.Params[key]=value
}