flamenco/internal/manager/eventbus/events_workers.go
Sybren A. Stüvel 76a24243f0 Manager: Introduce event bus system
Introduce an "event bus"-like system. It's more like a fan-out
broadcaster for certain events. Instead of directly sending events to
SocketIO, they are now sent to the broker, which in turn sends it to any
registered "forwarder". Currently there is ony one forwarder, for
SocketIO.

This opens the door for a proper MQTT client that sends the same events
to an MQTT server.
2024-02-03 22:55:23 +01:00

53 lines
1.7 KiB
Go

package eventbus
import (
"github.com/rs/zerolog/log"
"projects.blender.org/studio/flamenco/internal/manager/persistence"
"projects.blender.org/studio/flamenco/pkg/api"
)
// NewWorkerUpdate returns a partial SocketIOWorkerUpdate struct for the given worker.
// It only fills in the fields that represent the current state of the worker. For
// example, it omits `PreviousStatus`. The ommitted fields can be filled in by
// the caller.
func NewWorkerUpdate(worker *persistence.Worker) api.SocketIOWorkerUpdate {
workerUpdate := api.SocketIOWorkerUpdate{
Id: worker.UUID,
Name: worker.Name,
Status: worker.Status,
Version: worker.Software,
Updated: worker.UpdatedAt,
CanRestart: worker.CanRestart,
}
if worker.StatusRequested != "" {
workerUpdate.StatusChange = &api.WorkerStatusChangeRequest{
Status: worker.StatusRequested,
IsLazy: worker.LazyStatusRequest,
}
}
if !worker.LastSeenAt.IsZero() {
workerUpdate.LastSeen = &worker.LastSeenAt
}
// TODO: add tag IDs.
return workerUpdate
}
func (b *Broker) BroadcastNewWorker(workerUpdate api.SocketIOWorkerUpdate) {
if workerUpdate.PreviousStatus != nil {
log.Warn().Interface("workerUpdate", workerUpdate).Msg("eventbus: new workers should not have a previous state")
workerUpdate.PreviousStatus = nil
}
log.Debug().Interface("workerUpdate", workerUpdate).Msg("eventbus: broadcasting new worker")
b.broadcast(TopicWorkerUpdate, workerUpdate)
}
func (b *Broker) BroadcastWorkerUpdate(workerUpdate api.SocketIOWorkerUpdate) {
log.Debug().Interface("workerUpdate", workerUpdate).Msg("eventbus: broadcasting worker update")
b.broadcast(TopicWorkerUpdate, workerUpdate)
}