-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
172 lines (150 loc) · 4.43 KB
/
utils.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
package shigoto
import (
"math"
"time"
cronparser "github.com/KodepandaID/shigoto/pkg/cron-parser"
"github.com/KodepandaID/shigoto/pkg/mongodb-connector"
)
// After creating a new instance, the system will be load task
// data from persistent storage and added to scheduled storage mapping.
func LoadJobsFromPersistentStorage(c *Config) {
jobs, e := c.client.GetJobCollection()
if e != nil {
panic(e)
}
for _, job := range jobs {
loc, _ := time.LoadLocation(c.Timezone)
tnow := time.Now().Local().In(loc)
nextDate := tnow
if tnow.Unix() > job.NextDate.Unix() {
schedule, eFatal := c.parser.SetCurrentTime(tnow).Parse(job.CronFormat)
if eFatal != nil {
panic(eFatal)
}
nextDate = schedule.Next
}
tasks, e := c.client.GetTasks(job.ID)
if e != nil {
panic(e)
}
for _, task := range tasks {
if ScheduleStorage[nextDate.String()] == nil {
ScheduleStorage[nextDate.String()] = []map[string]interface{}{
{
"id": task.JobId.Hex(),
"job_name": job.JobName,
"func_name": job.FuncName,
"params": task.Params,
"cron": job.CronFormat,
},
}
} else {
ss := ScheduleStorage[nextDate.String()].([]map[string]interface{})
_, match := checkSameJobName(job.JobName, ss)
if !match {
ScheduleStorage[nextDate.String()] = append(ss, map[string]interface{}{
"id": task.JobId.Hex(),
"job_name": job.JobName,
"func_name": job.FuncName,
"params": task.Params,
"cron": job.CronFormat,
})
}
}
}
}
}
func checkSameJobName(name string, j []map[string]interface{}) ([]map[string]interface{}, bool) {
var match bool
for index, task := range j {
if name == task["job_name"].(string) {
if (index + 1) <= len(j) {
j = append(j[:index], j[index+1:]...)
} else {
j = nil
}
match = true
}
}
return j, match
}
// checkTask will check available task, if the task available,
// the task will be running.
func checkTask(c *Config) {
loc, _ := time.LoadLocation(c.Timezone)
for {
tnow := time.Now().Local().In(loc)
if ScheduleStorage[tnow.String()] != nil {
ss := ScheduleStorage[tnow.String()].([]map[string]interface{})
if len(ss) > 0 {
var e error
for _, task := range ss {
funcName := task["func_name"].(string)
params := task["params"].([]interface{})
if len(params) == 0 {
e = CallFunc(funcName)
} else {
e = CallFuncWithParams(funcName, params)
}
updateJob(c, tnow, task["job_name"].(string), e)
}
updateNextRun(tnow.String(), c.Timezone, ss[0]["cron"].([]string), ss)
}
}
}
}
// To create the next schedule after the success running.
// The old schedule will be removed.
func updateNextRun(key, timezone string, cron []string, tasks []map[string]interface{}) time.Time {
delete(ScheduleStorage, key)
parser := cronparser.New(&cronparser.Parser{
Timezone: timezone,
})
loc, _ := time.LoadLocation(timezone)
schedule, eFatal := parser.SetCurrentTime(time.Now().Local().In(loc)).Parse(cron)
if eFatal != nil {
panic(eFatal)
}
var newTasks []map[string]interface{}
for _, task := range tasks {
newTasks = append(newTasks, map[string]interface{}{
"id": task["id"].(string),
"job_name": task["job_name"].(string),
"func_name": task["func_name"].(string),
"params": task["params"].([]interface{}),
"cron": task["cron"].([]string),
})
}
ScheduleStorage[schedule.Next.String()] = newTasks
return schedule.Next
}
// updateJob to updating persistent data like total_run, total_error,
// success_rate and error_rate after running the task.
func updateJob(c *Config, tnow time.Time, jobName string, e error) {
go func() {
eInc := 0
if e != nil {
eInc = 1
}
job, e := c.client.GetOneJobCollection(jobName)
if e == nil {
successRate, errRate := countSuccessAndErrorRate(float64(job.TotalRun+1), float64(job.TotalError+eInc))
schedule, eFatal := c.parser.SetCurrentTime(tnow).Parse(job.CronFormat)
if eFatal != nil {
panic(eFatal)
}
c.client.UpdateJobCollection(job.ID, &mongodb.JobCollection{
NextDate: schedule.Next,
SuccessRate: successRate,
ErrorRate: errRate,
}, eInc)
}
}()
}
func countSuccessAndErrorRate(totalRun, totalError float64) (success float64, err float64) {
success = ((totalRun - totalError) / totalRun) * 100
err = (totalError / totalRun) * 100
success = math.Ceil(success*100) / 100
err = math.Ceil(err*100) / 100
return success, err
}