lifecycle

package
v0.0.1-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package lifecycle provides a shared, testable scheduler for async state transitions. EC2 instances, ECS tasks, and RDS instances all need delayed state changes (e.g. pending → running, creating → available). This package provides a single abstraction instead of each service hand-rolling time.AfterFunc/goroutine/cancel patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scheduler

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

Scheduler manages keyed delayed callbacks. Each service creates its own Scheduler instance (no global state, DI-friendly).

func NewScheduler

func NewScheduler(clk clock.Clock) *Scheduler

NewScheduler creates a Scheduler using the given clock. Production: clock.New(). Tests: clock.NewMock() for instant time skips.

func (*Scheduler) After

func (s *Scheduler) After(key string, delay time.Duration, fn func())

After schedules fn to run after delay. Key identifies the transition (e.g. "i-abc123:terminate"). If a transition with the same key is already pending, it is cancelled before the new one is scheduled.

When delay is 0 and the clock is a real clock (not a mock), fn is executed synchronously within this call. This ensures that subsequent API calls immediately see the updated state instead of racing with a goroutine. With a mock clock, 0-delay timers remain pending until clock.Add is called, preserving test-time control.

func (*Scheduler) Cancel

func (s *Scheduler) Cancel(key string) bool

Cancel cancels a pending transition by key. Returns true if a pending transition was found and cancelled.

func (*Scheduler) PendingCount

func (s *Scheduler) PendingCount() int

PendingCount returns the number of currently scheduled transitions.

func (*Scheduler) Stop

func (s *Scheduler) Stop(ctx context.Context)

Stop cancels all pending transitions and waits for any in-flight callbacks to complete. Respects ctx for timeout on the wait.

Jump to

Keyboard shortcuts

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