completer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package completer batches job state updates and heartbeats. Results are written by size (100) or interval (50 ms) in single statements, retried with backoff on database errors, never dropped, and flushed on shutdown.

c, err := completer.New(completer.Config{ClientID: id, Store: store, Clock: clk})
go c.Run(ctx)
c.Add(completion)

Index

Constants

View Source
const (
	DefaultBatchSize     = 100
	DefaultFlushInterval = 50 * time.Millisecond
	DefaultBufferSize    = 10_000
	DefaultMaxRetryDelay = 5 * time.Second
	DefaultFlushTimeout  = 10 * time.Second
	DefaultCallTimeout   = 30 * time.Second
)

Defaults.

Variables

This section is empty.

Functions

This section is empty.

Types

type Completer

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

Completer batches job state updates (by size or interval) into single statements. Results are never dropped: on a database error they stay in memory and the write is retried with jittered exponential backoff. The buffer is unbounded in memory but producers stop fetching while Full reports true, which bounds it to BufferSize plus running jobs.

func New

func New(cfg Config) (*Completer, error)

New returns a Completer; ClientID, Store and Clock are required.

func (*Completer) Add

func (c *Completer) Add(comp job.Completion)

Add queues one result. It never blocks and never drops.

func (*Completer) Full

func (c *Completer) Full() bool

Full reports whether producers must stop fetching until the buffer drains.

func (*Completer) Len

func (c *Completer) Len() int

Len returns the number of results waiting to be written.

func (*Completer) Run

func (c *Completer) Run(ctx context.Context) error

Run writes results until ctx is cancelled, then performs a final flush bounded by FlushTimeout. It returns an error only if results remain unwritten after the final flush; those jobs stay running and are rescued.

type Config

type Config struct {
	ClientID string
	Store    job.CompletionWriter
	Clock    clock.Clock
	Logger   *slog.Logger
	// BatchSize is the maximum number of results per statement.
	BatchSize int
	// FlushInterval is the longest a result waits before being written.
	FlushInterval time.Duration
	// BufferSize is the number of pending results at which Full reports true.
	BufferSize int
	// MaxRetryDelay caps the backoff between failed writes.
	MaxRetryDelay time.Duration
	// FlushTimeout bounds the final flush on shutdown.
	FlushTimeout time.Duration
	// CallTimeout bounds one write.
	CallTimeout time.Duration
	// OnApplied receives the completions that passed the guard and the number rejected as stale.
	OnApplied func(applied []job.Completion, stale int)
	// OnDrained is called when the buffer stops being full.
	OnDrained func()
	// Rand returns a float in [0, 1) for retry jitter; nil uses math/rand/v2.
	Rand func() float64
}

Config configures a Completer.

type HeartbeatConfig

type HeartbeatConfig struct {
	ClientID string
	Store    job.HeartbeatWriter
	Clock    clock.Clock
	Logger   *slog.Logger
	// Interval between heartbeats (Config.HeartbeatInterval).
	Interval time.Duration
	// Running lists the attempts this client is executing.
	Running func() []job.Ref
}

HeartbeatConfig configures a Heartbeater.

type Heartbeater

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

Heartbeater refreshes heartbeat_at of this client's running jobs in batches every Interval. The completion guard applies.

func NewHeartbeater

func NewHeartbeater(cfg HeartbeatConfig) (*Heartbeater, error)

NewHeartbeater validates cfg.

func (*Heartbeater) Beat

func (h *Heartbeater) Beat(ctx context.Context, ref job.Ref) error

Beat refreshes the heartbeat of one attempt immediately (foreman.Heartbeat).

func (*Heartbeater) Run

func (h *Heartbeater) Run(ctx context.Context) error

Run beats every Interval until ctx is cancelled.

Jump to

Keyboard shortcuts

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