-
Notifications
You must be signed in to change notification settings - Fork 0
/
guestRunner.go
238 lines (205 loc) · 6.11 KB
/
guestRunner.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
package agent
import (
"errors"
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"github.com/mistifyio/mistify-agent/config"
)
type (
// GuestRunner manages actions being performed for a guest
GuestRunner struct {
Context *Context
GuestID string
Info *SyncThrottle
Stream *SyncThrottle
Async *PipelineQueue
QuitChan chan struct{}
}
// SyncThrottle throttles synchronous actions
SyncThrottle struct {
GuestID string
Name string
ConcurrentChan chan struct{}
QuitChan chan struct{}
}
// PipelineQueue holds asyncronous action pipelines
PipelineQueue struct {
GuestID string
Name string
Context *Context
PipelineChan chan *Pipeline
QuitChan chan struct{}
}
)
const requestRunnerKey = "requestRunner"
// NewGuestRunner creates a new GuestRunner
func (context *Context) NewGuestRunner(guestID string, maxInfo uint, maxStream uint) *GuestRunner {
// Prevent others from modifying at the same time
context.GuestRunnerMutex.Lock()
defer context.GuestRunnerMutex.Unlock()
// Check if one already exists
runner, ok := context.GuestRunners[guestID]
if ok {
return runner
}
// Create a new runner
runner = &GuestRunner{
Context: context,
GuestID: guestID,
Info: NewSyncThrottle("info", guestID, maxInfo),
Stream: NewSyncThrottle("stream", guestID, maxStream),
Async: NewPipelineQueue("async", guestID, context),
}
runner.Async.Process()
context.GuestRunners[guestID] = runner
LogRunnerInfo(guestID, "", "", "Created")
return runner
}
// DeleteGuestRunner deletes a GuestRunner
func (context *Context) DeleteGuestRunner(guestID string) {
// Prevent others from modifying at the same time
context.GuestRunnerMutex.Lock()
defer context.GuestRunnerMutex.Unlock()
guestRunner, ok := context.GuestRunners[guestID]
if ok {
guestRunner.Quit()
}
delete(context.GuestRunners, guestID)
LogRunnerInfo(guestID, "", "", "Deleted")
}
// GetGuestRunner retrieves a GuestRunner
func (context *Context) GetGuestRunner(guestID string) (*GuestRunner, error) {
runner, ok := context.GuestRunners[guestID]
if !ok {
return nil, errors.New("guest runner not found")
}
return runner, nil
}
// GetAgentRunner retrieves the main agent runner
func (context *Context) GetAgentRunner() (*GuestRunner, error) {
return context.GetGuestRunner("agent")
}
// Quit shuts down a GuestRunner
func (gr *GuestRunner) Quit() {
LogRunnerInfo(gr.GuestID, "", "", "Quiting")
gr.Async.Quit()
}
// Process directs actions into sync or async handling depending on the type
func (gr *GuestRunner) Process(pipeline *Pipeline) error {
var err error
switch pipeline.Type {
case config.InfoAction:
err = gr.Info.Process(pipeline)
case config.StreamAction:
err = gr.Stream.Process(pipeline)
case config.AsyncAction:
LogRunnerInfo(gr.GuestID, "async", "", "Queued")
gr.Async.Enqueue(pipeline)
}
return err
}
// NewSyncThrottle creates a new SyncThrottle
func NewSyncThrottle(name string, guestID string, maxConcurrency uint) *SyncThrottle {
st := &SyncThrottle{
Name: name,
GuestID: guestID,
ConcurrentChan: make(chan struct{}, maxConcurrency),
}
for i := uint(0); i < maxConcurrency; i++ {
st.ConcurrentChan <- struct{}{}
}
return st
}
// Process runs an action
func (st *SyncThrottle) Process(pipeline *Pipeline) error {
st.Reserve()
defer st.Release()
return pipeline.Run()
}
// Reserve blocks until an action is allowed to run based on throttling
func (st *SyncThrottle) Reserve() {
<-st.ConcurrentChan
return
}
// Release signals that the action is done
func (st *SyncThrottle) Release() {
st.ConcurrentChan <- struct{}{}
return
}
// NewPipelineQueue creates a new PipelineQueue
func NewPipelineQueue(name string, guestID string, context *Context) *PipelineQueue {
max := 100
pq := &PipelineQueue{
Name: name,
GuestID: guestID,
PipelineChan: make(chan *Pipeline, max),
QuitChan: make(chan struct{}),
Context: context,
}
return pq
}
// Enqueue queues an async action
func (pq *PipelineQueue) Enqueue(pipeline *Pipeline) {
if err := pq.Context.JobLog.AddJob(pipeline.ID, pq.GuestID, pipeline.Action); err != nil {
LogRunnerError(pq.GuestID, pq.Name, pipeline.ID, err.Error())
}
pq.PipelineChan <- pipeline
return
}
// Process monitors the queue and kicks off async actions
func (pq *PipelineQueue) Process() {
go func() {
for {
select {
case <-pq.QuitChan:
LogRunnerInfo(pq.GuestID, pq.Name, "", "Quitting")
return
case pipeline := <-pq.PipelineChan:
if err := pq.Context.JobLog.UpdateJob(pipeline.ID, pipeline.Action, Running, ""); err != nil {
LogRunnerError(pq.GuestID, pq.Name, pipeline.ID, err.Error())
}
if err := pipeline.Run(); err != nil {
if err = pq.Context.JobLog.UpdateJob(pipeline.ID, pipeline.Action, Errored, err.Error()); err != nil {
LogRunnerError(pq.GuestID, pq.Name, pipeline.ID, err.Error())
}
LogRunnerError(pq.GuestID, pq.Name, pipeline.ID, err.Error())
} else {
if err = pq.Context.JobLog.UpdateJob(pipeline.ID, pipeline.Action, Complete, ""); err != nil {
LogRunnerError(pq.GuestID, pq.Name, pipeline.ID, err.Error())
}
LogRunnerInfo(pq.GuestID, pq.Name, pipeline.ID, "Success")
}
}
}
}()
}
// Quit signals the pipeline queue to stop processing after the current action
func (pq *PipelineQueue) Quit() {
go func() {
pq.QuitChan <- struct{}{}
}()
}
// LogRunnerInfo writes informational logs
func LogRunnerInfo(guestID string, runnerName string, pipelineID string, logLine string) {
log.WithFields(log.Fields{
"guest": guestID,
"runner": runnerName,
"pipeline": pipelineID,
}).Info(logLine)
}
// LogRunnerError writes error logs
func LogRunnerError(guestID string, runnerName string, pipelineID string, logLine string) {
log.WithFields(log.Fields{
"guest": guestID,
"runner": runnerName,
"pipeline": pipelineID,
}).Error(logLine)
}
// getRequestRunner retrieves the guest runner from the request context
func getRequestRunner(r *http.Request) *GuestRunner {
if value := context.Get(r, requestRunnerKey); value != nil {
return value.(*GuestRunner)
}
return nil
}