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 ¶
- JobEnqueued — job was accepted into the queue
- JobStarted — worker began executing the job
- JobCompleted — job finished successfully
- JobFailed — job failed with no retries remaining
- JobRetrying — job failed but will be retried
- JobDLQ — job was moved to the dead letter queue
- JobCancelled: job reached cancelled
Workflow Lifecycle Hooks ¶
- WorkflowStarted — workflow run began
- WorkflowStepCompleted — a step finished successfully
- WorkflowStepFailed — a step failed
- WorkflowCompleted — workflow run finished successfully
- WorkflowFailed — workflow run failed terminally
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 ¶
- func ActorFrom(ctx context.Context) string
- func WithActor(ctx context.Context, subject string) context.Context
- type Action
- type ActionKind
- type CronFired
- type Extension
- type JobCancelled
- type JobCompleted
- type JobDLQ
- type JobEnqueued
- type JobFailed
- type JobRetrying
- type JobStarted
- type OperatorActionObserver
- type Registry
- func (r *Registry) EmitCronFired(ctx context.Context, entryName string, jobID id.JobID)
- func (r *Registry) EmitJobCancelled(ctx context.Context, j *job.Job)
- func (r *Registry) EmitJobCompleted(ctx context.Context, j *job.Job, elapsed time.Duration)
- func (r *Registry) EmitJobDLQ(ctx context.Context, j *job.Job, jobErr error)
- func (r *Registry) EmitJobEnqueued(ctx context.Context, j *job.Job)
- func (r *Registry) EmitJobFailed(ctx context.Context, j *job.Job, jobErr error)
- func (r *Registry) EmitJobRetrying(ctx context.Context, j *job.Job, attempt int, nextRunAt time.Time)
- func (r *Registry) EmitJobStarted(ctx context.Context, j *job.Job)
- func (r *Registry) EmitOperatorAction(ctx context.Context, a Action)
- func (r *Registry) EmitShutdown(ctx context.Context)
- func (r *Registry) EmitWorkflowCompleted(ctx context.Context, run *workflow.Run, elapsed time.Duration)
- func (r *Registry) EmitWorkflowFailed(ctx context.Context, run *workflow.Run, runErr error)
- func (r *Registry) EmitWorkflowStarted(ctx context.Context, run *workflow.Run)
- func (r *Registry) EmitWorkflowStepCompleted(ctx context.Context, run *workflow.Run, stepName string, elapsed time.Duration)
- func (r *Registry) EmitWorkflowStepFailed(ctx context.Context, run *workflow.Run, stepName string, stepErr error)
- func (r *Registry) Extensions() []Extension
- func (r *Registry) Register(e Extension)
- type Shutdown
- type WorkflowCompleted
- type WorkflowFailed
- type WorkflowStarted
- type WorkflowStepCompleted
- type WorkflowStepFailed
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 JobEnqueued ¶
JobEnqueued is called after a job is successfully enqueued.
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 ¶
JobStarted is called when a worker begins executing a job.
type OperatorActionObserver ¶
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 ¶
NewRegistry creates an extension registry with the given logger.
func (*Registry) EmitCronFired ¶
EmitCronFired notifies all extensions that implement CronFired.
func (*Registry) EmitJobCancelled ¶
EmitJobCancelled notifies all extensions that implement JobCancelled.
func (*Registry) EmitJobCompleted ¶
EmitJobCompleted notifies all extensions that implement JobCompleted.
func (*Registry) EmitJobDLQ ¶
EmitJobDLQ notifies all extensions that implement JobDLQ.
func (*Registry) EmitJobEnqueued ¶
EmitJobEnqueued notifies all extensions that implement JobEnqueued.
func (*Registry) EmitJobFailed ¶
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 ¶
EmitJobStarted notifies all extensions that implement JobStarted.
func (*Registry) EmitOperatorAction ¶
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 ¶
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 ¶
EmitWorkflowFailed notifies all extensions that implement WorkflowFailed.
func (*Registry) EmitWorkflowStarted ¶
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 ¶
Extensions returns all registered extensions.
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 ¶
WorkflowStarted is called when a workflow run begins.