Help us shape the future of Go! Take the 2026 Go Developer Survey

workers

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package workers provides explicit, cancellable worker execution. Constructors never start goroutines. Hosts own pools, tenant configuration, and shutdown.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Archive

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

func NewArchive

func NewArchive(source model.ArchiveSource, store model.ArchiveStore, signer model.AuditSigner, keys map[string]ed25519.PublicKey, auth model.Authorizer, c model.Caller, stream string) (*Archive, error)

func NewArchiveWithTelemetry

func NewArchiveWithTelemetry(source model.ArchiveSource, store model.ArchiveStore, signer model.AuditSigner, keys map[string]ed25519.PublicKey, auth model.Authorizer, c model.Caller, stream string, telemetry model.TelemetryOptions) (*Archive, error)

NewArchiveWithTelemetry uses host-owned providers without installing globals.

func (*Archive) Handle

func (a *Archive) Handle(ctx context.Context, d model.Delivery) (err error)

type AuditExport

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

AuditExport consumes trusted durable ledger events, never client-supplied notifications. The host worker identity is separate from the original requester.

func NewAuditExport

func NewAuditExport(runner model.AuditExportRunner, auth model.Authorizer, caller model.Caller, stream string) (*AuditExport, error)

func (*AuditExport) Handle

func (a *AuditExport) Handle(ctx context.Context, d model.Delivery) error

type EvidenceDisposalHandler

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

EvidenceDisposalHandler performs post-commit deletion from an external evidence store. The outbox retries partial family deletion, so the store's exact-version deletion operation must be idempotent.

func NewEvidenceDisposalHandler

func NewEvidenceDisposalHandler(source model.ArchiveSource, store model.EvidenceVersionDeleter, tenant string) (*EvidenceDisposalHandler, error)

func NewEvidenceDisposalHandlerWithTelemetry

func NewEvidenceDisposalHandlerWithTelemetry(source model.ArchiveSource, store model.EvidenceVersionDeleter, tenant string, telemetry model.TelemetryOptions) (*EvidenceDisposalHandler, error)

func (*EvidenceDisposalHandler) Handle

func (h *EvidenceDisposalHandler) Handle(ctx context.Context, d model.Delivery) (err error)

type Extraction

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

Extraction routes durable job notifications through the engine. Job claims, authorization, provider calls and proposal commits remain engine-owned.

func NewExtraction

func NewExtraction(runner model.ExtractionRunner, caller model.Caller, stream string) (*Extraction, error)

func (*Extraction) Handle

func (e *Extraction) Handle(ctx context.Context, d model.Delivery) error

type Handler

type Handler interface {
	Handle(context.Context, model.Delivery) error
}

type HandlerFunc

type HandlerFunc func(context.Context, model.Delivery) error

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, d model.Delivery) error

type MemoryRefresh

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

MemoryRefresh schedules durable jobs; it never invokes a model. Use a separate extraction consumer to execute them, with independently visible progress.

func NewMemoryRefresh

func NewMemoryRefresh(s model.MemoryRefreshScheduler, c model.Caller, stream, purpose string, bindings []model.MemoryRefreshBinding) (*MemoryRefresh, error)

func (*MemoryRefresh) Handle

func (w *MemoryRefresh) Handle(ctx context.Context, d model.Delivery) error

type MemoryRefreshTicker

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

MemoryRefreshTicker emits durable sweep events. It starts no goroutines; Run is explicitly supervised by the host. A tick never calls a model.

func NewMemoryRefreshTicker

func NewMemoryRefreshTicker(s model.MemoryRefreshSweepStarter, c model.Caller, stream, purpose string, bindings []model.MemoryRefreshBinding, interval time.Duration) (*MemoryRefreshTicker, error)

func (*MemoryRefreshTicker) Run

func (*MemoryRefreshTicker) Tick

type Options

type Options struct {
	Lease, PollInterval, MinBackoff, MaxBackoff time.Duration
	Telemetry                                   model.TelemetryOptions
}

type OrphanCleanup

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

func (*OrphanCleanup) Tick

Tick is bounded by pages per binding and the caller's deadline. A process restart scans again; stable object-specific command keys preserve receipts.

type OrphanCleanupBinding

type OrphanCleanupBinding struct {
	Scope        string `json:"scope"`
	PolicyID     string `json:"policy_id"`
	Jurisdiction string `json:"jurisdiction"`
}

type OrphanCleanupConfig

type OrphanCleanupConfig struct {
	Tenant          string                 `json:"tenant"`
	Stream          string                 `json:"stream"`
	Purpose         string                 `json:"purpose"`
	Reason          string                 `json:"reason"`
	Bindings        []OrphanCleanupBinding `json:"bindings"`
	PageSize        int                    `json:"page_size"`
	MaxPages        int                    `json:"max_pages"`
	IntervalMinutes int                    `json:"interval_minutes"`
}

func (*OrphanCleanupConfig) Validate

func (c *OrphanCleanupConfig) Validate() error

type OrphanCleanupResult

type OrphanCleanupResult struct{ Scanned, Disposed, Skipped int }

type ProgressMonitor

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

ProgressMonitor aggregates configured consumers by kind during SDK collection. Use one monitor per meter provider to avoid overlapping gauge series. Close unregisters the callback; the host owns the reader and meter provider.

func NewProgressMonitor

func NewProgressMonitor(reader model.OutboxProgressReader, targets []ProgressTarget, provider metric.MeterProvider, timeout time.Duration) (*ProgressMonitor, error)

func (*ProgressMonitor) Close

func (m *ProgressMonitor) Close() error

type ProgressTarget

type ProgressTarget struct {
	Key  model.ConsumerKey
	Kind string
}

ProgressTarget identifies a trusted, host-configured consumer. Kind is one of projection, archive, audit, extraction or refresh, never an arbitrary label.

type Projection

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

func NewProjection

func NewProjection(source model.ProjectionSource, index model.RetrievalIndex, auth model.Authorizer, caller model.Caller, generation string) (*Projection, error)
Example
package main

import (
	"context"
	"github.com/caxqueiroz/facts_engine/model"
	"github.com/caxqueiroz/facts_engine/workers"
	"time"
)

// runProjection demonstrates host-owned lifecycle and injected boundaries.
// RegisterConsumer and Run are explicit; the host closes pools after Run returns.
func runProjection(ctx context.Context, queue model.Outbox, source model.ProjectionSource, index model.RetrievalIndex, auth model.Authorizer, caller model.Caller, generation string) error {
	handler, err := workers.NewProjection(source, index, auth, caller, generation)
	if err != nil {
		return err
	}
	key := model.ConsumerKey{Tenant: caller.Tenant, Consumer: model.ProjectionConsumer(generation)}
	if err := queue.RegisterConsumer(ctx, key, 8); err != nil {
		return err
	}
	worker, err := workers.NewOutbox(queue, key, handler, workers.Options{Lease: time.Minute, PollInterval: time.Second, MinBackoff: time.Second, MaxBackoff: time.Minute})
	if err != nil {
		return err
	}
	return worker.Run(ctx)
}
func main() {
	// The production host supplies a role-restricted ledger, a tenant-bound CortexDB
	// adapter, OpenFGA, an authenticated worker caller and a cancellable context.
	_ = runProjection
}

func NewProjectionWithTelemetry

func NewProjectionWithTelemetry(source model.ProjectionSource, index model.RetrievalIndex, auth model.Authorizer, caller model.Caller, generation string, telemetry model.TelemetryOptions) (*Projection, error)

NewProjectionWithTelemetry uses host-owned providers without installing globals.

func (*Projection) Handle

func (p *Projection) Handle(ctx context.Context, d model.Delivery) (err error)

type ProjectionCleanup

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

func (*ProjectionCleanup) Tick

Tick advances a bounded in-memory cursor, including past held/frozen targets, so a long-lived worker eventually visits later pages. At EOF it starts a new scan on the next tick. Restarts safely rescan with stable command identities.

type ProjectionCleanupConfig

type ProjectionCleanupConfig struct {
	Tenant          string `json:"tenant"`
	Stream          string `json:"stream"`
	Reason          string `json:"reason"`
	PageSize        int    `json:"page_size"`
	MaxPages        int    `json:"max_pages"`
	IntervalMinutes int    `json:"interval_minutes"`
}

func (*ProjectionCleanupConfig) Validate

func (c *ProjectionCleanupConfig) Validate() error

type ProjectionCleanupResult

type ProjectionCleanupResult struct{ Scanned, Completed, Skipped int }

type Rebuild

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

Rebuild coordinates durable ledger states and fenced destination gates. Hosts authorize operators and provision destination databases separately.

func NewRebuild

func NewRebuild(store model.RebuildStore, index model.RebuildIndex, actor model.RebuildActor) (*Rebuild, error)

func (*Rebuild) Abandon

func (r *Rebuild) Abandon(ctx context.Context, k model.GenerationKey, fence int64) (model.RebuildStatus, error)

Abandon fences an unfinished candidate without deleting any database or data.

func (*Rebuild) Activate

func (r *Rebuild) Activate(ctx context.Context, k model.GenerationKey, verifiedFence int64) (model.RebuildStatus, error)

func (*Rebuild) Resume

func (r *Rebuild) Resume(ctx context.Context, k model.GenerationKey, fence int64) (model.RebuildStatus, error)

func (*Rebuild) Start

func (r *Rebuild) Start(ctx context.Context, k model.GenerationKey, attempts int) (model.RebuildStatus, error)

func (*Rebuild) Verify

type Worker

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

func NewOutbox

func NewOutbox(queue model.Outbox, key model.ConsumerKey, handler Handler, o Options) (*Worker, error)

func (*Worker) ProcessOne

func (w *Worker) ProcessOne(ctx context.Context) (bool, error)

ProcessOne handles at most one event. False means no event was claimable. A handler failure is durably recorded and may schedule a retry or exhaust attempts; database/lease errors return to the host. Handlers must honor cancellation and deduplicate external effects.

func (*Worker) Run

func (w *Worker) Run(ctx context.Context) error

Run polls until cancelled. Database errors return to the host supervisor; failed consumers remain visible and require an explicit operator retry.

Jump to

Keyboard shortcuts

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