Add Worker Cleanup

This commit is contained in:
Samuel Lorch 2024-04-26 22:48:09 +02:00
parent 5dbd15c71c
commit 4194ab5944
2 changed files with 27 additions and 0 deletions

View file

@ -40,6 +40,9 @@ func Start(_conf config.Config) {
serverClose <- true
}()
stopCleanup := make(chan bool, 1)
go cleanupDeadWorkers(stopCleanup)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
select {
@ -50,6 +53,7 @@ func Start(_conf config.Config) {
stopCtx, cancel := context.WithTimeout(context.Background(), time.Second*10)
server.Shutdown(stopCtx)
cancel()
stopCleanup <- true
slog.Info("Done")
}
}

View file

@ -101,3 +101,26 @@ func readMessage(ctx context.Context, c *websocket.Conn) error {
slog.InfoContext(ctx, "Got Websocket Message", "type", t.String(), "data", data)
return c.Write(ctx, t, data)
}
func cleanupDeadWorkers(stop chan bool) {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
func() {
WorkersMutex.Lock()
defer WorkersMutex.Unlock()
for uuid, w := range Workers {
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)
// TODO Free any Jobs that the Worker had
}
}
}()
case <-stop:
return
}
}
}