From 222091b68c20efa4884a2c016c535cfc7d857daf Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 6 Apr 2020 21:03:50 -0700 Subject: [PATCH] Fire an event to the websocket when a backup is completed --- server/backup.go | 8 ++++++++ server/events.go | 14 +++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/server/backup.go b/server/backup.go index f8bbc83..681f0dd 100644 --- a/server/backup.go +++ b/server/backup.go @@ -170,6 +170,14 @@ func (b *Backup) BackupAndNotify() error { return err } + // Emit an event over the socket so we can update the backup in realtime on + // the frontend for the server. + b.server.Events().PublishJson(BackupCompletedEvent, map[string]interface{}{ + "uuid": b.Uuid, + "sha256_hash": resp.Sha256Hash, + "file_size": resp.FileSize, + }) + return nil } diff --git a/server/events.go b/server/events.go index 22413a1..dca2849 100644 --- a/server/events.go +++ b/server/events.go @@ -1,6 +1,7 @@ package server import ( + "encoding/json" "sync" ) @@ -50,6 +51,17 @@ func (e *EventBus) Publish(topic string, data string) { } } +func (e *EventBus) PublishJson(topic string, data interface{}) error { + b, err := json.Marshal(data) + if err != nil { + return err + } + + e.Publish(topic, string(b)) + + return nil +} + // Subscribe to an emitter topic using a channel. func (e *EventBus) Subscribe(topic string, ch chan Event) { e.mu.Lock() @@ -74,4 +86,4 @@ func (e *EventBus) Unsubscribe(topic string, ch chan Event) { } } } -} \ No newline at end of file +}