schedule

package
v0.54.8 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package schedule provides in-process delayed, periodic, and idle triggers for agent hosts, such as chat-idle context cleanup and future self-triggering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
	AfterFunc(d time.Duration, f func()) Timer
}

Clock is the time source; tests inject a fake one.

type Event

type Event struct {
	Name     string
	Kind     EventKind
	Err      error
	Duration time.Duration
	At       time.Time
}

Event is passed to the observer set by WithObserver.

type EventKind

type EventKind string

EventKind names a scheduler event.

const (
	Scheduled EventKind = "scheduled"
	Fired     EventKind = "fired"
	Skipped   EventKind = "skipped"
	Cancelled EventKind = "cancelled"
	Paused    EventKind = "paused"
	Resumed   EventKind = "resumed"
	Done      EventKind = "done"
	Failed    EventKind = "failed"
)

type Func

type Func func(ctx context.Context) error

Func is called with a context cancelled when its task is cancelled or the scheduler closes.

type Handle

type Handle interface {
	Cancel() // unregister the task; a running call sees its ctx cancelled
	Reset()  // restart the timer from now
	Pause()  // stop the timer until Resume
	Resume() // restart the timer from now
}

Handle controls one registered task.

type Option

type Option func(*scheduler)

Option configures New.

func WithClock

func WithClock(clock Clock) Option

WithClock replaces the real clock (nil keeps it).

func WithObserver

func WithObserver(observer func(Event)) Option

WithObserver sets the event callback, called outside the scheduler lock. Events of one run may arrive out of order ("done" before "fired" for a very short run); order by Event.At when it matters.

type Scheduler

type Scheduler interface {
	After(name string, d time.Duration, fn Func) Handle // fire once after d, then unregister
	Every(name string, d time.Duration, fn Func) Handle // fire every d
	Idle(name string, d time.Duration, fn Func) Handle  // fire once d after the last Touch, then wait for the next Touch
	Touch()                                             // activity: restart every Idle timer
	Close() error                                       // cancel all tasks and wait for running ones
}

Scheduler runs named delayed, periodic and idle tasks in-process. Registering a name again replaces the old task; one task never runs concurrently with itself; observers are called outside the scheduler lock, so they may call back into the scheduler.

func New

func New(opts ...Option) Scheduler

New returns a Scheduler using the real clock unless WithClock is given.

type Timer

type Timer interface{ Stop() bool }

Timer is the part of *time.Timer the scheduler uses.

Jump to

Keyboard shortcuts

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