Move execution request map to runners

This commit is contained in:
Konrad Hanff
2021-05-05 09:55:46 +02:00
parent 612bc55bdd
commit 07cdf17eb4
4 changed files with 93 additions and 53 deletions

View File

@ -1,9 +1,7 @@
package api
import (
"errors"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/config"
@ -11,20 +9,8 @@ import (
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool"
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
"net/http"
"sync"
)
var (
executions = make(map[string]map[string]dto.ExecutionRequest)
executionsLock = sync.Mutex{}
)
func allocateExecutionMap(runner runner.Runner) {
executionsLock.Lock()
executions[runner.Id()] = make(map[string]dto.ExecutionRequest)
executionsLock.Unlock()
}
// provideRunner tries to respond with the id of a runner
// This runner is then reserved for future use
func provideRunner(writer http.ResponseWriter, request *http.Request) {
@ -42,7 +28,6 @@ func provideRunner(writer http.ResponseWriter, request *http.Request) {
writeInternalServerError(writer, err, dto.ErrorNomadOverload)
return
}
allocateExecutionMap(runner)
sendJson(writer, &dto.RunnerResponse{Id: runner.Id()}, http.StatusOK)
}
@ -55,33 +40,23 @@ func executeCommand(router *mux.Router) func(w http.ResponseWriter, r *http.Requ
return
}
var scheme string
if config.Config.Server.TLS {
scheme = "wss"
} else {
scheme = "ws"
}
r, ok := runner.FromContext(request.Context())
if !ok {
log.Fatal("Expected runner in context! Something must be broken ...")
}
var scheme string
if config.Config.Server.TLS {
scheme = "wss"
} else {
scheme = "ws"
}
r, ok := runner.FromContext(request.Context())
if !ok {
log.Fatal("Expected runner in context! Something must be broken ...")
}
id, err := uuid.NewRandom()
id, err := r.AddExecution(*executionRequest)
if err != nil {
log.Printf("Error creating new execution id: %v", err)
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
executionsLock.Lock()
runnerExecutions, ok := executions[r.Id()]
if !ok {
writeNotFound(writer, errors.New("runner has not been provided"))
return
}
runnerExecutions[id.String()] = *executionRequest
executionsLock.Unlock()
path, err := router.Get("runner-websocket").URL("runnerId", r.Id())
if err != nil {
log.Printf("Error creating runner websocket URL %v", err)

View File

@ -75,7 +75,6 @@ func TestExecuteRoute(t *testing.T) {
testRunner := runner.NewExerciseRunner("testRunner")
runnerPool.AddRunner(testRunner)
allocateExecutionMap(testRunner)
path, err := router.Get("runner-execute").URL("runnerId", testRunner.Id())
if err != nil {
@ -120,8 +119,10 @@ func TestExecuteRoute(t *testing.T) {
t.Fatal(err)
}
executionId := url.Query().Get("executionId")
storedExecutionRequest, ok := testRunner.Execution(runner.ExecutionId(executionId))
assert.Equal(t, executionRequest, executions[testRunner.Id()][executionId])
assert.True(t, ok, "No execution request with this id")
assert.Equal(t, executionRequest, storedExecutionRequest)
})
})