usage

package
v2.33.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AISeatsInterval = 4 * time.Hour
)

Variables

This section is empty.

Functions

func NewDBInserter

func NewDBInserter(opts ...InserterOption) agplusage.Inserter

NewDBInserter creates a new database-backed usage event inserter.

Types

type Cron added in v2.32.0

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

Cron runs registered CronJobs on the dbInserter's clock. Stopping the context passed to Start cancels all jobs. Daemon restarts naturally restart the timers since Start() creates them fresh — there is no state to persist or recover.

func NewCron added in v2.32.0

func NewCron(clock quartz.Clock, log slog.Logger, db database.Store, ins agplusage.Inserter) *Cron

NewCron creates a Cron that periodically generates and inserts heartbeat events. The clock controls all timers so that tests can advance time deterministically via quartz.Mock.

func (*Cron) Close added in v2.32.0

func (c *Cron) Close() error

Close cancels all jobs and waits for goroutines to exit.

func (*Cron) Register added in v2.32.0

func (c *Cron) Register(job CronJob) error

Register adds a job. It must be called before Start; calling it after Start returns an error.

func (*Cron) Start added in v2.32.0

func (c *Cron) Start(ctx context.Context)

Start launches a goroutine per job. Subsequent calls are no-ops. On daemon restart a new Cron should be created.

type CronJob added in v2.32.0

type CronJob struct {
	// Name is a human-readable label used in logs.
	Name string
	// Interval is the base duration between ticks.
	Interval time.Duration
	// EventType must match the events generated by the Fn.
	EventType usagetypes.UsageEventType
	// Jitter is the maximum random delay added after the boundary.
	// The actual offset is uniformly distributed in [0, Jitter).
	// This staggers replicas so one is likely to complete the work
	// before others attempt it, allowing them to skip via the
	// existence check (heartbeat inserts are idempotent).
	Jitter time.Duration
	// Fn produces the heartbeat event.
	Fn HeartbeatFunc
}

CronJob defines a periodic heartbeat job.

type HeartbeatFunc added in v2.32.0

type HeartbeatFunc func(ctx context.Context) (event usagetypes.HeartbeatEvent, err error)

HeartbeatFunc generates a heartbeat event and its stable ID. It is called periodically by the cron. Returning an error skips the insert for that tick and logs a warning.

func AISeatsHeartbeat added in v2.32.0

func AISeatsHeartbeat(db database.Store) HeartbeatFunc

AISeatsHeartbeat returns a HeartbeatFunc that queries the active AI seat count and emits it as an HBAISeats heartbeat event.

type InserterOption

type InserterOption func(*dbInserter)

func InserterWithClock

func InserterWithClock(clock quartz.Clock) InserterOption

InserterWithClock sets the quartz clock to use for the inserter.

type Publisher

type Publisher interface {
	// Close closes the publisher and waits for it to finish.
	io.Closer
	// Start starts the publisher. It must only be called once.
	Start() error
}

Publisher publishes usage events ***somewhere***.

func NewTallymanPublisher

func NewTallymanPublisher(ctx context.Context, log slog.Logger, db database.Store, keys map[string]ed25519.PublicKey, opts ...TallymanPublisherOption) Publisher

NewTallymanPublisher creates a Publisher that publishes usage events to Coder's Tallyman service.

type TallymanPublisherOption

type TallymanPublisherOption func(*tallymanPublisher)

func PublisherWithClock

func PublisherWithClock(clock quartz.Clock) TallymanPublisherOption

PublisherWithClock sets the clock to use for publishing usage events.

func PublisherWithHTTPClient

func PublisherWithHTTPClient(httpClient *http.Client) TallymanPublisherOption

PublisherWithHTTPClient sets the HTTP client to use for publishing usage events.

func PublisherWithIngestURL

func PublisherWithIngestURL(ingestURL string) TallymanPublisherOption

PublisherWithIngestURL sets the ingest URL to use for publishing usage events.

func PublisherWithInitialDelay

func PublisherWithInitialDelay(initialDelay time.Duration) TallymanPublisherOption

PublisherWithInitialDelay sets the initial delay for the publisher.

Jump to

Keyboard shortcuts

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