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

timer

package
v0.57.0 Latest Latest
Warning

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

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

Documentation

Overview

Package timer is the durable record of one-shot reminders an agent schedules for itself. The server claims the timers whose deadline has passed and delivers each note back into the owning session's loop as a system notice; the rows live in the project database, so a reminder scheduled before a restart still fires after it.

Index

Constants

View Source
const (
	StatusPending   = "pending"
	StatusDue       = "due"
	StatusDelivered = "delivered"
	StatusCancelled = "cancelled"
)

Timer states. pending is waiting for its fire_at; due has been claimed by a claim scan and is waiting to be delivered into its session; delivered has reached the session; cancelled was withdrawn before it fired. delivered and cancelled are terminal.

Variables

View Source
var ErrNotFound = errors.New("timer not found")

ErrNotFound reports that a timer is not there to act on: absent, owned by another session, or already terminal.

Functions

This section is empty.

Types

type Store

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

Store is the project database's set of agent timers. The schema is created by migration 068, which runs when the project database opens.

func Open

func Open(database *db.DB) *Store

Open returns the timer store over the project database.

func (*Store) Cancel

func (s *Store) Cancel(ctx context.Context, sessionID, id string) (Timer, error)

Cancel withdraws one of the session's own timers. Only a pending or due timer can be cancelled: an absent id, another session's id, or an already delivered or cancelled one all come back as ErrNotFound, and a cancelled timer is returned.

func (*Store) ClaimDue

func (s *Store) ClaimDue(ctx context.Context) ([]Timer, error)

ClaimDue moves every pending timer whose deadline has passed to due and returns exactly the rows it moved. The condition lives in the UPDATE, not a read-then-write above it, and RETURNING is part of the same statement: a second claimant's scan touches only rows still pending, so two claimants — two servers or a retry after a crash — can never both return one timer and fire its note twice. Rows already marked due stay out of the claim, so an unreported delivery is handed over by Undelivered instead.

func (*Store) Create

func (s *Store) Create(ctx context.Context, sessionID, note string, fireAt time.Time) (Timer, error)

Create schedules note for delivery into sessionID's loop at fireAt.

func (*Store) List

func (s *Store) List(ctx context.Context, sessionID string) ([]Timer, error)

List returns the session's timers that are still live to fire or to be delivered — pending and due — in fire_at order. Terminal ones are history.

func (*Store) MarkDelivered

func (s *Store) MarkDelivered(ctx context.Context, ids []string) error

MarkDelivered records that these claimed timers reached their sessions. A delivered timer is terminal and never reclaimed. An empty list is a no-op.

func (*Store) Prune

func (s *Store) Prune(ctx context.Context, olderThan time.Time) (int64, error)

Prune deletes the terminal timers — delivered and cancelled — whose updated_at predates olderThan, and returns how many rows went. Pending and due rows are never touched: a due row is still owed to its session (the next turn's catch-up delivers it), so a reminder the agent asked for is never silenced by housekeeping, even when its session can never run again.

func (*Store) Undelivered

func (s *Store) Undelivered(ctx context.Context, sessionID string) ([]Timer, error)

Undelivered returns the session's claimed-but-not-yet-delivered timers: a delivery the server saw through the claim but crashed before reporting is still owed to its session after the restart.

type Timer

type Timer struct {
	ID        string
	SessionID string
	Note      string
	FireAt    time.Time
	Status    string
	CreatedAt time.Time
	UpdatedAt time.Time
}

Timer is one scheduled reminder.

Jump to

Keyboard shortcuts

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