Cleanup runnermanager code

This commit is contained in:
2023-01-01 13:43:45 -07:00
parent 711a0257fc
commit ff78167325
2 changed files with 123 additions and 101 deletions
+26
View File
@@ -0,0 +1,26 @@
package runnermanager
import "nhooyr.io/websocket"
//var log = logging.MustGetLogger("cursorius-server")
type RunnerData struct {
msgType websocket.MessageType
data []byte
}
type Runner struct {
id string
tags []string
conn *websocket.Conn
receiveChan chan RunnerData
running bool
}
func (r *Runner) Id() string {
return r.id
}
func (r *Runner) RunCommand() (int64, string, string, error) {
return 0, "", "", nil
}
+32 -36
View File
@@ -21,18 +21,6 @@ type RunnerRegistration struct {
conn *websocket.Conn conn *websocket.Conn
} }
type RunnerData struct {
msgType websocket.MessageType
data []byte
}
type Runner struct {
id string
tags []string
conn *websocket.Conn
receiveChan chan RunnerData
running bool
}
type runnerManager struct { type runnerManager struct {
getRunnerCh chan GetRunnerRequest getRunnerCh chan GetRunnerRequest
registerCh chan RunnerRegistration registerCh chan RunnerRegistration
@@ -55,15 +43,10 @@ type runnerJob struct {
URL string URL string
} }
func runRunnerManager(r runnerManager) { func (r *runnerManager) processRequest(req GetRunnerRequest) {
for {
msgCase:
select {
case request := <-r.getRunnerCh:
var runnerTagsStr strings.Builder var runnerTagsStr strings.Builder
fmt.Fprintf(&runnerTagsStr, "%v", request.Tags[0]) fmt.Fprintf(&runnerTagsStr, "%v", req.Tags[0])
for _, tag := range request.Tags[1:] { for _, tag := range req.Tags[1:] {
fmt.Fprintf(&runnerTagsStr, " %v", tag) fmt.Fprintf(&runnerTagsStr, " %v", tag)
} }
log.Infof("Got request for runner with tags \"%v\"", runnerTagsStr.String()) log.Infof("Got request for runner with tags \"%v\"", runnerTagsStr.String())
@@ -83,6 +66,7 @@ func runRunnerManager(r runnerManager) {
case _, ok := <-runner.receiveChan: case _, ok := <-runner.receiveChan:
if ok { if ok {
// this should never happen // this should never happen
// TODO: should we disconnect the runner?
log.Errorf("Recieved data from inactive runner %v, this is a bug", runner.id) log.Errorf("Recieved data from inactive runner %v, this is a bug", runner.id)
continue continue
} }
@@ -92,11 +76,11 @@ func runRunnerManager(r runnerManager) {
r.connectedRunners = r.connectedRunners[:len(r.connectedRunners)-1] r.connectedRunners = r.connectedRunners[:len(r.connectedRunners)-1]
default: default:
runner.running = true runner.running = true
request.RespChan <- GetRunnerResponse{ req.RespChan <- GetRunnerResponse{
Runner: runner, Runner: runner,
Err: nil, Err: nil,
} }
break msgCase return
} }
} }
errorMsg := "could not find valid runner" errorMsg := "could not find valid runner"
@@ -104,20 +88,22 @@ func runRunnerManager(r runnerManager) {
errorMsg = "no connected runners" errorMsg = "no connected runners"
} }
log.Errorf("Could not allocate runner with tags \"%v\": %v", runnerTagsStr.String(), errorMsg) log.Errorf("Could not allocate runner with tags \"%v\": %v", runnerTagsStr.String(), errorMsg)
request.RespChan <- GetRunnerResponse{ req.RespChan <- GetRunnerResponse{
Runner: Runner{}, Runner: Runner{},
Err: fmt.Errorf("Could not allocate runner: %v", errorMsg), Err: fmt.Errorf("Could not allocate runner: %v", errorMsg),
} }
case registration := <-r.registerCh: }
log.Debugf("New runner appeared with id: %v and secret: %v", registration.Id, registration.Secret)
if configuredRunner, doesExist := r.configuredRunners[registration.Id]; doesExist { func (r *runnerManager) processRegistration(reg RunnerRegistration) {
if configuredRunner.Secret == registration.Secret { log.Debugf("New runner appeared with id: %v and secret: %v", reg.Id, reg.Secret)
log.Infof("Registering runner \"%v\" with tags %v", registration.Id, registration.Tags) if configuredRunner, doesExist := r.configuredRunners[reg.Id]; doesExist {
if configuredRunner.Secret == reg.Secret {
log.Infof("Registering runner \"%v\" with tags %v", reg.Id, reg.Tags)
runner := Runner{ runner := Runner{
id: registration.Id, id: reg.Id,
tags: registration.Tags, tags: reg.Tags,
conn: registration.conn, conn: reg.conn,
receiveChan: make(chan RunnerData), receiveChan: make(chan RunnerData),
running: false, running: false,
} }
@@ -126,7 +112,7 @@ func runRunnerManager(r runnerManager) {
// this is required to keep the connection functioning // this is required to keep the connection functioning
go func() { go func() {
for { for {
msgType, data, err := registration.conn.Read(context.Background()) msgType, data, err := reg.conn.Read(context.Background())
if err != nil { if err != nil {
// TODO: this is still racy, since a runner could be alloctade between the // TODO: this is still racy, since a runner could be alloctade between the
// connection returning an err and the channel closing // connection returning an err and the channel closing
@@ -143,13 +129,23 @@ func runRunnerManager(r runnerManager) {
}() }()
} else { } else {
log.Errorf("Disconnecting runner with id: %v and invalid secret: %v", registration.Id, registration.Secret) log.Errorf("Disconnecting runner with id: %v and invalid secret: %v", reg.Id, registration.Secret)
registration.conn.Close(websocket.StatusNormalClosure, "registration invalid") reg.conn.Close(websocket.StatusNormalClosure, "registration invalid")
} }
} else { } else {
log.Errorf("Disconnecting runner with invalid id: %v", registration.Id) log.Errorf("Disconnecting runner with invalid id: %v", reg.Id)
registration.conn.Close(websocket.StatusNormalClosure, "registration invalid") reg.conn.Close(websocket.StatusNormalClosure, "registration invalid")
} }
}
func runRunnerManager(r runnerManager) {
for {
select {
case request := <-r.getRunnerCh:
r.processRequest(request)
case registration := <-r.registerCh:
r.processRegistration(registration)
} }
} }
} }