Setup RPC Server
This commit is contained in:
parent
496d5f412f
commit
cf000931e4
2 changed files with 15 additions and 5 deletions
|
@ -7,6 +7,8 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/rpc"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/constants"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
|
@ -23,6 +25,8 @@ type Worker struct {
|
|||
var Workers = map[string]*Worker{}
|
||||
var WorkersMutex sync.Mutex
|
||||
|
||||
var rpcServer = rpc.NewServer()
|
||||
|
||||
func handleWorkerWebsocket(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get(constants.SHARED_SECRET_HEADER) != conf.SharedSecret {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
|
@ -68,6 +72,9 @@ func handleWorkerWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||
Connected: true,
|
||||
ConnectionChanged: time.Now(),
|
||||
}
|
||||
|
||||
// TODO call function for init worker connect or call function to handle Worker Reconnection (get task status, continue processing)
|
||||
|
||||
}()
|
||||
|
||||
// Set Status on Disconnect
|
||||
|
@ -99,7 +106,8 @@ func readMessage(ctx context.Context, c *websocket.Conn) error {
|
|||
return err
|
||||
}
|
||||
slog.InfoContext(ctx, "Got Websocket Message", "type", t.String(), "data", data)
|
||||
return c.Write(ctx, t, data)
|
||||
rpcServer.HandleMessage(ctx, c, data)
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanupDeadWorkers(stop chan bool) {
|
||||
|
@ -112,6 +120,7 @@ func cleanupDeadWorkers(stop chan bool) {
|
|||
defer WorkersMutex.Unlock()
|
||||
|
||||
for uuid, w := range Workers {
|
||||
// TODO Not dead Workers are sometimes also killed?
|
||||
if !w.Connected && w.ConnectionChanged.Add(time.Minute*5).Before(time.Now()) {
|
||||
slog.Warn("Removing Dead Worker", "uuid", uuid, "name", w.Name)
|
||||
delete(Workers, uuid)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue