Update runner manager for new database driven runner config
This commit is contained in:
+1
-1
@@ -581,7 +581,7 @@ WHERE id=$1;`
|
|||||||
Id: id,
|
Id: id,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := db.Conn.QueryRow(context.Background(), query, id).Scan(nil, &runner.Name, &runner.Token)
|
err := db.Conn.QueryRow(context.Background(), query, id).Scan(&runner.Name, &runner.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return runner, fmt.Errorf("Could not query database for runner with id %v: %w", id.String(), err)
|
return runner, fmt.Errorf("Could not query database for runner with id %v: %w", id.String(), err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
getRunnerCh, registerCh, err := runnermanager.StartRunnerManager(configData.Config.Runners)
|
getRunnerCh, registerCh, err := runnermanager.StartRunnerManager(configData.Config.Runners, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Could not start runner: %v", err)
|
log.Errorf("Could not start runner: %v", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"google.golang.org/protobuf/reflect/protoreflect"
|
"google.golang.org/protobuf/reflect/protoreflect"
|
||||||
"nhooyr.io/websocket"
|
"nhooyr.io/websocket"
|
||||||
@@ -17,14 +18,14 @@ type RunnerData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Runner struct {
|
type Runner struct {
|
||||||
id string
|
id uuid.UUID
|
||||||
tags []string
|
tags []string
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
receiveChan chan []byte
|
receiveChan chan []byte
|
||||||
running bool
|
running bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Runner) Id() string {
|
func (r *Runner) Id() uuid.UUID {
|
||||||
return r.id
|
return r.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/op/go-logging"
|
"github.com/op/go-logging"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"nhooyr.io/websocket"
|
"nhooyr.io/websocket"
|
||||||
|
|
||||||
"git.ohea.xyz/cursorius/server/config"
|
"git.ohea.xyz/cursorius/server/config"
|
||||||
|
"git.ohea.xyz/cursorius/server/database"
|
||||||
|
|
||||||
runner_api "git.ohea.xyz/cursorius/runner-api/go/api/v2"
|
runner_api "git.ohea.xyz/cursorius/runner-api/go/api/v2"
|
||||||
)
|
)
|
||||||
@@ -30,6 +32,7 @@ type runnerManager struct {
|
|||||||
connectedRunners []Runner
|
connectedRunners []Runner
|
||||||
numConnectedRunners uint64
|
numConnectedRunners uint64
|
||||||
configuredRunners map[string]config.Runner
|
configuredRunners map[string]config.Runner
|
||||||
|
db database.Database
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetRunnerRequest struct {
|
type GetRunnerRequest struct {
|
||||||
@@ -126,11 +129,30 @@ runnerIter:
|
|||||||
|
|
||||||
func (r *runnerManager) processRegistration(reg RunnerRegistration) {
|
func (r *runnerManager) processRegistration(reg RunnerRegistration) {
|
||||||
log.Debugf("New runner appeared with id: %v and secret: %v", reg.Id, reg.Secret)
|
log.Debugf("New runner appeared with id: %v and secret: %v", reg.Id, reg.Secret)
|
||||||
if configuredRunner, doesExist := r.configuredRunners[reg.Id]; doesExist {
|
|
||||||
if configuredRunner.Secret == reg.Secret {
|
// Get runner with give id from database
|
||||||
|
runnerId, err := uuid.Parse(reg.Id)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Disconnecting runner with id: %v, could not parse as UUID: %v", reg.Id, err)
|
||||||
|
reg.conn.Close(websocket.StatusNormalClosure, "registration invalid")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dbRunner, err := r.db.GetRunnerById(runnerId)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Disconnecting runner with id: %v, could not find runner in DB: %v", runnerId, err)
|
||||||
|
reg.conn.Close(websocket.StatusNormalClosure, "registration invalid")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if reg.Secret != dbRunner.Token {
|
||||||
|
log.Errorf("Disconnecting runner with id: %v, invalid secret", runnerId)
|
||||||
|
reg.conn.Close(websocket.StatusNormalClosure, "registration invalid")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log.Infof("Registering runner \"%v\" with tags %v", reg.Id, reg.Tags)
|
log.Infof("Registering runner \"%v\" with tags %v", reg.Id, reg.Tags)
|
||||||
runner := Runner{
|
runner := Runner{
|
||||||
id: reg.Id,
|
id: runnerId,
|
||||||
tags: reg.Tags,
|
tags: reg.Tags,
|
||||||
conn: reg.conn,
|
conn: reg.conn,
|
||||||
receiveChan: make(chan []byte),
|
receiveChan: make(chan []byte),
|
||||||
@@ -161,15 +183,6 @@ func (r *runnerManager) processRegistration(reg RunnerRegistration) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
} else {
|
|
||||||
log.Errorf("Disconnecting runner with id: %v and invalid secret: %v", reg.Id, reg.Secret)
|
|
||||||
reg.conn.Close(websocket.StatusNormalClosure, "registration invalid")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Errorf("Disconnecting runner with invalid id: %v", reg.Id)
|
|
||||||
reg.conn.Close(websocket.StatusNormalClosure, "registration invalid")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRunnerManager(r runnerManager) {
|
func runRunnerManager(r runnerManager) {
|
||||||
@@ -184,12 +197,13 @@ func runRunnerManager(r runnerManager) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartRunnerManager(configuredRunners map[string]config.Runner) (chan GetRunnerRequest, chan RunnerRegistration, error) {
|
func StartRunnerManager(configuredRunners map[string]config.Runner, db database.Database) (chan GetRunnerRequest, chan RunnerRegistration, error) {
|
||||||
scheduler := runnerManager{
|
scheduler := runnerManager{
|
||||||
getRunnerCh: make(chan GetRunnerRequest),
|
getRunnerCh: make(chan GetRunnerRequest),
|
||||||
registerCh: make(chan RunnerRegistration),
|
registerCh: make(chan RunnerRegistration),
|
||||||
connectedRunners: make([]Runner, 0),
|
connectedRunners: make([]Runner, 0),
|
||||||
configuredRunners: configuredRunners,
|
configuredRunners: configuredRunners,
|
||||||
|
db: db,
|
||||||
}
|
}
|
||||||
|
|
||||||
go runRunnerManager(scheduler)
|
go runRunnerManager(scheduler)
|
||||||
|
|||||||
Reference in New Issue
Block a user