-
Notifications
You must be signed in to change notification settings - Fork 4
/
caretakerd.go
185 lines (168 loc) · 4.87 KB
/
caretakerd.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package caretakerd
import (
"github.com/echocat/caretakerd/control"
"github.com/echocat/caretakerd/errors"
"github.com/echocat/caretakerd/keyStore"
"github.com/echocat/caretakerd/logger"
"github.com/echocat/caretakerd/panics"
"github.com/echocat/caretakerd/rpc"
"github.com/echocat/caretakerd/service"
usync "github.com/echocat/caretakerd/sync"
"github.com/echocat/caretakerd/values"
"os"
osignal "os/signal"
"runtime"
"sync"
"syscall"
)
// Caretakerd instance structure
type Caretakerd struct {
config *Config
logger *logger.Logger
control *control.Control
services *service.Services
lock *sync.Mutex
syncGroup *usync.Group
execution *Execution
signalChannel chan os.Signal
open bool
keyStore *keyStore.KeyStore
}
func finalize(what *Caretakerd) {
what.Close()
}
// NewCaretakerd creates a new Caretakerd instance from the given config
func NewCaretakerd(conf *Config, syncGroup *usync.Group) (*Caretakerd, error) {
err := conf.Validate()
if err != nil {
return nil, err
}
log, err := logger.NewLogger(conf.Logger, "caretakerd", syncGroup)
if err != nil {
return nil, errors.New("Could not create logger for caretakerd.").CausedBy(err)
}
ks, err := keyStore.NewKeyStore(bool(conf.RPC.Enabled), conf.KeyStore)
if err != nil {
return nil, err
}
ctl, err := control.NewControl(conf.Control, ks)
if err != nil {
return nil, err
}
services, err := service.NewServices(conf.Services, syncGroup, ks)
if err != nil {
return nil, err
}
result := Caretakerd{
open: true,
config: conf,
logger: log,
control: ctl,
keyStore: ks,
services: services,
lock: new(sync.Mutex),
syncGroup: syncGroup,
signalChannel: nil,
}
runtime.SetFinalizer(&result, finalize)
return &result, nil
}
// IsOpen returns "true" if caretakerd is still open. This should return "false" after Close() was called.
func (instance Caretakerd) IsOpen() bool {
return instance.open
}
// Close closes the caretakerd instance and clears resources.
// After calling this method it is no longer possible to use this instance.
func (instance *Caretakerd) Close() {
defer func() {
instance.open = false
}()
instance.Stop()
instance.services.Close()
instance.logger.Close()
}
// Logger returns the instantiated logger that belongs to this instance.
func (instance Caretakerd) Logger() *logger.Logger {
return instance.logger
}
// Control returns the instantiated control that belongs to this instance.
func (instance *Caretakerd) Control() *control.Control {
return instance.control
}
// Services returns the instantiated services that belong to this instance.
func (instance *Caretakerd) Services() *service.Services {
return instance.services
}
// KeyStore returns the instantiated keyStore that belongs to this instance.
func (instance *Caretakerd) KeyStore() *keyStore.KeyStore {
return instance.keyStore
}
// ConfigObject returns the config that was used to create this instances.
func (instance *Caretakerd) ConfigObject() interface{} {
return instance.config
}
// Run starts every services and required resources of caretakerd.
// This is a blocking method.
func (instance *Caretakerd) Run() (values.ExitCode, error) {
var r *rpc.RPC
defer func() {
instance.uninstallTerminationNotificationHandler()
if r != nil {
r.Stop()
}
}()
execution := NewExecution(instance)
if instance.config.RPC.Enabled == values.Boolean(true) {
r = rpc.NewRPC(instance.config.RPC, execution, instance, instance.logger)
r.Start()
}
instance.installTerminationNotificationHandler()
instance.execution = execution
return execution.Run()
}
// Stop stops this instance (if it is running).
// This method is blocking until every service and resource is stopped.
func (instance *Caretakerd) Stop() {
defer func() {
instance.execution = nil
}()
execution := instance.execution
if execution != nil {
execution.StopAll()
}
instance.syncGroup.Interrupt()
}
func (instance *Caretakerd) installTerminationNotificationHandler() {
instance.lock.Lock()
defer func() {
instance.lock.Unlock()
}()
if instance.signalChannel == nil {
instance.signalChannel = make(chan os.Signal, 1)
osignal.Notify(instance.signalChannel, syscall.SIGINT, syscall.SIGTERM)
go instance.terminationNotificationHandler()
}
}
func (instance *Caretakerd) terminationNotificationHandler() {
defer panics.DefaultPanicHandler()
for {
osSignal, channelReady := <-instance.signalChannel
if channelReady {
signal := values.Signal(osSignal.(syscall.Signal))
instance.Logger().Log(logger.Debug, "Received shutdown signal: %v", signal)
instance.Stop()
} else {
break
}
}
}
func (instance *Caretakerd) uninstallTerminationNotificationHandler() {
instance.lock.Lock()
defer func() {
instance.signalChannel = nil
instance.lock.Unlock()
}()
if instance.signalChannel != nil {
osignal.Stop(instance.signalChannel)
}
}