-
Notifications
You must be signed in to change notification settings - Fork 180
/
children-runner.go
311 lines (279 loc) · 9.67 KB
/
children-runner.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (c) 2019-2022. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package service
import (
"context"
"fmt"
pb "github.com/pydio/cells/v4/common/proto/registry"
"github.com/pydio/cells/v4/common/registry"
"github.com/pydio/cells/v4/common/runtime"
servicecontext "github.com/pydio/cells/v4/common/service/context"
"sync"
"time"
grpc2 "google.golang.org/grpc"
"go.uber.org/zap"
"github.com/pydio/cells/v4/common/client/grpc"
"github.com/pydio/cells/v4/common/config"
"github.com/pydio/cells/v4/common/log"
"github.com/pydio/cells/v4/common/proto/object"
"github.com/pydio/cells/v4/common/server/generic"
"github.com/pydio/cells/v4/common/utils/configx"
"github.com/pydio/cells/v4/common/utils/fork"
"github.com/pydio/cells/v4/common/utils/net"
)
// WithChildrenRunner option to define a micro server that runs children services
func WithChildrenRunner(parentName string, childrenPrefix string, cleanEndpointBeforeDelete bool, afterDeleteListener func(context.Context, string), secondaryPrefix ...string) []ServiceOption {
var runner *ChildrenRunner
return []ServiceOption{
WithGeneric(func(ctx context.Context, srv *generic.Server) error {
runner = NewChildrenRunner(parentName, childrenPrefix, secondaryPrefix...)
runner.StartFromInitialConf(ctx, config.Get("services", parentName))
runner.beforeDeleteClean = cleanEndpointBeforeDelete
if afterDeleteListener != nil {
runner.OnDeleteConfig(afterDeleteListener)
}
return runner.Watch(ctx)
}),
WithGenericStop(func(ctx context.Context, srv *generic.Server) error {
if runner != nil {
runner.StopAll(ctx)
}
return nil
}),
}
}
// NewChildrenRunner creates a ChildrenRunner
func NewChildrenRunner(parentName string, childPrefix string, secondaryPrefix ...string) *ChildrenRunner {
c := &ChildrenRunner{
parentName: parentName,
childPrefix: childPrefix,
}
if len(secondaryPrefix) > 0 && len(secondaryPrefix[0]) > 0 {
c.secondaryPrefix = secondaryPrefix[0]
}
c.mutex = &sync.RWMutex{}
c.services = make(map[string]*fork.Process)
return c
}
// ChildrenRunner For Regexp based service
type ChildrenRunner struct {
mutex *sync.RWMutex
services map[string]*fork.Process
parentName string
childPrefix string
secondaryPrefix string
beforeDeleteClean bool
afterDeleteChan chan string
initialCtx context.Context
}
// OnDeleteConfig defines what's happening when the config related to the service is deleted
func (c *ChildrenRunner) OnDeleteConfig(callback func(context.Context, string)) {
c.afterDeleteChan = make(chan string)
go func() {
for deleted := range c.afterDeleteChan {
callback(c.initialCtx, deleted)
}
}()
}
// StartFromInitialConf list the sources keys and start them
func (c *ChildrenRunner) StartFromInitialConf(ctx context.Context, cfg configx.Values) {
sources := config.SourceNamesFromDataConfigs(cfg)
c.initialCtx = ctx
log.Logger(ctx).Info("Starting umbrella service "+c.childPrefix+" with sources", log.DangerouslyZapSmallSlice("sources", sources))
for _, s := range sources {
if !c.FilterOutSource(ctx, s) {
go c.Start(ctx, s)
}
}
}
// Start starts a forked process for a new source
func (c *ChildrenRunner) Start(ctx context.Context, source string, retries ...int) error {
name := c.childPrefix + source
var opts []fork.Option
if config.Get("services", name, "debugFork").Bool() {
opts = append(opts, fork.WithDebug())
}
if len(config.DefaultBindOverrideToFlags()) > 0 {
opts = append(opts, fork.WithCustomFlags(config.DefaultBindOverrideToFlags()...))
}
opts = append(opts, fork.WithRetries(3))
opts = append(opts, fork.WithWatch(func(event string, process *fork.Process) {
c.mutex.Lock()
if event == "start" {
c.services[source] = process
} else if event == "stop" {
// TODO - should be centralized - Getting all processes attached to an old process and remove all
if reg := servicecontext.GetRegistry(ctx); reg != nil {
if pid, ok := process.GetPID(); ok {
items, err := reg.List(
registry.WithType(pb.ItemType_SERVER),
registry.WithFilter(func(item registry.Item) bool {
if item.Metadata()[runtime.NodeMetaPID] == pid {
return true
}
return false
}),
)
if err != nil {
return
}
for _, item := range items {
if err := reg.Deregister(item); err != nil {
log.Logger(ctx).Warn("could not deregister", zap.Error(err))
}
}
}
}
delete(c.services, source)
}
c.mutex.Unlock()
}))
names := []string{name}
if c.secondaryPrefix != "" {
names = append(names, c.secondaryPrefix+source)
}
process := fork.NewProcess(ctx, names, opts...)
process.StartAndWait()
return nil
}
// StopAll services
func (c *ChildrenRunner) StopAll(ctx context.Context) {
for name, cmd := range c.services {
log.Logger(ctx).Debug("stopping sub-process " + c.childPrefix + name)
cmd.Stop()
}
}
func (c *ChildrenRunner) updateSourcesList(ctx context.Context, sources []string) {
//log.Logger(ctx).Info("Got an event on sources keys for " + c.parentName + ". Let's start/stop services accordingly")
log.Logger(ctx).Info("Got an event on sources keys for "+c.parentName, zap.Strings("new sources", sources), zap.Any("currently running", c.services))
all := config.Get("services")
var servicesConf map[string]interface{}
all.Scan(&servicesConf)
// First stopping what's been removed
for name, cmd := range c.services {
found := false
for _, source := range sources {
if source == name && !c.FilterOutSource(ctx, source) {
found = true
break
}
}
if found {
continue
}
// Verify if it was fully deleted (not just filtered out)
_, exists := servicesConf[c.childPrefix+name]
if !exists && c.beforeDeleteClean {
caller := object.NewResourceCleanerEndpointClient(grpc.GetClientConnFromCtx(ctx, c.childPrefix+name))
if resp, err := caller.CleanResourcesBeforeDelete(ctx, &object.CleanResourcesRequest{}, grpc2.WaitForReady(false)); err == nil {
log.Logger(ctx).Info("Successfully cleaned resources before stopping "+c.childPrefix+name, zap.String("msg", resp.Message))
} else {
log.Logger(ctx).Error("Could not clean resources before stopping service", zap.Error(err))
}
if c.secondaryPrefix != "" {
caller = object.NewResourceCleanerEndpointClient(grpc.GetClientConnFromCtx(ctx, c.secondaryPrefix+name))
if resp, err := caller.CleanResourcesBeforeDelete(ctx, &object.CleanResourcesRequest{}, grpc2.WaitForReady(false)); err == nil {
log.Logger(ctx).Info("Successfully cleaned resources before stopping "+c.secondaryPrefix+name, zap.String("msg", resp.Message))
} else {
log.Logger(ctx).Error("Could not clean resources before stopping service", zap.Error(err))
}
}
}
log.Logger(ctx).Info("Stopping sub-service " + c.childPrefix + name)
cmd.Stop()
if !exists && c.afterDeleteChan != nil {
c.afterDeleteChan <- name
}
}
// Then start what's been added
for _, source := range sources {
c.mutex.RLock()
_, ok := c.services[source]
c.mutex.RUnlock()
if !ok && !c.FilterOutSource(ctx, source) {
log.Logger(ctx).Info("Starting sub-service " + c.childPrefix + source)
go c.Start(ctx, source)
}
}
}
// Watch watches the configuration changes for new sources
func (c *ChildrenRunner) Watch(ctx context.Context) error {
watcher, err := config.Watch(configx.WithPath("services", c.parentName, "sources"))
if err != nil {
return err
}
ss := make(chan []string)
go func() {
timer := time.NewTimer(1500 * time.Millisecond)
var sources []string
for {
select {
case <-timer.C:
if len(sources) > 0 {
c.updateSourcesList(ctx, sources)
sources = []string{}
}
case s := <-ss:
sources = s
timer.Stop()
timer = time.NewTimer(1500 * time.Millisecond)
case <-ctx.Done():
watcher.Stop()
return
}
}
}()
go func() {
for {
res, err := watcher.Next()
if err != nil {
break
}
arr := res.(configx.Values).StringArray()
ss <- config.SourceNamesFiltered(arr)
}
}()
return nil
}
// FilterOutSource checks in the actual source config if there are some
// keys that would prevent running on this node
func (c *ChildrenRunner) FilterOutSource(ctx context.Context, sourceName string) bool {
cfg := config.Get("services", c.childPrefix+sourceName)
if cfg == nil {
return false
}
var basic map[string]interface{}
if e := cfg.Scan(&basic); e == nil {
if val, ok := basic["Disabled"]; ok {
if b, is := val.(bool); is && b {
log.Logger(ctx).Info("Ignoring " + c.childPrefix + sourceName + " as it is disabled")
return true
} else if b, is := val.(string); is && b == "true" {
log.Logger(ctx).Info("Ignoring " + c.childPrefix + sourceName + " as it is disabled")
return true
}
}
if val, ok := basic["PeerAddress"]; ok && val.(string) != "" && !net.PeerAddressIsLocal(val.(string)) {
log.Logger(ctx).Info(fmt.Sprintf("Ignoring %s as PeerAddress (%s) does not correspond to any current peer ip", c.childPrefix+sourceName, val))
return true
}
}
return false
}