event

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package event provides the event bus abstraction for lifecycle hooks and asynchronous communication between ctrlplane subsystems. It defines event types, the Bus interface, and handler registration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus interface {
	// Publish sends an event to all matching subscribers.
	Publish(ctx context.Context, event *Event) error

	// Subscribe registers a handler for the given event types.
	// If no types are specified, the handler receives all events.
	Subscribe(handler Handler, types ...Type) Subscription

	// Close shuts down the event bus and releases resources.
	Close() error
}

Bus is the interface for publishing and subscribing to events.

type Event

type Event struct {
	ID         id.ID          `json:"id"`
	Type       Type           `json:"type"`
	TenantID   string         `json:"tenant_id"`
	InstanceID id.ID          `json:"instance_id,omitzero"`
	ActorID    string         `json:"actor_id,omitempty"`
	Payload    map[string]any `json:"payload,omitempty"`
	Timestamp  time.Time      `json:"timestamp"`
}

Event is the envelope for all ctrlplane events.

func NewEvent

func NewEvent(t Type, tenantID string) *Event

NewEvent creates an event with a fresh ID and timestamp.

func (*Event) WithActor

func (e *Event) WithActor(actorID string) *Event

WithActor sets the actor ID on the event.

func (*Event) WithInstance

func (e *Event) WithInstance(instanceID id.ID) *Event

WithInstance sets the instance ID on the event.

func (*Event) WithPayload

func (e *Event) WithPayload(payload map[string]any) *Event

WithPayload sets the payload on the event.

type Handler

type Handler func(ctx context.Context, event *Event) error

Handler is a function that processes an event.

type InMemoryBus

type InMemoryBus struct {
	// contains filtered or unexported fields
}

InMemoryBus is a channel-based event bus for single-process use and testing.

func NewInMemoryBus

func NewInMemoryBus() *InMemoryBus

NewInMemoryBus creates a new in-memory event bus.

func (*InMemoryBus) Close

func (b *InMemoryBus) Close() error

Close is a no-op for the in-memory bus.

func (*InMemoryBus) Publish

func (b *InMemoryBus) Publish(ctx context.Context, evt *Event) error

Publish sends an event to all matching subscribers synchronously.

func (*InMemoryBus) Subscribe

func (b *InMemoryBus) Subscribe(handler Handler, types ...Type) Subscription

Subscribe registers a handler for the given event types. If no types are specified, the handler receives all events.

type Subscription

type Subscription interface {
	// Unsubscribe removes this subscription.
	Unsubscribe()
}

Subscription represents an active event subscription.

type Type

type Type string

Type identifies the kind of event.

const (
	InstanceCreated     Type = "instance.created"
	InstanceStarted     Type = "instance.started"
	InstanceStopped     Type = "instance.stopped"
	InstanceFailed      Type = "instance.failed"
	InstanceDeleted     Type = "instance.deleted"
	InstanceScaled      Type = "instance.scaled"
	InstanceSuspended   Type = "instance.suspended"
	InstanceUnsuspended Type = "instance.unsuspended"
)

Instance events.

const (
	DeployStarted    Type = "deploy.started"
	DeploySucceeded  Type = "deploy.succeeded"
	DeployFailed     Type = "deploy.failed"
	DeployRolledBack Type = "deploy.rolled_back"
)

Deploy events.

const (
	HealthCheckPassed Type = "health.passed"
	HealthCheckFailed Type = "health.failed"
	HealthDegraded    Type = "health.degraded"
	HealthRecovered   Type = "health.recovered"
)

Health events.

const (
	DomainAdded     Type = "domain.added"
	DomainVerified  Type = "domain.verified"
	DomainRemoved   Type = "domain.removed"
	CertProvisioned Type = "cert.provisioned"
	CertExpiring    Type = "cert.expiring"
)

Network events.

const (
	TenantCreated   Type = "tenant.created"
	TenantSuspended Type = "tenant.suspended"
	TenantDeleted   Type = "tenant.deleted"
	QuotaExceeded   Type = "quota.exceeded"
)

Admin events.

type Webhook

type Webhook struct {
	ctrlplane.Entity

	TenantID string `db:"tenant_id" json:"tenant_id"`
	URL      string `db:"url"       json:"url"`
	Secret   string `db:"secret"    json:"-"`
	Events   []Type `db:"events"    json:"events"`
	Active   bool   `db:"active"    json:"active"`
}

Webhook registers an external endpoint for event delivery.

type WebhookDelivery

type WebhookDelivery struct {
	ctrlplane.Entity

	WebhookID  id.ID      `db:"webhook_id"  json:"webhook_id"`
	EventID    id.ID      `db:"event_id"    json:"event_id"`
	StatusCode int        `db:"status_code" json:"status_code"`
	Attempts   int        `db:"attempts"    json:"attempts"`
	NextRetry  *time.Time `db:"next_retry"  json:"next_retry,omitempty"`
	Error      string     `db:"error"       json:"error,omitempty"`
}

WebhookDelivery tracks delivery attempts for an event to a webhook.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL