Fix sending status event to all subscribers, send disk usage when server is offline

This commit is contained in:
Matthew Penner 2020-05-22 11:01:27 -06:00
parent bd063682dc
commit 74097cc4ad
2 changed files with 34 additions and 4 deletions

View File

@ -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:

View File

@ -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