-
Notifications
You must be signed in to change notification settings - Fork 168
/
transient-wrapper.go
60 lines (45 loc) · 1.29 KB
/
transient-wrapper.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
package registry
import (
"fmt"
"time"
"golang.org/x/exp/maps"
pb "github.com/pydio/cells/v4/common/proto/registry"
)
var _ Registry = (*TransientWrapper)(nil)
type TransientWrapper struct {
Registry
opts Options
}
func NewTransientWrapper(reg Registry, opts ...Option) Registry {
// mandatory
o := Options{}
for _, opt := range opts {
opt(&o)
}
return &TransientWrapper{
Registry: reg,
opts: o,
}
}
func (m *TransientWrapper) Register(item Item, opts ...RegisterOption) error {
items := m.opts.filterItems(item)
if len(items) == 1 {
targetItems := m.Registry.ListAdjacentItems(item, WithType(pb.ItemType_SERVER), WithType(pb.ItemType_NODE))
status := item.Metadata()[MetaStatusKey]
for _, targetItem := range targetItems {
if ms, ok := targetItem.(MetaSetter); ok {
targetMeta := targetItem.Metadata()
if status == string(StatusStarting) || status == string(StatusStopping) {
targetMetaClone := maps.Clone(targetMeta)
targetMetaClone[MetaTimestampKey] = fmt.Sprintf("%d", time.Now().UnixNano())
targetMetaClone[MetaStatusKey] = string(StatusTransient)
ms.SetMetadata(targetMetaClone)
if err := m.Registry.Register(targetItem, opts...); err != nil {
return err
}
}
}
}
}
return m.Registry.Register(item, opts...)
}