-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestart_module.go
72 lines (64 loc) · 1.44 KB
/
restart_module.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
package main
import (
"encoding/gob"
_ "fmt"
"os"
"time"
"github.com/golang/protobuf/proto"
"github.com/larskluge/babl-server/kafka"
. "github.com/larskluge/babl-server/utils"
bn "github.com/larskluge/babl/bablnaming"
pb "github.com/larskluge/babl/protobuf/messages"
_ "github.com/muesli/cache2go"
cache "github.com/patrickmn/go-cache"
)
func getCache() *cache.Cache {
fp, err := os.Open(cacheDir)
defer fp.Close()
if err != nil {
return cache.New(1*time.Minute, 15*time.Second)
} else {
dec := gob.NewDecoder(fp)
items := map[string]cache.Item{}
err := dec.Decode(&items)
Check(err)
return cache.NewFrom(1*time.Minute, 15*time.Second, items)
}
}
func saveCache(c *cache.Cache) {
items := map[string]cache.Item{}
fp, err := os.Create(cacheDir)
defer fp.Close()
enc := gob.NewEncoder(fp)
items = c.Items()
err = enc.Encode(&items)
Check(err)
}
func IncrementOom(id string) int {
c := getCache()
var n int
var err error
_, found := c.Get(id)
if found {
n, err = c.IncrementInt(id, 1)
Check(err)
} else {
c.Set(id, 1, OomTimeout)
n = 1
}
saveCache(c)
return n
}
func (s *server) BroadcastModuleRestartRequest(m *RestartRequest) error {
dr := pb.RestartRequest{InstanceId: m.InstanceId}
req := pb.Meta{
Restart: &dr,
}
msg, err := proto.Marshal(&req)
if err != nil {
return err
}
topic := bn.ModuleToTopic(m.Module, true)
kafka.SendMessage(s.kafkaProducer, "", topic, &msg) // TODO return err
return nil
}