Implement getRunner grpc endpoint

This includes refactoring the jobscheduler into the runnermanager. This
service manages runner connections and allocating them to pipelines.
These requests are done via the pipeline grpc api
This commit is contained in:
2022-12-31 17:22:00 -07:00
parent 663306c3be
commit 3cbe670bc1
5 changed files with 127 additions and 72 deletions
+47 -5
View File
@@ -2,11 +2,14 @@ package pipeline_api
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"git.ohea.xyz/cursorius/server/jobscheduler"
apiv1 "git.ohea.xyz/cursorius/server/proto/gen/api/v1"
"git.ohea.xyz/cursorius/server/proto/gen/api/v1/apiv1connect"
"git.ohea.xyz/cursorius/server/runnermanager"
"github.com/bufbuild/connect-go"
"github.com/op/go-logging"
)
@@ -14,7 +17,15 @@ import (
var log = logging.MustGetLogger("cursorius-server")
type ApiServer struct {
runCh chan jobscheduler.Run
getRunnerCh chan runnermanager.GetRunnerRequest
allocatedRunners map[int64]RunnerWrapper
currentId int64
currentIdMutex sync.Mutex
}
type RunnerWrapper struct {
runner runnermanager.Runner
mutex sync.Mutex
}
func (s *ApiServer) GetRunner(
@@ -22,8 +33,35 @@ func (s *ApiServer) GetRunner(
req *connect.Request[apiv1.GetRunnerRequest],
) (*connect.Response[apiv1.GetRunnerResponse], error) {
respChan := make(chan runnermanager.GetRunnerResponse)
s.getRunnerCh <- runnermanager.GetRunnerRequest{
Tags: req.Msg.Tags,
RespChan: respChan,
}
var runnerTagsStr strings.Builder
fmt.Fprintf(&runnerTagsStr, "%v", req.Msg.Tags[0])
for _, tag := range req.Msg.Tags[1:] {
fmt.Fprintf(&runnerTagsStr, " %v", tag)
}
response := <-respChan
if response.Err != nil {
log.Errorf("Could not get runner with tags \"%v\": %v", runnerTagsStr, response.Err)
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("Could not get runner"))
}
log.Info("Got runner with tags: %v", runnerTagsStr)
s.currentIdMutex.Lock()
runnerId := s.currentId
s.currentId++
s.currentIdMutex.Unlock()
s.allocatedRunners[runnerId] = RunnerWrapper{runner: response.Runner}
res := connect.NewResponse(&apiv1.GetRunnerResponse{
RunnerId: 0,
RunnerId: runnerId,
})
res.Header().Set("GetRunner-Version", "v1")
return res, nil
@@ -43,8 +81,12 @@ func (s *ApiServer) RunCommand(
return res, nil
}
func CreateHandler(mux *http.ServeMux, runCh chan jobscheduler.Run) {
api_server := &ApiServer{runCh: runCh}
func CreateHandler(mux *http.ServeMux, getRunnerCh chan runnermanager.GetRunnerRequest) {
api_server := &ApiServer{
getRunnerCh: getRunnerCh,
allocatedRunners: make(map[int64]RunnerWrapper),
currentId: 0,
}
path, handler := apiv1connect.NewGetRunnerServiceHandler(api_server)
mux.Handle(path, handler)
path, handler = apiv1connect.NewRunCommandServiceHandler(api_server)