From 74097cc4addc7648f739ed130225a7273f1c0e05 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Fri, 22 May 2020 11:01:27 -0600 Subject: [PATCH] Fix sending status event to all subscribers, send disk usage when server is offline --- router/websocket/websocket.go | 34 ++++++++++++++++++++++++++++++---- server/server.go | 4 ++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/router/websocket/websocket.go b/router/websocket/websocket.go index f0471c6..d187c2c 100644 --- a/router/websocket/websocket.go +++ b/router/websocket/websocket.go @@ -1,6 +1,7 @@ package websocket import ( + "encoding/json" "fmt" "github.com/gbrlsnchs/jwt/v3" "github.com/google/uuid" @@ -223,15 +224,40 @@ func (h *Handler) HandleInbound(m Message) error { h.setJwt(token) } - // On every authentication event, send the current server status back - // to the client. :) - h.server.Events().Publish(server.StatusEvent, h.server.GetState()) - h.unsafeSendJson(Message{ Event: AuthenticationSuccessEvent, Args: []string{}, }) + // On every authentication event, send the current server status back + // to the client. :) + state := h.server.GetState() + h.SendJson(&Message{ + Event: server.StatusEvent, + Args: []string{state}, + }) + + // Only send the current disk usage if the server is offline, if docker container is running, + // Environment#EnableResourcePolling() will send this data to all clients. + if state == server.ProcessOfflineState { + _ = h.server.Filesystem.HasSpaceAvailable() + + resources := server.ResourceUsage{ + Memory: 0, + MemoryLimit: 0, + CpuAbsolute: 0.0, + Disk: h.server.Resources.Disk, + } + resources.Network.RxBytes = 0 + resources.Network.TxBytes = 0 + + b, _ := json.Marshal(resources) + h.SendJson(&Message{ + Event: server.StatsEvent, + Args: []string{string(b)}, + }) + } + return nil } case SetStateEvent: diff --git a/server/server.go b/server/server.go index 82deffe..db831c4 100644 --- a/server/server.go +++ b/server/server.go @@ -247,6 +247,10 @@ func FromConfiguration(data *api.ServerConfigurationResponse) (*Server, error) { } s.Resources = ResourceUsage{} + // Force the disk usage to become cached to return in a resources response + // or when connecting to the websocket of an offline server. + go s.Filesystem.HasSpaceAvailable() + // Forces the configuration to be synced with the panel. if err := s.SyncWithConfiguration(data); err != nil { return nil, err