Help us shape the future of Go! Take the 2026 Go Developer Survey

ext

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ext defines the extension system for Dispatch.

Extensions are notified of lifecycle events and can react to them — recording metrics, emitting webhooks, writing audit logs, etc. Each lifecycle hook is a separate interface so extensions opt in only to the events they care about.

Implementing an Extension

type MyExtension struct{}

func (e *MyExtension) Name() string { return "my-extension" }

// Opt in to specific hooks by implementing their interfaces.
func (e *MyExtension) OnJobCompleted(ctx context.Context, j *job.Job, elapsed time.Duration) error {
    log.Printf("job %s completed in %s", j.ID, elapsed)
    return nil
}

Job Lifecycle Hooks

Workflow Lifecycle Hooks

Other Hooks

  • CronFired — a cron entry was triggered and a job was enqueued
  • Shutdown — the dispatcher is shutting down gracefully

Operator Actions

  • OperatorActionObserver: an operator cancelled, retried, replayed, deleted, purged, toggled or triggered something through the engine. The Action carries the subject from WithActor as its Actor.

The Registry fans out each event to all registered extensions that implement the corresponding hook interface.

Package ext defines the extension system for Dispatch. Extensions are notified of lifecycle events (job enqueued, completed, failed, etc.) and can react to them — logging, metrics, tracing, etc.

Each lifecycle hook is a separate interface so extensions opt in only to the events they care about.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActorFrom

func ActorFrom(ctx context.Context) string

ActorFrom returns the subject stored by WithActor, or "" when ctx carries none.

func WithActor

func WithActor(ctx context.Context, subject string) context.Context

WithActor returns a copy of ctx that carries the subject taking an operator action. The engine reads it back through ActorFrom.

Types

type Action

type Action struct {
	Kind     ActionKind
	Actor    string   // from ActorFrom(ctx); empty when unknown
	JobID    id.JobID // job acted on
	NewJobID id.JobID // job created (replay, cron trigger)
	DLQID    id.DLQID
	CronID   id.CronID
	RunID    id.RunID
	Step     string // workflow replay step
	Count    int64  // purge count, replay-all count
	At       time.Time
}

Action describes one operator action. Only the fields that apply to its Kind are set; the rest keep their zero values.

type ActionKind

type ActionKind string

ActionKind names one kind of operator action.

const (
	ActionJobCancelled     ActionKind = "job.cancelled"
	ActionJobRetried       ActionKind = "job.retried"
	ActionDLQReplayed      ActionKind = "dlq.replayed"
	ActionDLQDeleted       ActionKind = "dlq.deleted"
	ActionDLQPurged        ActionKind = "dlq.purged"
	ActionCronEnabled      ActionKind = "cron.enabled"
	ActionCronDisabled     ActionKind = "cron.disabled"
	ActionCronDeleted      ActionKind = "cron.deleted"
	ActionCronTriggered    ActionKind = "cron.triggered"
	ActionWorkflowReplayed ActionKind = "workflow.replayed"
)

Operator action kinds. The engine emits exactly one Action per successful operator call.

type CronFired

type CronFired interface {
	OnCronFired(ctx context.Context, entryName string, jobID id.JobID) error
}

CronFired is called when a cron entry fires and enqueues a job.

type Extension

type Extension interface {
	// Name returns a unique human-readable name for the extension.
	Name() string
}

Extension is the base interface all extensions must implement.

type JobCancelled

type JobCancelled interface {
	OnJobCancelled(ctx context.Context, j *job.Job) error
}

JobCancelled fires when a job reaches cancelled: immediately for a pending or retrying job, and for a running job when its worker observes the cancel (lease lost to a cancelled row) instead of reporting job.failed.

type JobCompleted

type JobCompleted interface {
	OnJobCompleted(ctx context.Context, j *job.Job, elapsed time.Duration) error
}

JobCompleted is called after a job finishes successfully.

type JobDLQ

type JobDLQ interface {
	OnJobDLQ(ctx context.Context, j *job.Job, err error) error
}

JobDLQ is called when a job is moved to the dead letter queue.

type JobEnqueued

type JobEnqueued interface {
	OnJobEnqueued(ctx context.Context, j *job.Job) error
}

JobEnqueued is called after a job is successfully enqueued.

type JobFailed

type JobFailed interface {
	OnJobFailed(ctx context.Context, j *job.Job, err error) error
}

JobFailed is called when a job fails terminally (no more retries).

type JobRetrying

type JobRetrying interface {
	OnJobRetrying(ctx context.Context, j *job.Job, attempt int, nextRunAt time.Time) error
}

JobRetrying is called when a job fails but is scheduled for retry.

type JobStarted

type JobStarted interface {
	OnJobStarted(ctx context.Context, j *job.Job) error
}

JobStarted is called when a worker begins executing a job.

type OperatorActionObserver

type OperatorActionObserver interface {
	OnOperatorAction(ctx context.Context, a Action) error
}

OperatorActionObserver sees every operator action taken through the engine.

type Registry

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

Registry holds registered extensions and dispatches lifecycle events to them. It type-caches extensions at registration time so emit calls iterate only over extensions that implement the relevant hook.

func NewRegistry

func NewRegistry(logger log.Logger) *Registry

NewRegistry creates an extension registry with the given logger.

func (*Registry) EmitCronFired

func (r *Registry) EmitCronFired(ctx context.Context, entryName string, jobID id.JobID)

EmitCronFired notifies all extensions that implement CronFired.

func (*Registry) EmitJobCancelled

func (r *Registry) EmitJobCancelled(ctx context.Context, j *job.Job)

EmitJobCancelled notifies all extensions that implement JobCancelled.

func (*Registry) EmitJobCompleted

func (r *Registry) EmitJobCompleted(ctx context.Context, j *job.Job, elapsed time.Duration)

EmitJobCompleted notifies all extensions that implement JobCompleted.

func (*Registry) EmitJobDLQ

func (r *Registry) EmitJobDLQ(ctx context.Context, j *job.Job, jobErr error)

EmitJobDLQ notifies all extensions that implement JobDLQ.

func (*Registry) EmitJobEnqueued

func (r *Registry) EmitJobEnqueued(ctx context.Context, j *job.Job)

EmitJobEnqueued notifies all extensions that implement JobEnqueued.

func (*Registry) EmitJobFailed

func (r *Registry) EmitJobFailed(ctx context.Context, j *job.Job, jobErr error)

EmitJobFailed notifies all extensions that implement JobFailed.

func (*Registry) EmitJobRetrying

func (r *Registry) EmitJobRetrying(ctx context.Context, j *job.Job, attempt int, nextRunAt time.Time)

EmitJobRetrying notifies all extensions that implement JobRetrying.

func (*Registry) EmitJobStarted

func (r *Registry) EmitJobStarted(ctx context.Context, j *job.Job)

EmitJobStarted notifies all extensions that implement JobStarted.

func (*Registry) EmitOperatorAction

func (r *Registry) EmitOperatorAction(ctx context.Context, a Action)

EmitOperatorAction notifies all extensions that implement OperatorActionObserver. An empty Actor is filled from ActorFrom(ctx) and a zero At with the current time in UTC, so callers only set what they know.

func (*Registry) EmitShutdown

func (r *Registry) EmitShutdown(ctx context.Context)

EmitShutdown notifies all extensions that implement Shutdown.

func (*Registry) EmitWorkflowCompleted

func (r *Registry) EmitWorkflowCompleted(ctx context.Context, run *workflow.Run, elapsed time.Duration)

EmitWorkflowCompleted notifies all extensions that implement WorkflowCompleted.

func (*Registry) EmitWorkflowFailed

func (r *Registry) EmitWorkflowFailed(ctx context.Context, run *workflow.Run, runErr error)

EmitWorkflowFailed notifies all extensions that implement WorkflowFailed.

func (*Registry) EmitWorkflowStarted

func (r *Registry) EmitWorkflowStarted(ctx context.Context, run *workflow.Run)

EmitWorkflowStarted notifies all extensions that implement WorkflowStarted.

func (*Registry) EmitWorkflowStepCompleted

func (r *Registry) EmitWorkflowStepCompleted(ctx context.Context, run *workflow.Run, stepName string, elapsed time.Duration)

EmitWorkflowStepCompleted notifies all extensions that implement WorkflowStepCompleted.

func (*Registry) EmitWorkflowStepFailed

func (r *Registry) EmitWorkflowStepFailed(ctx context.Context, run *workflow.Run, stepName string, stepErr error)

EmitWorkflowStepFailed notifies all extensions that implement WorkflowStepFailed.

func (*Registry) Extensions

func (r *Registry) Extensions() []Extension

Extensions returns all registered extensions.

func (*Registry) Register

func (r *Registry) Register(e Extension)

Register adds an extension and type-asserts it into all applicable hook caches. Extensions are notified in registration order.

type Shutdown

type Shutdown interface {
	OnShutdown(ctx context.Context) error
}

Shutdown is called during graceful shutdown.

type WorkflowCompleted

type WorkflowCompleted interface {
	OnWorkflowCompleted(ctx context.Context, r *workflow.Run, elapsed time.Duration) error
}

WorkflowCompleted is called after a workflow run finishes successfully.

type WorkflowFailed

type WorkflowFailed interface {
	OnWorkflowFailed(ctx context.Context, r *workflow.Run, err error) error
}

WorkflowFailed is called when a workflow run fails terminally.

type WorkflowStarted

type WorkflowStarted interface {
	OnWorkflowStarted(ctx context.Context, r *workflow.Run) error
}

WorkflowStarted is called when a workflow run begins.

type WorkflowStepCompleted

type WorkflowStepCompleted interface {
	OnWorkflowStepCompleted(ctx context.Context, r *workflow.Run, stepName string, elapsed time.Duration) error
}

WorkflowStepCompleted is called after a workflow step completes.

type WorkflowStepFailed

type WorkflowStepFailed interface {
	OnWorkflowStepFailed(ctx context.Context, r *workflow.Run, stepName string, err error) error
}

WorkflowStepFailed is called when a workflow step fails.

Jump to

Keyboard shortcuts

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