automations

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 10 Imported by: 0 modules, 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 automations owns Automations: saved schedules that deliver a target Session create or instructions, and the invocation records of each delivery. This package holds the rules; the PostgreSQL adapter stores them and the API package runs the deliveries.

Index

Constants

View Source
const (
	Active    = "active"
	Paused    = "paused"
	Completed = "completed"

	Periodic = "periodic"
	Once     = "once"
)

Automation statuses and types.

Variables

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

ErrNotFound reports an Automation, an invocation, or a list cursor that names none of the tenant's. Missing, malformed and foreign IDs are indistinguishable.

Functions

func RunAt

func RunAt(unix int64) (time.Time, error)

RunAt converts a Unix timestamp in seconds, which must not pass year 9999.

Types

type Automation

type Automation struct {
	ID, TenantID string
	Type         string
	Name         string
	Metadata     map[string]string
	Status       string
	RunAt        *time.Time // once
	Expression   string     // periodic
	Timezone     string     // periodic
	// NextRunAt is the next occurrence to record while the Automation is active.
	NextRunAt  *time.Time
	TargetType string
	Target     json.RawMessage
	Contents   json.RawMessage
	CreatedAt  time.Time
	// Pending and Queued report a queued scheduled invocation and a queued
	// invocation of any source.
	Pending, Queued bool
}

Automation is a saved schedule. Target is the safe projection of its target; Contents is the target request, set only on the way in, and the storage seals it.

func (Automation) Apply

func (a Automation) Apply(p Patch, now time.Time) (Automation, error)

Apply applies an update. Timing fields must match the stored type, run_at must be in the future and cannot change once the scheduled invocation exists, and an active Automation recomputes its next occurrence.

func (Automation) Occurrence

func (a Automation) Occurrence(now time.Time) Occurrence

Occurrence decides the invocation of an active Automation whose NextRunAt is due. After downtime a periodic Automation records only its latest due occurrence.

func (Automation) Pause

func (a Automation) Pause() (Automation, error)

Pause stops scheduled delivery. A paused Automation stays paused.

func (Automation) Resource

func (a Automation) Resource() v1.Automation

Resource projects the Automation onto its public resource.

func (Automation) Resume

func (a Automation) Resume(now time.Time) (Automation, error)

Resume restarts scheduled delivery. A one-time Automation without a pending scheduled invocation needs a future run_at. An active Automation is unchanged.

type ConflictError

type ConflictError struct{ Message string }

ConflictError reports an operation the Automation's state does not allow.

func (*ConflictError) Error

func (e *ConflictError) Error() string

type Delivery

type Delivery struct {
	InvocationID, TargetType string
	Scope                    identity.Principal
	Request                  json.RawMessage
}

Delivery is a queued invocation ready to deliver, with the Project that owns the Automation and the opened target request.

type InvalidError

type InvalidError struct{ Param, Message string }

InvalidError reports a request value that breaks a rule, with the member that holds it.

func (*InvalidError) Error

func (e *InvalidError) Error() string

type Invocation

type Invocation struct {
	ID, AutomationID string
	CreatedAt        time.Time
	ScheduledAt      *time.Time
	// Snapshot is the Automation resource at the occurrence; nil for a manual
	// invocation.
	Snapshot                                     json.RawMessage
	Status, SkipReason, SessionID, Code, Message string
}

Invocation is the delivery record of one occurrence or manual invocation.

func (Invocation) Resource

func (i Invocation) Resource() v1.AutomationInvocation

Resource projects the invocation onto its public resource.

type InvocationPage

type InvocationPage struct {
	Invocations []Invocation
	HasMore     bool
}

InvocationPage is a page of invocations.

type ListQuery

type ListQuery struct {
	After     string
	Limit     int
	Ascending bool
	SessionID *string
}

ListQuery selects a page. SessionID filters Automations that target a Session.

type Occurrence

type Occurrence struct {
	ScheduledAt time.Time
	// Skipped reports an occurrence recorded as skipped because an earlier
	// invocation is still queued.
	Skipped bool
	// Next is the Automation's next occurrence; nil for none.
	Next *time.Time
	// Completed reports a one-time Automation whose schedule has finished.
	Completed bool
}

Occurrence is what a due Automation records.

type Outcome

type Outcome struct {
	InvocationID, Status, SessionID, Code, Message string
}

Outcome settles a queued invocation. Status is completed or failed.

type Page

type Page struct {
	Automations []Automation
	HasMore     bool
}

Page is a page of Automations.

type Patch

type Patch struct {
	Name                 *string
	Metadata             map[string]string
	TargetType           string
	Target, Contents     json.RawMessage
	Expression, Timezone *string
	RunAt                *time.Time
}

Patch is an update. Nil members keep the stored value. A target replaces the whole target and keeps its type.

type Reader

type Reader interface {
	Get(ctx context.Context, tenantID, id string) (Automation, error)
	List(ctx context.Context, tenantID string, query ListQuery) (Page, error)
	GetInvocation(ctx context.Context, tenantID, id, invocationID string) (Invocation, error)
	ListInvocations(ctx context.Context, tenantID, id string, query ListQuery) (InvocationPage, error)
}

Reader reads Automations and their invocations. A list whose After names nothing returns ErrNotFound, and so does one of a missing Automation.

type Schedule

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

Schedule is a parsed five-field crontab(5) expression in a time zone.

func ParseSchedule

func ParseSchedule(expression, timezone string) (*Schedule, error)

ParseSchedule parses expression, which is minute, hour, day of month, month and day of week. Each field is "*" or a comma list of n, a-b, */s or a-b/s; months and weekdays also take three-letter names, and weekday 7 is Sunday. It rejects descriptors, seconds and an expression with no occurrence in the next five years. timezone is an IANA name.

func (*Schedule) Next

func (s *Schedule) Next(after time.Time) (time.Time, bool)

Next returns the first occurrence after the given time, or false when there is none in the next five years. It follows crontab(5) around daylight saving changes. A fixed-time Schedule, whose minute and hour fields do not start with "*", runs a wall-clock time in a gap at the first instant after the gap and a repeated one only at its first instant. Any other Schedule runs at every instant whose wall-clock time matches: never in a gap, on both passes of a repeated hour.

type Service

type Service struct{ Storage }

Service runs the Automation use cases; the storage runs the rest.

func NewService

func NewService(storage Storage) (*Service, error)

func (*Service) Create

func (s *Service) Create(ctx context.Context, a Automation) (Automation, error)

Create saves an active Automation without invoking it.

func (*Service) Pause

func (s *Service) Pause(ctx context.Context, tenantID, id string) (Automation, error)

func (*Service) Resume

func (s *Service) Resume(ctx context.Context, tenantID, id string) (Automation, error)

func (*Service) Update

func (s *Service) Update(ctx context.Context, tenantID, id string, p Patch) (Automation, error)

type Storage

type Storage interface {
	Create(ctx context.Context, a Automation) (Automation, error)
	// Update locks the Automation, applies change to it and saves the result.
	Update(ctx context.Context, tenantID, id string, change func(Automation) (Automation, error)) (Automation, error)
	// Delete removes the Automation and its invocations.
	Delete(ctx context.Context, tenantID, id string) (string, error)
	// Invoke queues a manual invocation of the Automation's current target.
	Invoke(ctx context.Context, tenantID, id string) (Invocation, error)
	// Record records the due occurrences of active Automations, each in its own
	// transaction, and returns how many it recorded. A failing Automation does
	// not stop the others; the error joins the failures.
	Record(ctx context.Context, now time.Time) (int, error)
	// Deliverable returns at most limit queued invocations to deliver: for each
	// Automation, its oldest queued invocation that is manual or whose
	// Automation is active, least recently attempted first. It marks them
	// attempted. An invocation whose target cannot be opened is settled failed.
	Deliverable(ctx context.Context, limit int) ([]Delivery, error)
	// Settle settles a queued invocation and completes its one-time Automation
	// when the invocation is scheduled. An invocation that is no longer queued
	// or no longer exists is left alone.
	Settle(ctx context.Context, outcome Outcome) error
}

Storage saves Automations. Each write runs in one transaction that also records the write audit. A method returns ErrNotFound for an Automation the tenant does not own.

Jump to

Keyboard shortcuts

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