bugfix: The web interface now refreshes when the config file is changed! (#281)

This commit is contained in:
James Read 2024-04-20 00:11:40 +01:00 committed by GitHub
parent 86b2187236
commit 5a644b0856
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 33 additions and 8 deletions

View File

@ -184,6 +184,8 @@ message GetReadyzResponse {
string status = 1;
}
message EventConfigChanged {}
service OliveTinApiService {
rpc GetDashboardComponents(GetDashboardComponentsRequest) returns (GetDashboardComponentsResponse) {
option (google.api.http) = {

View File

@ -137,6 +137,7 @@ func main() {
executor := executor.DefaultExecutor()
executor.AddListener(websocket.ExecutionListener)
config.AddListener(websocket.OnConfigChanged)
go onstartup.Execute(cfg, executor)
go oncron.Schedule(cfg, executor)

View File

@ -21,8 +21,14 @@ var (
Name: "olivetin_config_reloaded_count",
Help: "The number of times the config has been reloaded",
})
listeners []func()
)
func AddListener(l func()) {
listeners = append(listeners, l)
}
func Reload(cfg *Config) {
if err := viper.UnmarshalExact(&cfg); err != nil {
log.Errorf("Config unmarshal error %+v", err)
@ -34,4 +40,8 @@ func Reload(cfg *Config) {
cfg.SetDir(path.Dir(viper.ConfigFileUsed()))
cfg.Sanitize()
for _, l := range listeners {
l()
}
}

View File

@ -6,6 +6,7 @@ import (
ws "github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
"net/http"
)
@ -24,6 +25,12 @@ var marshalOptions = protojson.MarshalOptions{
EmitUnpopulated: true,
}
func OnConfigChanged() {
evt := &pb.EventConfigChanged{}
broadcast(evt)
}
var ExecutionListener WebsocketExecutionListener
type WebsocketExecutionListener struct{}
@ -72,12 +79,19 @@ func (WebsocketExecutionListener) OnExecutionFinished(logEntry *executor.Interna
ExecutionFinished: logEntry.ExecutionFinished,
}
broadcast("ExecutionFinished", le)
broadcast(le)
}
func broadcast(messageType string, pbmsg *pb.LogEntry) {
func broadcast(pbmsg protoreflect.ProtoMessage) {
payload, err := marshalOptions.Marshal(pbmsg)
if err != nil {
log.Errorf("websocket marshal error: %v", err)
return
}
messageType := pbmsg.ProtoReflect().Descriptor().FullName()
// <EVIL>
// So, the websocket wants to encode messages using the same protomarshaller
// as the REST API - this gives consistency instead of using encoding/json
@ -97,13 +111,8 @@ func broadcast(messageType string, pbmsg *pb.LogEntry) {
hackyMessage = append(hackyMessage, []byte("}")...)
// </EVIL>
if err != nil {
log.Errorf("websocket marshal error: %v", err)
return
}
for _, client := range clients {
client.conn.WriteMessage(1, hackyMessage)
client.conn.WriteMessage(ws.TextMessage, hackyMessage)
}
}

View File

@ -40,6 +40,7 @@ function websocketOnMessage (msg) {
e.payload = j.payload
switch (j.type) {
case 'EventConfigChanged':
case 'ExecutionFinished':
window.dispatchEvent(e)
break

View File

@ -134,6 +134,8 @@ function main () {
initMarshaller()
setupLogSearchBox()
window.addEventListener('EventConfigChanged', fetchGetDashboardComponents) // For websocket
window.fetch('webUiSettings.json').then(res => {
return res.json()
}).then(res => {