This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjobs.go
199 lines (180 loc) · 3.97 KB
/
jobs.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
/*************************************************************************
* Copyright 2022 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package main
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
)
type job struct {
id int
cf context.CancelFunc
updates chan string
done bool
name string
latestUpdate string
}
func (j *job) IdString() string {
return fmt.Sprintf("Job %d", j.id)
}
func (j *job) LatestUpdate() string {
updateLoop:
for {
select {
case u := <-j.updates:
j.latestUpdate = u
default:
break updateLoop
}
}
if j.done {
return fmt.Sprintf("Done: %v", j.latestUpdate)
}
return j.latestUpdate
}
func (j *job) Cancel() {
j.cf()
}
func (j *job) Done() bool {
return j.done
}
type jobTracker struct {
sync.Mutex
jobs map[string]*job
cfg *cfgType
id int
}
func newJobTracker(cfg *cfgType) *jobTracker {
return &jobTracker{jobs: map[string]*job{}, cfg: cfg}
}
func (t *jobTracker) StartSourcetypeScanJob(cfgName string) *job {
t.Lock()
defer t.Unlock()
key := fmt.Sprintf("%s:%d", cfgName, rand.Int31())
ctx, cf := context.WithCancel(context.Background())
updateChan := make(chan string, 1000)
infostr := fmt.Sprintf("Check sourcetypes on %s", cfgName)
j := &job{cf: cf, updates: updateChan, id: t.id, name: infostr}
t.jobs[key] = j
t.id++
go func() {
err := checkMappings(cfgName, ctx, updateChan)
if err != nil {
lg.Warnf("Job returned %v", err)
updateChan <- fmt.Sprintf("Job returned error: %v", err)
}
t.done(key)
}()
return j
}
func (t *jobTracker) StartFileJob(cfgName string) *job {
t.Lock()
defer t.Unlock()
key := fmt.Sprintf("files:%s", cfgName)
if j, ok := t.jobs[key]; ok {
if !j.done {
return nil
}
}
ctx, cf := context.WithCancel(context.Background())
updateChan := make(chan string, 1000)
infostr := fmt.Sprintf("File config %s", cfgName)
j := &job{cf: cf, updates: updateChan, id: t.id, name: infostr}
t.jobs[key] = j
t.id++
go func() {
err := fileJob(cfgName, ctx, updateChan)
if err != nil {
lg.Warnf("Job returned %v", err)
updateChan <- fmt.Sprintf("Job returned error: %v", err)
}
t.done(key)
}()
return j
}
func (t *jobTracker) StartSplunkJob(cfgName string, progress SplunkToGravwell) *job {
t.Lock()
defer t.Unlock()
key := fmt.Sprintf("%s:%s", cfgName, progress.key())
if j, ok := t.jobs[key]; ok {
if !j.done {
return nil
}
}
ctx, cf := context.WithCancel(context.Background())
updateChan := make(chan string, 1000)
infostr := fmt.Sprintf("Server %s index %s sourcetype %s", cfgName, progress.Index, progress.Sourcetype)
j := &job{cf: cf, updates: updateChan, id: t.id, name: infostr}
t.jobs[key] = j
t.id++
go func() {
err := splunkJob(cfgName, progress, t.cfg, ctx, updateChan)
if err != nil {
lg.Warnf("Job returned %v", err)
updateChan <- fmt.Sprintf("Job returned error: %v", err)
}
t.done(key)
}()
return j
}
func (t *jobTracker) done(key string) {
t.Lock()
defer t.Unlock()
if j, ok := t.jobs[key]; ok {
j.done = true
t.jobs[key] = j
}
}
func (t *jobTracker) Shutdown() {
t.Lock()
defer t.Unlock()
for _, v := range t.jobs {
v.Cancel()
}
}
func (t *jobTracker) JobsDone() bool {
t.Lock()
defer t.Unlock()
for _, v := range t.jobs {
if !v.done {
return false
}
}
return true
}
func (t *jobTracker) ActiveJobs() int {
var count int
t.Lock()
defer t.Unlock()
for _, v := range t.jobs {
if !v.done {
count++
}
}
return count
}
func (t *jobTracker) GetAllJobs() (jobs []*job) {
t.Lock()
defer t.Unlock()
for _, v := range t.jobs {
jobs = append(jobs, v)
}
return
}
func (t *jobTracker) GetJobById(id int) (j *job, err error) {
t.Lock()
defer t.Unlock()
for _, v := range t.jobs {
if v.id == id {
return v, nil
}
}
return nil, errors.New("Job not found")
}