-
Notifications
You must be signed in to change notification settings - Fork 4
/
backplane.go
82 lines (69 loc) · 2.21 KB
/
backplane.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
// Package backplane allows you to create a Choria based management backplane for your application
//
// Your application will live on the Choria network where it can be discovered and managed remotely
// on a very large scale with built in authentication, auditing and authorization.
//
// You will be able to interact with your application from the Choria CLI, Ruby API or Go API and
// perform some or all of the below
//
// * Circuit Breaker that can pause and resume your application
// * Healthchecks to query the internal health of your application
// * Shutdown the application
//
// Additionally data about your application like it's configuration can be exposed to the Choria
// discovery subsystem
package backplane
import (
"context"
"fmt"
"sync"
"github.com/choria-io/go-choria/server"
"github.com/choria-io/mcorpc-agent-provider/mcorpc"
"github.com/sirupsen/logrus"
)
// Version is the version of the management backplane
var Version = "development"
// Management is a embeddable Choria based backplane for your Go application
type Management struct {
cfg *Config
cserver *server.Instance
mu *sync.Mutex
factsMu *sync.Mutex
log *logrus.Entry
agent *mcorpc.Agent
outbox chan *DataItem
}
// Run creates a new instance of the backplane
func Run(ctx context.Context, wg *sync.WaitGroup, conf ConfigProvider, opts ...Option) (m *Management, err error) {
m = &Management{
mu: &sync.Mutex{},
outbox: make(chan *DataItem, 1),
}
m.cfg, err = newConfig("backplane", conf, opts...)
if err != nil {
return nil, fmt.Errorf("could not initialize Choria backplane: %s", err)
}
m.log = m.cfg.fw.Logger("backplane")
if m.cfg.infosource != nil {
f, err := m.exposeFacts(ctx, wg)
if err != nil {
return nil, fmt.Errorf("could not expose facts: %s", err)
}
m.cfg.ccfg.FactSourceFile = f
}
err = m.startServer(ctx, wg)
if err != nil {
return nil, fmt.Errorf("could not start Choria server: %s", err)
}
err = m.startAgents(ctx)
if err != nil {
return nil, fmt.Errorf("could not start backplane agents: %s", err)
}
if m.cfg.publishdata {
err = m.startDataPublisher(ctx, wg)
if err != nil {
return nil, fmt.Errorf("could not start data publisher: %s", err)
}
}
return m, nil
}