-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval.go
286 lines (254 loc) · 7 KB
/
interval.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*******************************************************************************
* Copyright 2018 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package scheduler
import (
"fmt"
"github.com/Circutor/edgex/internal/pkg/db"
"github.com/Circutor/edgex/internal/support/scheduler/errors"
contract "github.com/Circutor/edgex/pkg/models"
"github.com/robfig/cron/v3"
)
func getIntervals(limit int) ([]contract.Interval, error) {
var err error
var intervals []contract.Interval
if limit <= 0 {
intervals, err = dbClient.Intervals()
} else {
intervals, err = dbClient.IntervalsWithLimit(limit)
}
if err != nil {
return nil, err
}
return intervals, err
}
func addNewInterval(interval contract.Interval) (string, error) {
name := interval.Name
// Check if the name is unique
ret, err := dbClient.IntervalByName(name)
if err == nil && ret.Name == name {
return "", errors.NewErrIntervalNameInUse(name)
}
// Validate the Start time format
start := interval.Start
if start != "" {
if _, err := msToTime(start); err != nil {
return "", errors.NewErrInvalidTimeFormat(start)
}
}
// Validate the End time format
end := interval.End
if end != "" {
if _, err := msToTime(end); err != nil {
return "", errors.NewErrInvalidTimeFormat(end)
}
}
// Validate the Cron
cronRec := interval.Cron
freq := interval.Frequency
if cronRec != "" {
if _, err := cron.ParseStandard(cronRec); err != nil {
return "", errors.NewErrInvalidCronFormat(cronRec)
}
} else if freq != "" {
if !isFrequencyValid(interval.Frequency) {
return "", errors.NewErrInvalidFrequencyFormat(freq)
}
} else {
return "", fmt.Errorf("both cron and frequency fields empty")
}
// Validate that interval is not in queue
ret, err = scClient.QueryIntervalByName(name)
if err == nil && ret.Name == name {
return "", errors.NewErrIntervalNameInUse(name)
}
// Add the new interval to the database
ID, err := dbClient.AddInterval(interval)
if err != nil {
return "", err
}
// Push the new interval into scheduler queue
interval.ID = ID
err = scClient.AddIntervalToQueue(interval)
if err != nil {
//failed to add to scheduler queue
LoggingClient.Error(err.Error())
}
return ID, nil
}
func updateInterval(from contract.Interval) error {
to, err := dbClient.IntervalById(from.ID)
if err != nil {
// Check by name
_, err := dbClient.IntervalByName(from.Name)
if err != nil {
return errors.NewErrIntervalNotFound(from.ID)
}
}
// Update the fields
if from.Cron != "" {
if _, err := cron.ParseStandard(from.Cron); err != nil {
return errors.NewErrInvalidCronFormat(from.Cron)
}
to.Cron = from.Cron
}
if from.Frequency != "" {
if to.Cron == "" {
if !isFrequencyValid(from.Frequency) {
return errors.NewErrInvalidFrequencyFormat(from.Frequency)
}
}
to.Frequency = from.Frequency
}
if from.End != "" {
if _, err := msToTime(from.End); err != nil {
return errors.NewErrInvalidTimeFormat(from.End)
}
to.End = from.End
}
if from.Start != "" {
if _, err := msToTime(from.Start); err != nil {
return errors.NewErrInvalidTimeFormat(from.Start)
}
to.Start = from.Start
}
if from.Origin != 0 {
to.Origin = from.Origin
}
// Check if new name is unique
if from.Name != "" && from.Name != to.Name {
checkInterval, err := dbClient.IntervalByName(from.Name)
if err != nil {
if checkInterval.ID != "" {
return errors.NewErrIntervalNameInUse(from.Name)
}
}
// Check if the interval still has attached interval actions
stillInUse, err := isIntervalStillInUse(to)
if err != nil {
return err
}
if stillInUse {
return errors.NewErrIntervalStillInUse(to.Name)
}
to.Name = from.Name
}
err = scClient.UpdateIntervalInQueue(to)
if err != nil {
//failed to update the scheduler queue
LoggingClient.Error(err.Error())
return err
}
return dbClient.UpdateInterval(to)
}
func getIntervalById(id string) (contract.Interval, error) {
interval, err := dbClient.IntervalById(id)
if err != nil {
if err == db.ErrNotFound {
err = errors.NewErrIntervalNotFound(id)
}
return contract.Interval{}, err
}
return interval, nil
}
func getIntervalByName(name string) (interval contract.Interval, err error) {
interval, err = dbClient.IntervalByName(name)
if err != nil {
LoggingClient.Error(err.Error())
if err == db.ErrNotFound {
err = errors.NewErrIntervalNotFound(name)
}
return contract.Interval{}, err
}
return interval, nil
}
func deleteIntervalByName(name string) error {
// check in memory first
inMemory, err := scClient.QueryIntervalByName(name)
if err != nil {
return errors.NewErrIntervalNotFound(name)
}
// remove in memory
err = scClient.RemoveIntervalInQueue(inMemory.ID)
if err != nil {
return errors.NewErrDbNotFound()
}
// check if interval exists
interval, err := getIntervalByName(name)
if err != nil {
return err
}
if err = deleteInterval(interval); err != nil {
return err
}
return nil
}
func deleteIntervalById(id string) error {
// check in memory first
inMemory, err := scClient.QueryIntervalByID(id)
if err != nil {
return errors.NewErrIntervalNotFound(id)
}
// remove in memory
err = scClient.RemoveIntervalInQueue(inMemory.ID)
if err != nil {
return errors.NewErrDbNotFound()
}
// check if interval exists
interval, err := getIntervalById(id)
if err != nil {
return err
}
if err = deleteInterval(interval); err != nil {
return err
}
return nil
}
func deleteInterval(interval contract.Interval) error {
intervalActions, err := dbClient.IntervalActionsByIntervalName(interval.Name)
if err != nil {
LoggingClient.Error(err.Error())
return err
}
if len(intervalActions) > 0 {
LoggingClient.Error("Data integrity issue. Interval is still referenced by existing Interval Actions.")
return errors.NewErrIntervalStillInUse(interval.Name)
}
// Delete the interval
if err = dbClient.DeleteIntervalById(interval.ID); err != nil {
LoggingClient.Error(err.Error())
return err
}
return nil
}
// Helper function
func isIntervalStillInUse(s contract.Interval) (bool, error) {
var intervalActions []contract.IntervalAction
intervalActions, err := dbClient.IntervalActionsByIntervalName(s.Name)
if err != nil {
return false, err
}
if len(intervalActions) > 0 {
return true, nil
}
return false, nil
}
func scrubAll() (int, error) {
LoggingClient.Info("Scrubbing All Interval(s) and IntervalAction(s).")
count, err := dbClient.ScrubAllIntervals()
if err != nil {
LoggingClient.Error(err.Error())
return 0, err
}
return count, nil
}