This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathstore.go
141 lines (123 loc) · 3.15 KB
/
store.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 scheduler
import (
"encoding/json"
"reflect"
"strconv"
"strings"
"time"
"github.com/rakanalh/scheduler/storage"
"github.com/rakanalh/scheduler/task"
)
type storeBridge struct {
store storage.TaskStore
funcRegistry *task.FuncRegistry
}
func (sb *storeBridge) Add(task *task.Task) error {
attributes, err := sb.getTaskAttributes(task)
if err != nil {
return err
}
return sb.store.Add(attributes)
}
func (sb *storeBridge) Fetch() ([]*task.Task, error) {
storedTasks, err := sb.store.Fetch()
if err != nil {
return []*task.Task{}, err
}
var tasks []*task.Task
for _, storedTask := range storedTasks {
lastRun, err := time.Parse(time.RFC3339, storedTask.LastRun)
if err != nil {
return nil, err
}
nextRun, err := time.Parse(time.RFC3339, storedTask.NextRun)
if err != nil {
return nil, err
}
duration, err := time.ParseDuration(storedTask.Duration)
if err != nil {
return nil, err
}
isRecurring, err := strconv.Atoi(storedTask.IsRecurring)
if err != nil {
return nil, err
}
funcMeta, err := sb.funcRegistry.Get(storedTask.Name)
if err != nil {
return nil, err
}
params, err := paramsFromString(funcMeta, storedTask.Params)
if err != nil {
return nil, err
}
t := task.NewWithSchedule(funcMeta, params, task.Schedule{
IsRecurring: isRecurring == 1,
Duration: time.Duration(duration),
LastRun: lastRun,
NextRun: nextRun,
})
tasks = append(tasks, t)
}
return tasks, nil
}
func (sb *storeBridge) Remove(task *task.Task) error {
attributes, err := sb.getTaskAttributes(task)
if err != nil {
return err
}
return sb.store.Remove(attributes)
}
func (sb *storeBridge) getTaskAttributes(task *task.Task) (storage.TaskAttributes, error) {
params, err := paramsToString(task.Params)
if err != nil {
return storage.TaskAttributes{}, err
}
isRecurring := 0
if task.IsRecurring {
isRecurring = 1
}
return storage.TaskAttributes{
Hash: string(task.Hash()),
Name: task.Func.Name,
LastRun: task.LastRun.Format(time.RFC3339),
NextRun: task.NextRun.Format(time.RFC3339),
Duration: task.Duration.String(),
IsRecurring: strconv.Itoa(isRecurring),
Params: params,
}, nil
}
func paramsToString(params []task.Param) (string, error) {
var paramsList []string
for _, param := range params {
paramStr, err := json.Marshal(param)
if err != nil {
return "", err
}
paramsList = append(paramsList, string(paramStr))
}
data, err := json.Marshal(paramsList)
return string(data), err
}
func paramsFromString(funcMeta task.FunctionMeta, payload string) ([]task.Param, error) {
var params []task.Param
if strings.TrimSpace(payload) == "" {
return params, nil
}
paramTypes := funcMeta.Params()
var paramsStrings []string
err := json.Unmarshal([]byte(payload), ¶msStrings)
if err != nil {
return params, err
}
for i, paramStr := range paramsStrings {
paramType := paramTypes[i]
target := reflect.New(paramType)
err := json.Unmarshal([]byte(paramStr), target.Interface())
if err != nil {
return params, err
}
param := reflect.Indirect(target).Interface().(task.Param)
params = append(params, param)
}
return params, nil
}