with '#' will be ignored, and an empty message aborts the commit. Author: Ethan Alicea <64653625+Tech-Gamer@users.noreply.github.com> On branch develop Your branch is up to date with 'origin/develop'. Changes to be committed: modified: .github/workflows/push.yaml modified: .github/workflows/release.yaml modified: CHANGELOG.md modified: Dockerfile modified: Makefile modified: README.md modified: cmd/configure.go modified: cmd/diagnostics.go modified: cmd/root.go modified: config/config.go modified: environment/allocations.go modified: environment/docker.go modified: environment/docker/api.go modified: environment/docker/container.go modified: environment/docker/environment.go modified: environment/docker/power.go modified: environment/docker/stats.go modified: environment/environment.go modified: environment/settings.go modified: events/events.go modified: go.mod modified: internal/cron/activity_cron.go modified: internal/cron/cron.go modified: internal/cron/sftp_cron.go modified: internal/database/database.go modified: internal/progress/progress.go modified: internal/progress/progress_test.go modified: loggers/cli/cli.go new file: oryxBuildBinary modified: parser/parser.go modified: remote/http.go modified: remote/servers.go modified: remote/types.go modified: router/downloader/downloader.go modified: router/middleware.go modified: router/middleware/middleware.go modified: router/middleware/request_error.go modified: router/router.go modified: router/router_download.go modified: router/router_server.go modified: router/router_server_backup.go modified: router/router_server_files.go modified: router/router_server_transfer.go modified: router/router_server_ws.go modified: router/router_system.go modified: router/router_transfer.go modified: router/tokens/parser.go modified: router/websocket/listeners.go modified: router/websocket/websocket.go modified: server/activity.go modified: server/backup.go modified: server/backup/backup.go modified: server/backup/backup_local.go modified: server/backup/backup_s3.go modified: server/configuration.go modified: server/console.go modified: server/crash.go modified: server/events.go modified: server/filesystem/archive.go modified: server/filesystem/filesystem.go modified: server/filesystem/filesystem_test.go modified: server/install.go modified: server/installer/installer.go modified: server/listeners.go modified: server/manager.go modified: server/mounts.go modified: server/power.go modified: server/power_test.go modified: server/resources.go modified: server/server.go modified: server/transfer/archive.go modified: server/transfer/source.go modified: server/transfer/transfer.go modified: server/update.go modified: sftp/event.go modified: sftp/handler.go modified: sftp/server.go modified: wings.go
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package events
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"emperror.dev/errors"
|
|
"github.com/goccy/go-json"
|
|
|
|
"github.com/Tech-Gamer/nwy-wings/system"
|
|
)
|
|
|
|
// Event represents an Event sent over a Bus.
|
|
type Event struct {
|
|
Topic string
|
|
Data interface{}
|
|
}
|
|
|
|
// Bus represents an Event Bus.
|
|
type Bus struct {
|
|
*system.SinkPool
|
|
}
|
|
|
|
// NewBus returns a new empty Bus. This is simply a nicer wrapper around the
|
|
// system.SinkPool implementation that allows for more simplistic usage within
|
|
// the codebase.
|
|
//
|
|
// All of the events emitted out of this bus are byte slices that can be decoded
|
|
// back into an events.Event interface.
|
|
func NewBus() *Bus {
|
|
return &Bus{
|
|
system.NewSinkPool(),
|
|
}
|
|
}
|
|
|
|
// Publish publishes a message to the Bus.
|
|
func (b *Bus) Publish(topic string, data interface{}) {
|
|
// Some of our actions for the socket support passing a more specific namespace,
|
|
// such as "backup completed:1234" to indicate which specific backup was completed.
|
|
//
|
|
// In these cases, we still need to send the event using the standard listener
|
|
// name of "backup completed".
|
|
if strings.Contains(topic, ":") {
|
|
parts := strings.SplitN(topic, ":", 2)
|
|
if len(parts) == 2 {
|
|
topic = parts[0]
|
|
}
|
|
}
|
|
|
|
enc, err := json.Marshal(Event{Topic: topic, Data: data})
|
|
if err != nil {
|
|
panic(errors.WithStack(err))
|
|
}
|
|
b.Push(enc)
|
|
}
|
|
|
|
// MustDecode decodes the event byte slice back into an events.Event struct or
|
|
// panics if an error is encountered during this process.
|
|
func MustDecode(data []byte) (e Event) {
|
|
if err := DecodeTo(data, &e); err != nil {
|
|
panic(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
// DecodeTo decodes a byte slice of event data into the given interface.
|
|
func DecodeTo(data []byte, v interface{}) error {
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
|
return errors.Wrap(err, "events: failed to decode byte slice")
|
|
}
|
|
return nil
|
|
}
|