-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorer.go
77 lines (65 loc) · 1.43 KB
/
storer.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
package ecflow_watchman
import (
"bytes"
"github.com/go-redis/redis"
log "github.com/sirupsen/logrus"
"unsafe"
)
type Storer interface {
Create()
Send(owner string, repo string, message *bytes.Buffer)
Close()
}
type RedisStorer struct {
Address string
Password string
Database int
client *redis.Client
}
func (s *RedisStorer) Create() {
s.client = redis.NewClient(&redis.Options{
Addr: s.Address,
Password: s.Password,
DB: s.Database,
})
}
func (s *RedisStorer) Close() {
if s.client != nil {
s.client.Close()
s.client = nil
}
}
func (s *RedisStorer) Send(owner string, repo string, message *bytes.Buffer) {
log.WithFields(log.Fields{
"owner": owner,
"repo": repo,
}).Infof("store to redis... ")
key := owner + "/" + repo + "/status"
value := BytesToString(message.Bytes())
err := s.client.Set(key, value, 0).Err()
if err != nil {
log.WithFields(log.Fields{
"owner": owner,
"repo": repo,
}).Error("store to redis has error: ", err)
return
}
log.WithFields(log.Fields{
"owner": owner,
"repo": repo,
}).Info("store to redis...done")
}
func StoreToRedis(config EcflowServerConfig, message *bytes.Buffer, redisUrl string) {
storer := RedisStorer{
Address: redisUrl,
Password: "",
Database: 0,
}
storer.Create()
defer storer.Close()
storer.Send(config.Owner, config.Repo, message)
}
// from go-redis
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}