Implement worker status
This commit is contained in:
parent
cf000931e4
commit
0be5af70c3
5 changed files with 62 additions and 1 deletions
2
go.mod
2
go.mod
|
@ -6,5 +6,7 @@ require github.com/BurntSushi/toml v1.3.2
|
|||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mackerelio/go-osstat v0.2.4 // indirect
|
||||
golang.org/x/sys v0.6.0 // indirect
|
||||
nhooyr.io/websocket v1.8.11 // indirect
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -2,5 +2,9 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
|
|||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/mackerelio/go-osstat v0.2.4 h1:qxGbdPkFo65PXOb/F/nhDKpF2nGmGaCFDLXoZjJTtUs=
|
||||
github.com/mackerelio/go-osstat v0.2.4/go.mod h1:Zy+qzGdZs3A9cuIqmgbJvwbmLQH9dJvtio5ZjJTbdlQ=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
|
||||
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
|
||||
|
|
|
@ -2,7 +2,10 @@ package server
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/types"
|
||||
)
|
||||
|
||||
func handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -11,7 +14,20 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|||
WorkersMutex.Lock()
|
||||
defer WorkersMutex.Unlock()
|
||||
for i := range Workers {
|
||||
data = data + fmt.Sprintf("ID: %v, Name: %v, Address: %v Connected %v\n", i, Workers[i].Name, Workers[i].Address, Workers[i].Connected)
|
||||
if Workers[i].Connected {
|
||||
var status types.WorkerStatus
|
||||
_, err := rpcServer.Call(r.Context(), Workers[i].Conn, "status", nil, &status)
|
||||
if err != nil {
|
||||
w.Write([]byte(err.Error()))
|
||||
slog.ErrorContext(r.Context(), "Error Getting Worker Status", "err", err)
|
||||
return
|
||||
}
|
||||
slog.InfoContext(r.Context(), "Got Worker Status", "id", i, "status", status)
|
||||
data = data + fmt.Sprintf("ID: %v, Name: %v, Address: %v Connected %v\n", i, Workers[i].Name, Workers[i].Address, Workers[i].Connected)
|
||||
data = data + fmt.Sprintf("### CPU Cores: %v, CPU Usage: %v, Memory Total: %v Memory Usage %v\n", status.CPUCount, status.CPUUsage, status.MemoryTotal, status.MemoryUsage)
|
||||
} else {
|
||||
data = data + fmt.Sprintf("ID: %v, Name: %v, Address: %v Connected %v\n", i, Workers[i].Name, Workers[i].Address, Workers[i].Connected)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
8
types/status.go
Normal file
8
types/status.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package types
|
||||
|
||||
type WorkerStatus struct {
|
||||
CPUCount uint64 `json:"cpu_count"`
|
||||
CPUUsage uint64 `json:"cpu_usage"`
|
||||
MemoryTotal uint64 `json:"memory_total"`
|
||||
MemoryUsage uint64 `json:"memory_usage"`
|
||||
}
|
31
worker/status.go
Normal file
31
worker/status.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package worker
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mackerelio/go-osstat/cpu"
|
||||
"github.com/mackerelio/go-osstat/memory"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/rpc"
|
||||
"git.lastassault.de/speatzle/morffix/types"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rpcServer.RegisterMethod("status", status)
|
||||
}
|
||||
|
||||
func status(ctx context.Context, req rpc.Request) (any, error) {
|
||||
s := types.WorkerStatus{}
|
||||
cStats, _ := cpu.Get()
|
||||
if cStats != nil {
|
||||
s.CPUUsage = cStats.Total
|
||||
s.CPUCount = uint64(cStats.CPUCount)
|
||||
}
|
||||
mStats, _ := memory.Get()
|
||||
if mStats != nil {
|
||||
s.MemoryUsage = mStats.Used
|
||||
s.MemoryTotal = mStats.Total
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
Loading…
Add table
Reference in a new issue