scheduler

package
v0.0.0-...-510734f Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package scheduler runs durable one-off and daily local-time notifications. Storage and delivery are interfaces; the Firestore adapter lives in firestore.

Index

Constants

This section is empty.

Variables

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

Functions

func Key

func Key(uid, id string) string

Types

type Daily

type Daily struct {
	LocalTime int    `json:"localTime" firestore:"localTime"`
	Timezone  string `json:"timezone" firestore:"timezone"`
}

Daily uses HHMM and an IANA timezone. Nonexistent wall times are skipped; repeated wall times fire at the first occurrence only.

func (Daily) Next

func (d Daily) Next(after time.Time) (time.Time, error)

Next returns the first daily occurrence strictly after after. Iterating real instants avoids time.Date's unspecified choice around daylight-saving changes.

func (Daily) Validate

func (d Daily) Validate() error

type Job

type Job struct {
	ID                  string       `firestore:"id"`
	RecipientID         string       `firestore:"recipientId"`
	Daily               *Daily       `firestore:"daily,omitempty"`
	Message             push.Message `firestore:"message"`
	State               State        `firestore:"state"`
	Shard               int          `firestore:"shard"`
	DueAt               time.Time    `firestore:"dueAt"`
	AvailableAt         time.Time    `firestore:"availableAt"`
	LeaseToken          string       `firestore:"leaseToken"`
	Attempts            int          `firestore:"attempts"`
	Completed           []string     `firestore:"completed"`
	InvalidTargets      []string     `firestore:"invalidTargets"`
	LastError           string       `firestore:"lastError"`
	HasPermanentFailure bool         `firestore:"hasPermanentFailure"`
	UpdatedAt           time.Time    `firestore:"updatedAt"`
}

Job is adapter data, not a client-writeable API. AvailableAt is the indexed retry/lease deadline; DueAt remains stable for the current occurrence.

func NewJob

func NewJob(s Schedule, now time.Time) (Job, error)

type Report

type Report struct{ Claimed, Completed, Retrying, Failed int }

type Schedule

type Schedule struct {
	ID          string       `json:"id"`
	RecipientID string       `json:"recipientId"`
	At          time.Time    `json:"at,omitempty"`
	Daily       *Daily       `json:"daily,omitempty"`
	Message     push.Message `json:"message"`
}

func (Schedule) Validate

func (s Schedule) Validate() error

type Sender

type Sender interface {
	Send(context.Context, push.Message, []push.Target) (push.BatchResult, error)
}

type State

type State string
const (
	Ready       State = "ready"
	Done        State = "done"
	Failed      State = "failed"
	Canceled    State = "canceled"
	ShardCount        = 32
	MaxAttempts       = 5
)

type Store

type Store interface {
	Put(context.Context, Job) error
	Get(context.Context, string, string) (Job, error)
	Cancel(context.Context, string, string, time.Time) error
	ClaimDue(context.Context, time.Time, int, int, time.Duration) ([]Job, error)
	Finish(context.Context, Job, Job) error
}

Store must claim and finish with atomic compare-and-set semantics. A stale lease cannot finish, overwrite a replacement, or resurrect a canceled job.

type Targets

type Targets interface {
	ActiveTargets(context.Context, string, time.Time) ([]push.Target, error)
	DisablePushInstallations(context.Context, []push.InstallationRef, time.Time) error
}

type Worker

type Worker struct {
	Store   Store
	Targets Targets
	Sender  Sender
	// Each claim is processed sequentially. Tick claims one job at a time so
	// queued work cannot outlive its lease while waiting behind provider retries.
	Lease       time.Duration
	SendTimeout time.Duration
	Now         func() time.Time
}

func (Worker) Tick

func (w Worker) Tick(ctx context.Context, shard, limit int) (Report, error)

Tick drains at most limit jobs on a shard. Several workers may safely overlap. Limits bound work per invocation; call again while a shard has a backlog.

Directories

Path Synopsis
Package firestore implements durable, sharded scheduler storage on Firestore.
Package firestore implements durable, sharded scheduler storage on Firestore.

Jump to

Keyboard shortcuts

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