Dispatch

Extensions

Lifecycle hooks for jobs, workflows, cron, and shutdown events.

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. Extensions only implement the interfaces they care about, and the ext.Registry fans events out to all registered extensions.

Implementing an extension

import (
    "github.com/xraph/dispatch/ext"
    "github.com/xraph/dispatch/job"
)

type AuditExtension struct {
    logger *slog.Logger
}

func (e *AuditExtension) Name() string { return "audit" }

// Job hooks (opt in by implementing the interface)
func (e *AuditExtension) OnJobCompleted(ctx context.Context, j *job.Job, elapsed time.Duration) error {
    e.logger.Info("job completed", "name", j.Name, "elapsed", elapsed)
    return nil
}

func (e *AuditExtension) OnJobFailed(ctx context.Context, j *job.Job, err error) error {
    e.logger.Error("job failed", "name", j.Name, "error", err)
    return nil
}

Registering an extension

eng := engine.Build(d,
    engine.WithExtension(&AuditExtension{logger: slog.Default()}),
    engine.WithExtension(observability.NewMetricsExtension()),
)

Multiple extensions can be registered. They are called in registration order for each event.

Available hooks

Job lifecycle

InterfaceMethodTriggered when
ext.JobEnqueuedOnJobEnqueuedJob is accepted into the queue
ext.JobStartedOnJobStartedWorker begins executing
ext.JobCompletedOnJobCompletedJob finishes successfully
ext.JobFailedOnJobFailedJob fails with no retries remaining
ext.JobRetryingOnJobRetryingJob fails and is scheduled for retry
ext.JobDLQOnJobDLQJob is moved to the DLQ

Workflow lifecycle

InterfaceMethodTriggered when
ext.WorkflowStartedOnWorkflowStartedWorkflow run begins
ext.WorkflowStepCompletedOnWorkflowStepCompletedA step completes
ext.WorkflowStepFailedOnWorkflowStepFailedA step fails
ext.WorkflowCompletedOnWorkflowCompletedWorkflow finishes successfully
ext.WorkflowFailedOnWorkflowFailedWorkflow fails terminally

Other hooks

InterfaceMethodTriggered when
ext.CronFiredOnCronFiredA cron entry fires and a job is enqueued
ext.ShutdownOnShutdownThe dispatcher shuts down gracefully

Built-in extensions

ExtensionPackagePurpose
MetricsExtensionobservabilityOpenTelemetry counters for every lifecycle event
RelayHookExtensionrelay_hookEmit typed webhook events via Relay

On this page