3cbe670bc1
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
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package pipeline_api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
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"
|
|
)
|
|
|
|
var log = logging.MustGetLogger("cursorius-server")
|
|
|
|
type ApiServer struct {
|
|
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(
|
|
ctx context.Context,
|
|
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: runnerId,
|
|
})
|
|
res.Header().Set("GetRunner-Version", "v1")
|
|
return res, nil
|
|
}
|
|
|
|
func (s *ApiServer) RunCommand(
|
|
ctx context.Context,
|
|
req *connect.Request[apiv1.RunCommandRequest],
|
|
) (*connect.Response[apiv1.RunCommandResponse], error) {
|
|
|
|
res := connect.NewResponse(&apiv1.RunCommandResponse{
|
|
ReturnCode: 0,
|
|
Stdout: "",
|
|
Stderr: "",
|
|
})
|
|
res.Header().Set("RunCommand-Version", "v1")
|
|
return res, nil
|
|
}
|
|
|
|
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)
|
|
mux.Handle(path, handler)
|
|
}
|