Documentation
¶
Overview ¶
Package events provides typed event emission and subscription for the jb-mesh event bus. System events are emitted automatically by node/tool lifecycle hooks. User events are emitted by tools via the SDK.
NATS subject namespace (DESIGN.md §2.3):
events.tool.<event> — tool lifecycle events.node.<event> — node lifecycle events.user.<topic> — user-defined (free-form)
Index ¶
- type Bus
- type Event
- func NodeHealth(node, status string, resources map[string]interface{}) Event
- func NodeJoined(node string, capabilities map[string]interface{}) Event
- func NodeLeft(node, reason string) Event
- func ToolConfigured(node, tool string, config map[string]interface{}) Event
- func ToolCrashed(node, tool string, err error, restartCount int) Event
- func ToolInstalled(node, tool, version string) Event
- func ToolRemoved(node, tool string) Event
- func ToolStarted(node, tool string) Event
- func ToolStopped(node, tool, reason string) Event
- func UserEvent(node, topic string, data map[string]interface{}) Event
- type EventHandler
- type PersistConfig
- type PersistentBus
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus handles event emission and subscription over NATS.
func (*Bus) Emit ¶
Emit publishes an event to the appropriate NATS subject. The subject is derived from the event type: "tool.installed" → "events.tool.installed"
func (*Bus) Subscribe ¶
func (b *Bus) Subscribe(pattern string, handler EventHandler) (*nats.Subscription, error)
Subscribe listens for events matching a subject pattern. Pattern examples:
- "events.tool.*" — all tool events
- "events.node.*" — all node events
- "events.tool.crashed" — only crash events
- "events.>" — all events
- "events.user.>" — all user events
func (*Bus) SubscribeType ¶
func (b *Bus) SubscribeType(eventType string, handler EventHandler) (*nats.Subscription, error)
SubscribeType is a convenience for subscribing to a specific event type. E.g., SubscribeType("tool.crashed", handler)
type Event ¶
type Event struct {
Type string `json:"type"` // e.g. "tool.installed", "node.joined"
Node string `json:"node"` // originating node
Timestamp time.Time `json:"timestamp"`
Data map[string]interface{} `json:"data"`
}
Event is the envelope for all mesh events.
func NodeHealth ¶
NodeHealth creates a node.health event.
func NodeJoined ¶
NodeJoined creates a node.joined event.
func ToolConfigured ¶
ToolConfigured creates a tool.configured event.
func ToolCrashed ¶
ToolCrashed creates a tool.crashed event.
func ToolInstalled ¶
ToolInstalled creates a tool.installed event.
func ToolRemoved ¶
ToolRemoved creates a tool.removed event.
func ToolStarted ¶
ToolStarted creates a tool.started event.
func ToolStopped ¶
ToolStopped creates a tool.stopped event.
type PersistConfig ¶
type PersistConfig struct {
StreamName string // default: "MESH_EVENTS"
MaxAge time.Duration // how long to keep events, default: 24h
MaxMsgs int64 // max events to keep, default: 10000 (0 = unlimited)
}
PersistConfig controls event persistence behavior.
func DefaultPersistConfig ¶
func DefaultPersistConfig() PersistConfig
DefaultPersistConfig returns sensible defaults.
type PersistentBus ¶
type PersistentBus struct {
*Bus
// contains filtered or unexported fields
}
PersistentBus extends Bus with JetStream-backed event persistence. Events are stored in a stream and can be replayed/queried.
func NewPersistentBus ¶
func NewPersistentBus(nc *nats.Conn, js nats.JetStreamContext, nodeName string, cfg PersistConfig) (*PersistentBus, error)
NewPersistentBus creates an event bus with JetStream persistence. All events published to events.> are captured in a stream.
func (*PersistentBus) History ¶
func (pb *PersistentBus) History(filter string, limit int) ([]Event, error)
History returns recent events matching a subject filter. Filter examples: "events.tool.crashed", "events.>", "events.node.*"
func (*PersistentBus) HistorySince ¶
HistorySince returns events after a given timestamp.