-
Notifications
You must be signed in to change notification settings - Fork 167
/
collector.go
204 lines (190 loc) · 4.91 KB
/
collector.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
package tasks
import (
"context"
"github.com/pydio/cells/v4/common/log"
"strings"
"sync"
"github.com/pydio/cells/v4/common/proto/jobs"
"github.com/pydio/cells/v4/common/utils/uuid"
)
type collector struct {
*sync.WaitGroup
ctx context.Context
id string
cm chan *jobs.ActionMessage
started, finished bool
done, final chan bool
coll []*jobs.ActionMessage
}
func newCollector(ctx context.Context) *collector {
c := &collector{
ctx: ctx,
id: uuid.New()[:6],
WaitGroup: &sync.WaitGroup{},
cm: make(chan *jobs.ActionMessage, 1000),
done: make(chan bool, 1),
final: make(chan bool, 1),
}
//fmt.Println("Setup Collector " + c.id)
go func() {
for {
select {
case <-c.ctx.Done():
c.finished = true
//fmt.Printf("Collector %s interrupted by context with %d elements\n", c.id, len(c.coll))
close(c.cm)
close(c.final)
return
case <-c.done:
c.finished = true
//fmt.Printf("Collector %s finished with %d elements\n", c.id, len(c.coll))
close(c.cm)
close(c.final)
return
case msg := <-c.cm:
c.coll = append(c.coll, msg)
}
}
}()
return c
}
func (c *collector) WaitMsg() *jobs.ActionMessage {
<-c.final
// MERGE NOW
out := &jobs.ActionMessage{}
mergeVars := make(map[string]interface{})
for _, m := range c.coll {
if m.Event != nil {
out.Event = m.Event
}
out.Users = append(out.Users, m.Users...)
out.Roles = append(out.Roles, m.Roles...)
out.Acls = append(out.Acls, m.Acls...)
out.Activities = append(out.Activities, m.Activities...)
out.Nodes = append(out.Nodes, m.Nodes...)
out.DataSources = append(out.DataSources, m.DataSources...)
out.Workspaces = append(out.Workspaces, m.Workspaces...)
out.OutputChain = append(out.OutputChain, m.OutputChain...)
// Handle ArrayMerge Variables, only load "Merge:XXX" vars
var mKeys []string
for _, k := range m.StackedVarsKeys() {
if strings.HasPrefix(k, "Merge:") {
mKeys = append(mKeys, k)
}
}
if len(mKeys) > 0 {
mVars := m.StackedVars(mKeys...)
for k, v := range mVars {
mergeKey := k
k = strings.TrimPrefix(k, "Merge:")
var jsonPath string
if strings.Contains(k, ":") {
parts := strings.Split(k, ":")
k = parts[0]
jsonPath = parts[1]
if jsonPath != "" && jobs.JSONPathSelector == nil {
log.TasksLogger(c.ctx).Warn("No JSONPathSelector found, variable 'Merge:" + k + "' will be merged as is")
jsonPath = ""
}
}
var sl []interface{}
if mv, o := mergeVars[k]; o {
if ms, ok := mv.([]interface{}); ok {
sl = ms
} else {
log.TasksLogger(c.ctx).Warn("Merging variable 'Merge:" + k + "', current value is not a slice, skipping")
continue
}
} else {
if jsonPath != "" {
log.TasksLogger(c.ctx).Debug("Merging contents into a slice " + k + " using JSONPath selector")
} else {
log.TasksLogger(c.ctx).Debug("Merging contents into a slice " + k)
}
}
if jsonPath != "" { // JSONPathSelector != nil checked above
if list, er := jobs.JSONPathSelector(c.ctx, map[string]interface{}{"Data": v}, jsonPath); er == nil {
for _, l := range list {
sl = append(sl, l)
}
} else {
log.TasksLogger(c.ctx).Warn("Merging variable 'Merge:" + k + "' with JSONPath " + jsonPath + " failed, input data is passed as $.Data")
}
} else {
sl = append(sl, v)
}
mergeVars[k] = sl
// Remove initial key from OutputChain - clone map to avoid direct write access to .Vars
var i int
for i = len(out.OutputChain) - 1; i >= 0; i-- {
o := out.OutputChain[i]
if o.Vars == nil {
continue
}
if _, here := o.Vars[mergeKey]; !here {
continue
}
vv := make(map[string]string, len(o.Vars)-1)
for name, value := range o.Vars {
if name != mergeKey {
vv[name] = value
}
}
o.Vars = vv
break
}
}
}
// Append merged vars to LastOutput or new one
if len(mergeVars) > 0 {
if lo := out.GetLastOutput(); lo != nil {
for k, v := range mergeVars {
lo.SetVar(k, v)
}
} else {
o := &jobs.ActionOutput{}
for k, v := range mergeVars {
o.SetVar(k, v)
}
out.OutputChain = append(out.OutputChain, o)
}
}
}
return out
}
func (c *collector) autoStart() {
if c.started {
return
}
//fmt.Println("AutoStart", c.id)
c.started = true
go func() {
c.Wait()
close(c.done)
}()
}
func (c *collector) Merge(i *jobs.ActionMessage) {
//fmt.Println("Merge on ", c.id, c.finished)
if c.finished {
return
}
c.autoStart()
c.cm <- i
}
func (c *collector) Add(delta int) {
//fmt.Println("Add ", delta, c.id, c.finished)
if c.finished {
return
}
c.autoStart()
//fmt.Println("ADDING", delta)
c.WaitGroup.Add(delta)
}
func (c *collector) Done() {
//fmt.Println("Done ", c.id, c.finished)
if c.finished {
return
}
//fmt.Println("REMOVING 1")
c.WaitGroup.Done()
}