/
job_persistence.go
92 lines (76 loc) · 1.51 KB
/
job_persistence.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
package core
import (
"github.com/bysir-zl/bygo/cache"
"github.com/bysir-zl/bygo/log"
"github.com/bysir-zl/bygo/util/uuid"
"time"
)
var redis = cache.NewRedis("")
func readAllUndoJobs(newJob NewJobFunc) (jobWraps *[]*JobWrap, err error) {
unDoJobs, err := redis.HGETALL(tableNameUndoJob)
if err != nil {
return
}
jobWraps_ := make([]*JobWrap, len(unDoJobs))
index := 0
for id, data := range unDoJobs {
jw := &JobWrap{
job: newJob(),
IdDb: id,
}
err = jw.Unmarshal(data.([]byte))
if err != nil {
return
}
jobWraps_[index] = jw
index++
}
jobWraps = &jobWraps_
return
}
func addJob(jobWrap *JobWrap) (err error) {
data, err := jobWrap.Marshal()
if err != nil {
return
}
id := uuid.Rand().Hex()
jobWrap.IdDb = id
err = redis.HMSET(tableNameUndoJob, id, data, 0)
if err != nil {
return
}
return
}
func deleteJobs(jobIds []string) (aff int64, err error) {
err = redis.HDEL(tableNameUndoJob, jobIds...)
if err != nil {
return
}
return
}
var willDeleteJobIds chan string = make(chan string, 2000)
func deleteJob(jobId string) (err error) {
willDeleteJobIds <- jobId
return
}
func InitPersistence(redisHost string) {
redis = cache.NewRedis(redisHost)
go func() {
ids := []string{}
for {
select {
case jobId := <-willDeleteJobIds:
ids = append(ids, jobId)
case <-time.Tick(time.Second * 5):
if len(ids) == 0 {
continue
}
_, err := deleteJobs(ids)
if err != nil {
log.Error("runner-pers", err)
}
ids = []string{}
}
}
}()
}