Files
sam 682995bdf7 feat(runs): workflow drafts, publish with an expected version, delete, enabled flag, run on demand, run stages and statistics (#2061, #2113, #2098)
A disabled workflow is never started by a trigger, another workflow or a
person. Delete is refused while a trigger, another workflow or a run in
progress names it. A parked or ended run restarts from a step.
2026-11-22 11:34:49 +11:00

244 lines
9.1 KiB
Go

package store
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
var (
// ErrNoWorkflow means no workflow is stored under that name and version.
ErrNoWorkflow = errors.New("no such workflow")
// ErrVersionConflict means the workflow's latest version is not the one
// the writer expected.
ErrVersionConflict = errors.New("the workflow has another version than expected")
// ErrDraftConflict means the stored draft is not the revision the writer
// last saw.
ErrDraftConflict = errors.New("the draft changed since it was read")
// ErrNoDraft means the workflow has no draft.
ErrNoDraft = errors.New("no such draft")
)
// Workflow is one stored version of a workflow document.
type Workflow struct {
Name string `db:"name"`
Version int `db:"version"`
Body string `db:"body"`
CreatedBy string `db:"created_by"`
CreatedAt time.Time `db:"created_at"`
// Disabled is a workflow nothing may start. The zero value is enabled.
Disabled bool `db:"disabled"`
}
const workflowColumns = `w.name, w.version, w.body, w.created_by, w.created_at, COALESCE(NOT s.enabled, false) AS disabled`
// Workflow returns a version of a workflow, or its latest when version is zero.
func (s *Store) Workflow(ctx context.Context, orgID, name string, version int) (Workflow, error) {
var w Workflow
err := s.db.GetContext(ctx, &w, `
SELECT `+workflowColumns+` FROM runs.workflows w
LEFT JOIN runs.workflow_settings s ON s.org_id = w.org_id AND s.name = w.name
WHERE w.org_id = $1 AND w.name = $2 AND ($3 = 0 OR w.version = $3)
ORDER BY w.version DESC LIMIT 1`, orgID, name, version)
if errors.Is(err, sql.ErrNoRows) {
return Workflow{}, ErrNoWorkflow
}
if err != nil {
return Workflow{}, fmt.Errorf("read workflow: %w", err)
}
return w, nil
}
// Workflows returns the latest version of every workflow.
func (s *Store) Workflows(ctx context.Context, orgID string) ([]Workflow, error) {
var out []Workflow
if err := s.db.SelectContext(ctx, &out, `
SELECT DISTINCT ON (w.name) `+workflowColumns+` FROM runs.workflows w
LEFT JOIN runs.workflow_settings s ON s.org_id = w.org_id AND s.name = w.name
WHERE w.org_id = $1 ORDER BY w.name, w.version DESC`, orgID); err != nil {
return nil, fmt.Errorf("list workflows: %w", err)
}
return out, nil
}
// AddWorkflow stores body as the next version of a workflow and returns it.
// When expected is given, the workflow's latest version must be that one (zero
// for a workflow with none), or nothing is stored.
func (s *Store) AddWorkflow(ctx context.Context, orgID, name, body, by string, expected *int) (Workflow, error) {
var want sql.NullInt64
if expected != nil {
want = sql.NullInt64{Int64: int64(*expected), Valid: true}
}
var w Workflow
err := s.db.GetContext(ctx, &w, `
INSERT INTO runs.workflows (org_id, name, version, body, created_by)
SELECT $1, $2, COALESCE(max(version), 0) + 1, $3, $4 FROM runs.workflows WHERE org_id = $1 AND name = $2
HAVING $5::bigint IS NULL OR COALESCE(max(version), 0) = $5
RETURNING name, version, body, created_by, created_at, false AS disabled`, orgID, name, body, by, want)
if errors.Is(err, sql.ErrNoRows) {
return Workflow{}, ErrVersionConflict
}
if err != nil {
return Workflow{}, fmt.Errorf("store workflow: %w", err)
}
return w, nil
}
// SetEnabled enables or disables a stored workflow.
func (s *Store) SetEnabled(ctx context.Context, orgID, name string, enabled bool) error {
res, err := s.db.ExecContext(ctx, `
INSERT INTO runs.workflow_settings (org_id, name, enabled)
SELECT $1, $2, $3 WHERE EXISTS (SELECT 1 FROM runs.workflows WHERE org_id = $1 AND name = $2)
ON CONFLICT (org_id, name) DO UPDATE SET enabled = EXCLUDED.enabled`, orgID, name, enabled)
if err != nil {
return fmt.Errorf("set workflow enabled: %w", err)
}
if affected(res) == 0 {
return ErrNoWorkflow
}
return nil
}
// DeleteWorkflow removes every version of a workflow with its draft and
// settings. The runs it made keep their record.
func (s *Store) DeleteWorkflow(ctx context.Context, orgID, name string) error {
tx, err := s.db.BeginTxx(ctx, nil)
if err != nil {
return fmt.Errorf("delete workflow: %w", err)
}
defer func() { _ = tx.Rollback() }()
res, err := tx.ExecContext(ctx, `DELETE FROM runs.workflows WHERE org_id = $1 AND name = $2`, orgID, name)
if err != nil {
return fmt.Errorf("delete workflow: %w", err)
}
if affected(res) == 0 {
return ErrNoWorkflow
}
if _, err := tx.ExecContext(ctx, `DELETE FROM runs.workflow_drafts WHERE org_id = $1 AND name = $2`, orgID, name); err != nil {
return fmt.Errorf("delete workflow draft: %w", err)
}
if _, err := tx.ExecContext(ctx, `DELETE FROM runs.workflow_settings WHERE org_id = $1 AND name = $2`, orgID, name); err != nil {
return fmt.Errorf("delete workflow settings: %w", err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("delete workflow: %w", err)
}
return nil
}
// Draft is the unpublished edit of a workflow.
type Draft struct {
Name string `db:"name"`
Body string `db:"body"`
BaseVersion int `db:"base_version"`
Revision int `db:"revision"`
UpdatedBy string `db:"updated_by"`
UpdatedAt time.Time `db:"updated_at"`
}
const draftColumns = `name, body, base_version, revision, updated_by, updated_at`
// Draft returns a workflow's draft.
func (s *Store) Draft(ctx context.Context, orgID, name string) (Draft, error) {
var d Draft
err := s.db.GetContext(ctx, &d, `SELECT `+draftColumns+` FROM runs.workflow_drafts WHERE org_id = $1 AND name = $2`, orgID, name)
if errors.Is(err, sql.ErrNoRows) {
return Draft{}, ErrNoDraft
}
if err != nil {
return Draft{}, fmt.Errorf("read draft: %w", err)
}
return d, nil
}
// Drafts returns every workflow's draft.
func (s *Store) Drafts(ctx context.Context, orgID string) ([]Draft, error) {
var out []Draft
if err := s.db.SelectContext(ctx, &out, `SELECT `+draftColumns+` FROM runs.workflow_drafts WHERE org_id = $1 ORDER BY name`, orgID); err != nil {
return nil, fmt.Errorf("list drafts: %w", err)
}
return out, nil
}
// PutDraft writes a workflow's draft. revision is the one the writer last saw,
// zero for a workflow with no draft; a draft at any other revision is not
// overwritten.
func (s *Store) PutDraft(ctx context.Context, orgID, name, body, by string, revision int) (Draft, error) {
var d Draft
var err error
if revision == 0 {
err = s.db.GetContext(ctx, &d, `
INSERT INTO runs.workflow_drafts (org_id, name, body, base_version, updated_by)
SELECT $1, $2, $3, COALESCE(max(version), 0), $4 FROM runs.workflows WHERE org_id = $1 AND name = $2
ON CONFLICT (org_id, name) DO NOTHING
RETURNING `+draftColumns, orgID, name, body, by)
} else {
err = s.db.GetContext(ctx, &d, `
UPDATE runs.workflow_drafts SET body = $3, revision = revision + 1, updated_by = $4, updated_at = now()
WHERE org_id = $1 AND name = $2 AND revision = $5
RETURNING `+draftColumns, orgID, name, body, by, revision)
}
if errors.Is(err, sql.ErrNoRows) {
return Draft{}, ErrDraftConflict
}
if err != nil {
return Draft{}, fmt.Errorf("write draft: %w", err)
}
return d, nil
}
// DeleteDraft discards a workflow's draft, and reports whether it had one.
func (s *Store) DeleteDraft(ctx context.Context, orgID, name string) (bool, error) {
res, err := s.db.ExecContext(ctx, `DELETE FROM runs.workflow_drafts WHERE org_id = $1 AND name = $2`, orgID, name)
if err != nil {
return false, fmt.Errorf("delete draft: %w", err)
}
return affected(res) > 0, nil
}
// Stats is what a workflow's runs add up to. Dry runs are not counted.
type Stats struct {
Workflow string `db:"workflow"`
Total int `db:"total"`
Succeeded int `db:"succeeded"`
// Failed counts runs that failed, parked or were declined.
Failed int `db:"failed"`
// Active counts runs that haven't ended.
Active int `db:"active"`
LastRunID string `db:"last_run_id"`
LastState string `db:"last_state"`
LastRunAt time.Time `db:"last_run_at"`
}
// Stats returns each workflow's run statistics over all of its runs.
func (s *Store) Stats(ctx context.Context, orgID string) ([]Stats, error) {
var out []Stats
if err := s.db.SelectContext(ctx, &out, `
SELECT workflow,
count(*) AS total,
count(*) FILTER (WHERE state = 'succeeded') AS succeeded,
count(*) FILTER (WHERE state IN ('failed', 'parked', 'declined')) AS failed,
count(*) FILTER (WHERE state IN ('queued', 'running', 'waiting', 'waiting_approval')) AS active,
(array_agg(id::text ORDER BY created_at DESC))[1] AS last_run_id,
(array_agg(state ORDER BY created_at DESC))[1] AS last_state,
max(created_at) AS last_run_at
FROM runs.runs WHERE org_id = $1 AND NOT dry
GROUP BY workflow ORDER BY workflow`, orgID); err != nil {
return nil, fmt.Errorf("count workflow runs: %w", err)
}
return out, nil
}
// ActiveRuns counts a workflow's runs that haven't ended.
func (s *Store) ActiveRuns(ctx context.Context, orgID, name string) (int, error) {
var n int
if err := s.db.GetContext(ctx, &n, `
SELECT count(*) FROM runs.runs
WHERE org_id = $1 AND workflow = $2 AND NOT dry AND state IN ('queued', 'running', 'waiting', 'waiting_approval')`, orgID, name); err != nil {
return 0, fmt.Errorf("count active runs: %w", err)
}
return n, nil
}