scriptevents

package
v1.170.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 16 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 scriptevents enqueues a run of each automation that names a webhook source for every event the source stores (#2332). The webhook receiver (posted events and form submissions) and the imap poller hand it a source's events once they are durable; each event becomes one run with trigger event, carrying the event as run.event, executed by the run queue's workers under the script's saved author roles, as a scheduled run is.

Nothing runs inline: the sender was answered when the event was stored, and enqueuing is one transaction per script that names the source. Which scripts name a source is read from memory (Bindings), so a source none names costs a delivery no query (#2480). A failure to enqueue is logged, and never fails the delivery, which stored the event whatever happens to its runs.

Index

Constants

View Source
const (
	KeyID         = "id"
	KeyType       = "type"
	KeyKey        = "key"
	KeyReceivedAt = "received_at"
	KeySource     = "source"
	KeyPayload    = "payload"
)

The keys run.event carries.

View Source
const BindingsChannel = "script_event_sources"

BindingsChannel is the NOTIFY channel a change to any script's webhook source is announced on (migration 000230's trigger), which drops every replica's Bindings.

View Source
const DefaultBindingsMaxAge = 5 * time.Second

DefaultBindingsMaxAge is the longest Bindings answers from one read when no change is heard: a NOTIFY sent while a replica's LISTEN connection was down is lost, and this is how long such a replica can miss a new binding. It is the receiver's own source refresh interval.

View Source
const DefaultMaxQueued = 100

DefaultMaxQueued is how many pending event runs a script may hold when scripts.events.max_queued names none; an event past it is recorded as skipped_backlog.

Variables

View Source
var ErrHandlerNotFound = errors.New("no such webhook handler")

ErrHandlerNotFound is a webhook handler that does not exist.

Functions

func Value

func Value(source string, ev whevent.Event) map[string]any

Value is ev as run.event reads it: its id, type, key, when it was received and from which source, and its payload decoded. A payload that is not JSON, which a source never stores, is handed over as its text.

Types

type BindingReader added in v1.153.1

type BindingReader interface {
	EventBindings(ctx context.Context) (map[string][]string, error)
}

BindingReader reads which scripts run for every webhook source (PostgresStore).

type Bindings added in v1.153.1

type Bindings struct {
	Reader BindingReader
	// MaxAge bounds how long one read is used with no change heard; zero is
	// DefaultBindingsMaxAge.
	MaxAge time.Duration
	// contains filtered or unexported fields
}

Bindings is which scripts run for each webhook source, kept in memory so a stored delivery costs no query (#2480). The webhook receiver hands every delivery to the enqueuer inside the sender's request; asking the database each time held the shared pool through a sender's burst even for a source no script names.

The map is read whole, at most once per MaxAge, and again on the next delivery after Notify, which a LISTEN on BindingsChannel calls when any replica changes a script's source. Callers arriving while a read is under way share it, so a burst after a change is one query.

func (*Bindings) For added in v1.153.1

func (b *Bindings) For(ctx context.Context, source string) ([]string, error)

For returns the ids of the scripts that run for source, from the map while it is current and from a fresh read otherwise. A source no script names is an empty list.

func (*Bindings) Notify added in v1.153.1

func (b *Bindings) Notify()

Notify drops the map: the next lookup reads it again. It is the pglisten.Notifier a LISTEN on BindingsChannel wakes.

type Enqueuer

type Enqueuer struct {
	Store Store
	// Bindings names the scripts that run for each source, from memory
	// (#2480).
	Bindings *Bindings
	Scripts  Scripts
	// MaxQueued bounds each script's pending event runs; zero is
	// DefaultMaxQueued.
	MaxQueued int
	// Timeout bounds one source's enqueue; zero is ten seconds.
	Timeout time.Duration
	// Environments resolves the version a handler naming an application
	// environment runs (#2696); nil refuses such a handler's runs.
	Environments appenv.Resolver
}

Enqueuer enqueues the runs of a source's events.

func (*Enqueuer) Stored

func (e *Enqueuer) Stored(ctx context.Context, source string, events []whevent.Event)

Stored enqueues one run of each script that names source for each of events, which the source has made durable. It does not return an error: the events are stored, and what failed is logged.

type EventEnqueue

type EventEnqueue struct {
	Enqueued int
	Skipped  int
	Already  int
}

EventEnqueue is what enqueuing one source's events for one script did (#2332): runs made pending, events recorded as skipped past the bound, and events that already had a run, which made none.

type EventSummary

type EventSummary struct {
	Pending       int        `json:"pending"`
	SkippedSince  int        `json:"skipped_since"`
	LastEventRun  *time.Time `json:"last_event_run,omitempty"`
	LastSkippedAt *time.Time `json:"last_skipped_at,omitempty"`
}

EventSummary is how a script's event runs stand: how many wait, and how many were skipped past the bound since a moment.

type Handler added in v1.161.0

type Handler struct {
	ID       string `json:"id"`
	ScriptID string `json:"script_id"`
	Source   string `json:"event_source"`
	Enabled  bool   `json:"enabled"`
	// Environment is the application environment its runs execute in,
	// empty for the last (#2696).
	Environment string    `json:"environment,omitempty"`
	CreatedBy   string    `json:"created_by"`
	UpdatedBy   string    `json:"updated_by"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

Handler is one webhook handler (#2434): each event the source stores enqueues one run of the script while it is enabled.

type HandlerFilter added in v1.161.0

type HandlerFilter struct {
	ScriptIDs []string
	Source    string
}

HandlerFilter selects webhook handlers. A nil ScriptIDs matches every script and an empty one none; an empty Source matches every source.

type PostgresStore

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

PostgresStore keeps event runs in script_runs and webhook handlers in script_webhook_handlers (#2332, #2434).

func NewStore

func NewStore(db *sql.DB) *PostgresStore

NewStore is the PostgreSQL event run store.

func (*PostgresStore) AddHandler added in v1.161.0

func (s *PostgresStore) AddHandler(ctx context.Context, scriptID, source, actor string) (h Handler, created bool, err error)

AddHandler binds a script to a source, or answers the handler that already binds it, re-enabled when it was off. created reports a new handler.

func (*PostgresStore) DeleteHandler added in v1.161.0

func (s *PostgresStore) DeleteHandler(ctx context.Context, id string) error

DeleteHandler removes one webhook handler. The runs its events made keep their source and event.

func (*PostgresStore) EnableHandler added in v1.161.0

func (s *PostgresStore) EnableHandler(ctx context.Context, id string, enabled bool, actor string) error

EnableHandler turns one webhook handler on or off.

func (*PostgresStore) EnqueueEvents

func (s *PostgresStore) EnqueueEvents(ctx context.Context, runs []*script.Run, maxQueued int) (EventEnqueue, error)

EnqueueEvents writes one run per event for the script the runs name, in one transaction holding the script's row: each is pending while the handler's pending event runs (the script's, for the source) are under maxQueued, and recorded as skipped_backlog past it. An event that already has a run makes none. A worker is woken when any run was made pending. Every run carries the same script, version and source; its Event and EventID name the event.

func (*PostgresStore) EventBindings added in v1.153.1

func (s *PostgresStore) EventBindings(ctx context.Context) (map[string][]string, error)

EventBindings returns the ids of the scripts that run for each webhook source a script names; a source none names is absent.

func (*PostgresStore) EventSummary

func (s *PostgresStore) EventSummary(ctx context.Context, scriptID string, since time.Time) (EventSummary, error)

EventSummary reports how the script's event runs stand, counting the ones skipped since since.

func (*PostgresStore) HandlerByID added in v1.161.0

func (s *PostgresStore) HandlerByID(ctx context.Context, id string) (Handler, error)

HandlerByID returns one webhook handler.

func (*PostgresStore) HandlerEnvironment added in v1.164.0

func (s *PostgresStore) HandlerEnvironment(ctx context.Context, scriptID, source string) (string, error)

HandlerEnvironment is the environment the script's handler for source names, "" for the last or for no handler.

func (*PostgresStore) Handlers added in v1.161.0

func (s *PostgresStore) Handlers(ctx context.Context, filter HandlerFilter) ([]Handler, error)

Handlers lists the webhook handlers a filter matches, by source.

func (*PostgresStore) SetHandlerEnvironment added in v1.164.0

func (s *PostgresStore) SetHandlerEnvironment(ctx context.Context, id, env, actor string) error

SetHandlerEnvironment changes the environment a webhook handler's runs execute in (#2696).

type Scripts

type Scripts interface {
	GetByID(ctx context.Context, id string) (*script.Script, error)
	GetVersion(ctx context.Context, scriptID string, version int) (*script.Version, error)
}

Scripts reads a script and the version a run of it executes (scriptstore.Store).

type Store

type Store interface {
	EnqueueEvents(ctx context.Context, runs []*script.Run, maxQueued int) (EventEnqueue, error)
}

Store enqueues the runs of a source's events (PostgresStore).

Directories

Path Synopsis
Package sourcerule is who may run a managed script for which webhook source (#2332).
Package sourcerule is who may run a managed script for which webhook source (#2332).

Jump to

Keyboard shortcuts

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