-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
85 lines (70 loc) · 1.87 KB
/
controller.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
package controller
import (
"errors"
"strconv"
"sync"
"github.com/runner-x/runner-x/engine/runtime"
)
type Controller interface {
SubmitRequest(runprops *runtime.RunProps) (*runtime.RunOutput, error)
runRequest(runprops *runtime.RunProps, agent *runtime.RuntimeAgent)
}
type AsyncController struct {
agents map[uint]*agentData
}
type agentData struct {
rwmutex sync.RWMutex
agent runtime.Runtime
}
func NewAsyncControllerWithMap(agents map[uint]*agentData) *AsyncController {
return &AsyncController{agents}
}
func NewAsyncController(size uint) *AsyncController {
agents := make(map[uint]*agentData)
for i := uint(0); i < size; i++ {
key := uint(i + 1)
agents[key] = &agentData{
rwmutex: sync.RWMutex{},
agent: runtime.NewRuntimeAgentWithIds(
"agent_"+strconv.FormatInt(int64(key), 10),
int(key),
&runtime.ProcessorArgsProvider{}),
}
}
return &AsyncController{agents}
}
type ControllerError error
var (
NoRunnerIsReady = ControllerError(errors.New("no runner available"))
InvalidInput = ControllerError(errors.New("invalid input"))
)
type ControllerRunOutput struct {
controllerErr ControllerError
runOutput *runtime.RunOutput
commandErr error
}
// SubmitRequest will run a command on the first runner agent it finds that is ready
func (ac *AsyncController) SubmitRequest(runprops *runtime.RunProps) *ControllerRunOutput {
if runprops == nil {
return &ControllerRunOutput{
controllerErr: InvalidInput,
runOutput: nil,
commandErr: nil,
}
}
for _, agentData := range ac.agents {
if agentData.agent.IsReady() {
runOutput, commandErr := agentData.agent.SafeRunCmd(runprops)
return &ControllerRunOutput{
controllerErr: nil,
runOutput: runOutput,
commandErr: commandErr,
}
}
}
return &ControllerRunOutput{
controllerErr: NoRunnerIsReady,
runOutput: nil,
commandErr: nil,
}
}