-
Notifications
You must be signed in to change notification settings - Fork 3
/
cron.go
118 lines (99 loc) · 2.97 KB
/
cron.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
package core
import (
"encoding/json"
"time"
)
type Cron struct {
ID string `json:"cronid"`
InitiatorID string `json:"initiatorid"`
InitiatorName string `json:"initiatorname"`
ColonyName string `json:"colonyname"`
Name string `json:"name"`
CronExpression string `json:"cronexpression"`
Interval int `json:"interval"`
Random bool `json:"random"`
NextRun time.Time `json:"nextrun"`
LastRun time.Time `json:"lastrun"`
WorkflowSpec string `json:"workflowspec"`
PrevProcessGraphID string `json:"prevprocessgraphid"`
WaitForPrevProcessGraph bool `json:"waitforprevprocessgraph"`
CheckerPeriod int `json:"checkerperiod"`
}
func CreateCron(colonyName string, name string, cronExpression string, interval int, random bool, workflowSpec string) *Cron {
return &Cron{ColonyName: colonyName, Name: name, CronExpression: cronExpression, Interval: interval, Random: random, NextRun: time.Time{}, LastRun: time.Time{}, WorkflowSpec: workflowSpec, WaitForPrevProcessGraph: false}
}
func ConvertJSONToCron(jsonString string) (*Cron, error) {
var cron *Cron
err := json.Unmarshal([]byte(jsonString), &cron)
if err != nil {
return nil, err
}
return cron, nil
}
func ConvertCronArrayToJSON(crons []*Cron) (string, error) {
jsonBytes, err := json.Marshal(crons)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
func ConvertJSONToCronArray(jsonString string) ([]*Cron, error) {
var crons []*Cron
err := json.Unmarshal([]byte(jsonString), &crons)
if err != nil {
return crons, err
}
return crons, nil
}
func IsCronArraysEqual(crons1 []*Cron, crons2 []*Cron) bool {
if crons1 == nil || crons2 == nil {
return false
}
counter := 0
for _, cron1 := range crons1 {
for _, cron2 := range crons2 {
if cron1.Equals(cron2) {
counter++
}
}
}
if counter == len(crons1) && counter == len(crons2) {
return true
}
return false
}
func (cron *Cron) Equals(cron2 *Cron) bool {
if cron2 == nil {
return false
}
same := true
if cron.ID != cron2.ID ||
cron.ColonyName != cron2.ColonyName ||
cron.Name != cron2.Name ||
cron.CronExpression != cron2.CronExpression ||
cron.Interval != cron2.Interval ||
cron.Random != cron2.Random ||
cron.NextRun.Unix() != cron2.NextRun.Unix() ||
cron.LastRun.Unix() != cron2.LastRun.Unix() ||
cron.WorkflowSpec != cron2.WorkflowSpec ||
cron.PrevProcessGraphID != cron2.PrevProcessGraphID ||
cron.WaitForPrevProcessGraph != cron2.WaitForPrevProcessGraph ||
cron.CheckerPeriod != cron2.CheckerPeriod {
same = false
}
return same
}
func (cron *Cron) ToJSON() (string, error) {
jsonBytes, err := json.Marshal(cron)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
func (cron *Cron) HasExpired() bool {
now := time.Now()
if now.Sub(cron.NextRun) > 0 {
return true
}
return false
}