This repository has been archived by the owner on Apr 14, 2019. It is now read-only.
/
registry.go
73 lines (61 loc) · 1.67 KB
/
registry.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
package main
import (
"os"
"runtime"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/nats-io/go-nats"
"github.com/robfig/cron"
"github.com/stefanprodan/syros/models"
)
type Registry struct {
Topic string
Agent models.SyrosService
NatsConnection *nats.EncodedConn
Cron *cron.Cron
Config *Config
}
func NewRegistry(config *Config, nc *nats.EncodedConn, cron *cron.Cron) *Registry {
agent := models.SyrosService{
Environment: "all",
Type: "app",
}
agent.Config, _ = models.ConfigToMap(config, "m")
agent.Config["syros_version"] = version
agent.Config["os"] = runtime.GOOS
agent.Config["arch"] = runtime.GOARCH
agent.Config["golang"] = runtime.Version()
agent.Config["max_procs"] = strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10)
agent.Config["goroutines"] = strconv.FormatInt(int64(runtime.NumGoroutine()), 10)
agent.Config["cpu_count"] = strconv.FormatInt(int64(runtime.NumCPU()), 10)
agent.Config["boot_time"] = time.Now().UTC().Format(time.RFC3339)
agent.Hostname, _ = os.Hostname()
uuid, _ := models.NewUUID()
agent.Id = models.Hash(agent.Hostname + uuid)
registry := &Registry{
Topic: "registry",
NatsConnection: nc,
Agent: agent,
Cron: cron,
Config: config,
}
return registry
}
func (r *Registry) Register() {
r.Cron.AddFunc("@every 15s", func() {
err := r.RegisterAgent()
if err != nil {
log.Errorf("Registry NATS natsPublish failed %v", err.Error())
}
})
}
func (r *Registry) RegisterAgent() error {
ag := r.Agent
ag.Collected = time.Now().UTC()
err := r.NatsConnection.Publish(r.Topic, ag)
if err != nil {
return err
}
return nil
}