Documentation
¶
Index ¶
- Constants
- Variables
- func AggregateChainStats(jobs []Job) map[string]int64
- func ExtractOTelTraceContext(ctx context.Context, job Job) context.Context
- func ExtractTraceID(ctx context.Context) string
- func InjectOTelTraceContext(ctx context.Context, req *EnqueueRequest)
- func IsNonRetryable(err error) bool
- func IsRetryable(err error) bool
- func LogJobAttrs(job Job) []any
- func NonRetryable(err error) error
- func ParallelClaimDemo()
- func RegisterTyped[T any](e *Engine, jobType string, ...)
- func RegisterTypedWithValidator[T any](e *Engine, jobType string, ...)
- func Retryable(err error) error
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type ChainFilter
- type ChainSummary
- type Engine
- func (e *Engine) BulkPurgeDLQ(ctx context.Context, filter JobFilter) (int64, error)
- func (e *Engine) BulkReplayDLQ(ctx context.Context, filter JobFilter) (int64, error)
- func (e *Engine) BulkSkipDLQ(ctx context.Context, filter JobFilter) (int64, error)
- func (e *Engine) Enqueue(ctx context.Context, req EnqueueRequest) (Job, error)
- func (e *Engine) EnqueueBatch(ctx context.Context, reqs []EnqueueRequest) ([]Job, error)
- func (e *Engine) EnqueueDelayed(ctx context.Context, req EnqueueRequest, delay time.Duration) (Job, error)
- func (e *Engine) Get(ctx context.Context, id uuid.UUID) (Job, error)
- func (e *Engine) JobTypes() []string
- func (e *Engine) NotifyChain(chainID string)
- func (e *Engine) Register(jobType string, h Handler)
- func (e *Engine) RegisterFunc(jobType string, fn func(ctx context.Context, job Job) error)
- func (e *Engine) ReplayJob(ctx context.Context, id uuid.UUID) error
- func (e *Engine) RescheduleJob(ctx context.Context, id uuid.UUID, newAvailableAt time.Time) (Job, error)
- func (e *Engine) Schedule(ctx context.Context, req EnqueueRequest, runAt time.Time) (Job, error)
- func (e *Engine) Shutdown(ctx context.Context) error
- func (e *Engine) SkipJob(ctx context.Context, id uuid.UUID) error
- func (e *Engine) Start(ctx context.Context) error
- type EnqueueRequest
- type EventListener
- type FunctionalEventListener
- type Handler
- type HandlerFunc
- type Job
- type JobFilter
- type Logger
- type Metrics
- type NonRetryableError
- type NoopEventListener
- type NoopLogger
- type NoopMetrics
- func (NoopMetrics) IncBlockedChain()
- func (NoopMetrics) IncClaimConflicts()
- func (NoopMetrics) IncClaimed()
- func (NoopMetrics) IncCompleted(string)
- func (NoopMetrics) IncFailed(string)
- func (NoopMetrics) IncPanics(string)
- func (NoopMetrics) IncRetry(string)
- func (NoopMetrics) IncStaleRecovered(int)
- func (NoopMetrics) ObserveExecDuration(string, time.Duration)
- func (NoopMetrics) ObserveQueueDelay(string, time.Duration)
- func (NoopMetrics) SetActiveLeases(int)
- type OTelMetrics
- func (m *OTelMetrics) IncBlockedChain()
- func (m *OTelMetrics) IncClaimConflicts()
- func (m *OTelMetrics) IncClaimed()
- func (m *OTelMetrics) IncCompleted(jobType string)
- func (m *OTelMetrics) IncFailed(jobType string)
- func (m *OTelMetrics) IncPanics(jobType string)
- func (m *OTelMetrics) IncRetry(jobType string)
- func (m *OTelMetrics) IncStaleRecovered(n int)
- func (m *OTelMetrics) ObserveExecDuration(jobType string, d time.Duration)
- func (m *OTelMetrics) ObserveQueueDelay(jobType string, d time.Duration)
- func (m *OTelMetrics) SetActiveLeases(n int)
- type Option
- func WithConcurrency(n int) Option
- func WithDLQThreshold(n int64) Option
- func WithEventListener(l EventListener) Option
- func WithLease(d time.Duration) Option
- func WithLogger(l Logger) Option
- func WithMetrics(m Metrics) Option
- func WithNotify(enabled bool) Option
- func WithOrderingMode(mode string) Option
- func WithOrderingStrategy(s OrderingStrategyResolver) Option
- func WithPanicHandler(h PanicHandler) Option
- func WithPollInterval(d time.Duration) Option
- func WithRecoveryInterval(d time.Duration) Option
- func WithRetryOnPanic(retry bool) Option
- func WithRetryPolicy(p retry.Policy) Option
- func WithSlogLogger(l *slog.Logger) Option
- func WithTracer(t trace.Tracer) Option
- func WithTracerProvider(tp trace.TracerProvider) Option
- func WithWebhook(cfg WebhookConfig) Option
- func WithWorkerID(id string) Option
- type OrderingStrategyResolver
- type PanicError
- type PanicHandler
- type PayloadValidatorFunc
- type Registry
- type Repository
- type RetryableError
- type SlogLogger
- type Stats
- type Validator
- type ValidatorCtx
- type WebhookConfig
- type WebhookDispatcher
- type WebhookPayload
Constants ¶
const ( OrderingModeStrict = "strict" OrderingModeSkipOnFailure = "skip-on-failure" OrderingModeDeadLetterAndContinue = "dead-letter-and-continue" )
Ordering Mode Constants (Policy-Driven Execution Modes)
Variables ¶
var ( ErrNotFound = errors.New("orderedjob: job not found") ErrAlreadyExists = errors.New("orderedjob: job already exists") ErrInvalidTransition = errors.New("orderedjob: invalid state transition") ErrLeaseConflict = errors.New("orderedjob: lease conflict / fencing violation") ErrChainBlocked = errors.New("orderedjob: chain blocked") ErrSequenceGap = errors.New("orderedjob: sequence gap") ErrHandlerNotFound = errors.New("orderedjob: handler not found") ErrNotClaimable = errors.New("orderedjob: job not claimable") ErrDuplicateChainSeq = errors.New("orderedjob: duplicate chain_id, sequence") ErrJobNotEligible = errors.New("orderedjob: job not eligible for rescheduling") ErrValidationFailed = errors.New("orderedjob: payload validation failed") )
Functions ¶
func AggregateChainStats ¶
ProcessJobsParallel is an example of using rill for parallel post-processing (metrics aggregation).
func ExtractOTelTraceContext ¶ added in v1.8.0
ExtractOTelTraceContext extracts TraceID from job into context.
func ExtractTraceID ¶
ExtractTraceID extracts trace ID from context.
func InjectOTelTraceContext ¶ added in v1.8.0
func InjectOTelTraceContext(ctx context.Context, req *EnqueueRequest)
InjectOTelTraceContext extracts W3C traceparent (or OTel SpanContext) from ctx and sets req.TraceID if req.TraceID is empty.
func IsNonRetryable ¶
func IsRetryable ¶
func LogJobAttrs ¶
func NonRetryable ¶
func ParallelClaimDemo ¶
func ParallelClaimDemo()
ParallelClaimDemo shows how rill can be used for fan-out execution if you ever need to process claimed jobs via pipeline.
func RegisterTyped ¶
func RegisterTyped[T any](e *Engine, jobType string, fn func(ctx context.Context, job Job, payload T) error)
RegisterTyped registers a type-safe handler function that automatically unmarshals JSON payloads. If payload type T implements Validator or ValidatorCtx interface, validation is executed automatically before calling fn.
func RegisterTypedWithValidator ¶ added in v1.7.0
func RegisterTypedWithValidator[T any]( e *Engine, jobType string, fn func(ctx context.Context, job Job, payload T) error, validator PayloadValidatorFunc[T], )
RegisterTypedWithValidator registers a type-safe handler function with optional custom payload validation logic. Validation occurs after JSON unmarshaling and self-validation. If validation returns an error, execution fails immediately with a NonRetryable error wrapping ErrValidationFailed.
Types ¶
type ChainFilter ¶ added in v1.4.0
type ChainSummary ¶ added in v1.2.0
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the core orchestrator.
func New ¶
func New(repo Repository, opts ...Option) *Engine
func (*Engine) BulkPurgeDLQ ¶ added in v1.9.0
func (*Engine) BulkReplayDLQ ¶ added in v1.9.0
func (*Engine) BulkSkipDLQ ¶ added in v1.9.0
func (*Engine) EnqueueBatch ¶
func (*Engine) EnqueueDelayed ¶ added in v1.7.0
func (e *Engine) EnqueueDelayed(ctx context.Context, req EnqueueRequest, delay time.Duration) (Job, error)
EnqueueDelayed enqueues a job scheduled for execution after delay.
func (*Engine) JobTypes ¶ added in v1.5.0
JobTypes returns all registered job type names in sorted order.
func (*Engine) NotifyChain ¶
func (*Engine) RegisterFunc ¶
func (*Engine) RescheduleJob ¶ added in v1.7.0
func (e *Engine) RescheduleJob(ctx context.Context, id uuid.UUID, newAvailableAt time.Time) (Job, error)
RescheduleJob modifies the scheduled available_at timestamp of a job.
type EnqueueRequest ¶
type EnqueueRequest struct {
ChainID string `json:"chain_id"`
Sequence int64 `json:"sequence"`
Type string `json:"job_type"`
Payload any `json:"payload"`
IdempotencyKey string `json:"idempotency_key,omitempty"`
TenantID string `json:"tenant_id,omitempty"`
TraceID string `json:"trace_id,omitempty"`
OrderingMode string `json:"ordering_mode,omitempty"`
MaxAttempts int `json:"max_attempts,omitempty"`
DeadlineAt *time.Time `json:"deadline_at,omitempty"`
AvailableAt *time.Time `json:"available_at,omitempty"`
}
EnqueueRequest is used by callers.
type EventListener ¶ added in v1.9.0
type EventListener interface {
OnJobFailed(ctx context.Context, job Job, err error)
OnChainBlocked(ctx context.Context, chainID string, job Job)
OnDLQThresholdExceeded(ctx context.Context, dlqCount int64)
}
EventListener defines callbacks for key queue lifecycle events.
type FunctionalEventListener ¶ added in v1.9.0
type FunctionalEventListener struct {
OnJobFailedFunc func(ctx context.Context, job Job, err error)
OnChainBlockedFunc func(ctx context.Context, chainID string, job Job)
OnDLQThresholdExceededFunc func(ctx context.Context, dlqCount int64)
}
FunctionalEventListener allows registering individual functions as EventListener.
func (*FunctionalEventListener) OnChainBlocked ¶ added in v1.9.0
func (f *FunctionalEventListener) OnChainBlocked(ctx context.Context, chainID string, job Job)
func (*FunctionalEventListener) OnDLQThresholdExceeded ¶ added in v1.9.0
func (f *FunctionalEventListener) OnDLQThresholdExceeded(ctx context.Context, dlqCount int64)
func (*FunctionalEventListener) OnJobFailed ¶ added in v1.9.0
func (f *FunctionalEventListener) OnJobFailed(ctx context.Context, job Job, err error)
type HandlerFunc ¶
type Job ¶
type Job struct {
ID uuid.UUID `json:"id"`
ChainID string `json:"chain_id"`
Sequence int64 `json:"sequence"`
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
IdempotencyKey string `json:"idempotency_key,omitempty"`
TenantID string `json:"tenant_id,omitempty"`
OrderingMode string `json:"ordering_mode,omitempty"`
// runtime fields
Status string `json:"status"`
Attempt int `json:"attempt"`
MaxAttempts int `json:"max_attempts"`
AvailableAt time.Time `json:"available_at"`
DeadlineAt *time.Time `json:"deadline_at,omitempty"`
WorkerID string `json:"worker_id,omitempty"`
LeaseUntil *time.Time `json:"lease_until,omitempty"`
LeaseGeneration int `json:"lease_generation"`
LastError string `json:"last_error,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
StartedAt *time.Time `json:"started_at,omitempty"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
FailedAt *time.Time `json:"failed_at,omitempty"`
TraceID string `json:"trace_id,omitempty"`
}
Job represents a unit of work bound to a chain.
func BatchEnqueueParallel ¶
BatchEnqueueParallel demonstrates rill usage for high-throughput enqueue validation / transformation. It validates payloads in parallel, then enqueues atomically via repository.
func (Job) IsTerminal ¶
type Logger ¶
type Logger interface {
Info(ctx context.Context, msg string, args ...any)
Error(ctx context.Context, msg string, args ...any)
Debug(ctx context.Context, msg string, args ...any)
}
Logger structured minimal per section 26.
type Metrics ¶
type Metrics interface {
IncClaimed()
IncCompleted(jobType string)
IncFailed(jobType string)
IncRetry(jobType string)
ObserveExecDuration(jobType string, d time.Duration)
ObserveQueueDelay(jobType string, d time.Duration)
SetActiveLeases(n int)
IncClaimConflicts()
IncStaleRecovered(n int)
IncBlockedChain()
IncPanics(jobType string)
}
Metrics interface per proposal section 25.
type NonRetryableError ¶
type NonRetryableError struct{ Err error }
NonRetryableError marks a permanent failure.
func (NonRetryableError) Error ¶
func (e NonRetryableError) Error() string
func (NonRetryableError) Unwrap ¶
func (e NonRetryableError) Unwrap() error
type NoopEventListener ¶ added in v1.9.0
type NoopEventListener struct{}
func (NoopEventListener) OnChainBlocked ¶ added in v1.9.0
func (NoopEventListener) OnChainBlocked(context.Context, string, Job)
func (NoopEventListener) OnDLQThresholdExceeded ¶ added in v1.9.0
func (NoopEventListener) OnDLQThresholdExceeded(context.Context, int64)
func (NoopEventListener) OnJobFailed ¶ added in v1.9.0
func (NoopEventListener) OnJobFailed(context.Context, Job, error)
type NoopLogger ¶
type NoopLogger struct{}
type NoopMetrics ¶
type NoopMetrics struct{}
func (NoopMetrics) IncBlockedChain ¶
func (NoopMetrics) IncBlockedChain()
func (NoopMetrics) IncClaimConflicts ¶
func (NoopMetrics) IncClaimConflicts()
func (NoopMetrics) IncClaimed ¶
func (NoopMetrics) IncClaimed()
func (NoopMetrics) IncCompleted ¶
func (NoopMetrics) IncCompleted(string)
func (NoopMetrics) IncFailed ¶
func (NoopMetrics) IncFailed(string)
func (NoopMetrics) IncPanics ¶ added in v1.7.0
func (NoopMetrics) IncPanics(string)
func (NoopMetrics) IncRetry ¶
func (NoopMetrics) IncRetry(string)
func (NoopMetrics) IncStaleRecovered ¶
func (NoopMetrics) IncStaleRecovered(int)
func (NoopMetrics) ObserveExecDuration ¶
func (NoopMetrics) ObserveExecDuration(string, time.Duration)
func (NoopMetrics) ObserveQueueDelay ¶
func (NoopMetrics) ObserveQueueDelay(string, time.Duration)
func (NoopMetrics) SetActiveLeases ¶
func (NoopMetrics) SetActiveLeases(int)
type OTelMetrics ¶ added in v1.8.0
type OTelMetrics struct {
// contains filtered or unexported fields
}
OTelMetrics implements Metrics backed by OpenTelemetry Metrics API.
func NewOTelMetrics ¶ added in v1.8.0
func NewOTelMetrics(mp metric.MeterProvider) (*OTelMetrics, error)
NewOTelMetrics creates a Metrics exporter backed by an OpenTelemetry Meter. If mp is nil, otel.GetMeterProvider() is used.
func (*OTelMetrics) IncBlockedChain ¶ added in v1.8.0
func (m *OTelMetrics) IncBlockedChain()
func (*OTelMetrics) IncClaimConflicts ¶ added in v1.8.0
func (m *OTelMetrics) IncClaimConflicts()
func (*OTelMetrics) IncClaimed ¶ added in v1.8.0
func (m *OTelMetrics) IncClaimed()
func (*OTelMetrics) IncCompleted ¶ added in v1.8.0
func (m *OTelMetrics) IncCompleted(jobType string)
func (*OTelMetrics) IncFailed ¶ added in v1.8.0
func (m *OTelMetrics) IncFailed(jobType string)
func (*OTelMetrics) IncPanics ¶ added in v1.8.0
func (m *OTelMetrics) IncPanics(jobType string)
func (*OTelMetrics) IncRetry ¶ added in v1.8.0
func (m *OTelMetrics) IncRetry(jobType string)
func (*OTelMetrics) IncStaleRecovered ¶ added in v1.8.0
func (m *OTelMetrics) IncStaleRecovered(n int)
func (*OTelMetrics) ObserveExecDuration ¶ added in v1.8.0
func (m *OTelMetrics) ObserveExecDuration(jobType string, d time.Duration)
func (*OTelMetrics) ObserveQueueDelay ¶ added in v1.8.0
func (m *OTelMetrics) ObserveQueueDelay(jobType string, d time.Duration)
func (*OTelMetrics) SetActiveLeases ¶ added in v1.8.0
func (m *OTelMetrics) SetActiveLeases(n int)
type Option ¶
type Option func(*Engine)
func WithConcurrency ¶
func WithDLQThreshold ¶ added in v1.9.0
func WithEventListener ¶ added in v1.9.0
func WithEventListener(l EventListener) Option
func WithLogger ¶
func WithMetrics ¶
func WithNotify ¶
func WithOrderingMode ¶
func WithOrderingStrategy ¶ added in v1.6.0
func WithOrderingStrategy(s OrderingStrategyResolver) Option
func WithPanicHandler ¶ added in v1.7.0
func WithPanicHandler(h PanicHandler) Option
func WithPollInterval ¶
func WithRecoveryInterval ¶
func WithRetryOnPanic ¶ added in v1.7.0
func WithRetryPolicy ¶
func WithSlogLogger ¶
func WithTracer ¶ added in v1.8.0
func WithTracerProvider ¶ added in v1.8.0
func WithTracerProvider(tp trace.TracerProvider) Option
func WithWebhook ¶ added in v1.9.0
func WithWebhook(cfg WebhookConfig) Option
func WithWorkerID ¶
type OrderingStrategyResolver ¶ added in v1.6.0
type OrderingStrategyResolver func(req EnqueueRequest) string
OrderingStrategyResolver resolves ordering mode per job request dynamically.
type PanicError ¶ added in v1.7.0
PanicError encapsulates a panic value and stack trace captured during job execution.
func (*PanicError) Error ¶ added in v1.7.0
func (p *PanicError) Error() string
type PanicHandler ¶ added in v1.7.0
PanicHandler handles panics caught during job execution.
type PayloadValidatorFunc ¶ added in v1.7.0
PayloadValidatorFunc is a custom validation function type for a payload of type T.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds job_type -> handler
func NewRegistry ¶
func NewRegistry() *Registry
type Repository ¶
type Repository interface {
// Enqueue inserts a job. If req.Sequence == 0 it auto-assigns.
Enqueue(ctx context.Context, req EnqueueRequest) (Job, error)
// EnqueueBatch atomic if required.
EnqueueBatch(ctx context.Context, reqs []EnqueueRequest) ([]Job, error)
Get(ctx context.Context, id uuid.UUID) (Job, error)
// GetByChainSeq fetches by chain+seq
GetByChainSeq(ctx context.Context, chainID string, seq int64) (Job, error)
// Claim finds and atomically marks a job as PROCESSING (C3, C4, C10).
// Returns ErrNotFound if none eligible.
Claim(ctx context.Context, workerID string, lease time.Duration) (Job, error)
// Complete marks COMPLETED with fencing check (C10, C5).
Complete(ctx context.Context, id uuid.UUID, workerID string, leaseGen int) error
// Fail marks FAILED / DEAD_LETTERED
Fail(ctx context.Context, id uuid.UUID, workerID string, leaseGen int, errMsg string, terminal bool) error
// Retry marks RETRYING with next available_at (C6)
Retry(ctx context.Context, id uuid.UUID, workerID string, leaseGen int, errMsg string, nextAvailable time.Time) error
// Heartbeat extends lease
Heartbeat(ctx context.Context, id uuid.UUID, workerID string, leaseGen int, lease time.Duration) error
// Cancel
RequestCancel(ctx context.Context, id uuid.UUID) error
ConfirmCancel(ctx context.Context, id uuid.UUID, workerID string, leaseGen int) error
// Recovery & DLQ Operations
RecoverStale(ctx context.Context, limit int, retryPolicy func(attempt int) time.Duration) (int, error)
ReplayJob(ctx context.Context, id uuid.UUID) error
ReplayJobWithPayload(ctx context.Context, id uuid.UUID, newPayload json.RawMessage) error
SkipJob(ctx context.Context, id uuid.UUID) error
RescheduleJob(ctx context.Context, id uuid.UUID, availableAt time.Time) (Job, error)
BulkReplayDLQ(ctx context.Context, filter JobFilter) (int64, error)
BulkSkipDLQ(ctx context.Context, filter JobFilter) (int64, error)
BulkPurgeDLQ(ctx context.Context, filter JobFilter) (int64, error)
CountDLQ(ctx context.Context) (int64, error)
// Make next eligible after completion (C5)
PromoteNext(ctx context.Context, chainID string, completedSeq int64) error
// List etc
ListPendingChains(ctx context.Context) ([]string, error)
// Inspection & Dashboard Methods
GetStats(ctx context.Context) (Stats, error)
ListJobs(ctx context.Context, filter JobFilter) ([]Job, int64, error)
ListChains(ctx context.Context, filter ChainFilter) ([]ChainSummary, int64, error)
// Real-time LISTEN/NOTIFY interface
NotifyChannel() string
Listen(ctx context.Context, callback func(chainID string)) error
}
Repository is the persistence port. All coordination decisions use DB time where consistency matters (C16).
type RetryableError ¶
type RetryableError struct{ Err error }
RetryableError marks a transient failure that should be retried.
func (RetryableError) Error ¶
func (e RetryableError) Error() string
func (RetryableError) Unwrap ¶
func (e RetryableError) Unwrap() error
type SlogLogger ¶
func NewSlogLogger ¶
func NewSlogLogger(l *slog.Logger) *SlogLogger
type Stats ¶ added in v1.2.0
type Stats struct {
Pending int64 `json:"pending"`
Blocked int64 `json:"blocked"`
Processing int64 `json:"processing"`
Completed int64 `json:"completed"`
Retrying int64 `json:"retrying"`
Failed int64 `json:"failed"`
DeadLettered int64 `json:"dead_lettered"`
CancelRequested int64 `json:"cancel_requested"`
Cancelled int64 `json:"cancelled"`
Scheduled int64 `json:"scheduled"`
Total int64 `json:"total"`
ActiveChains int64 `json:"active_chains"`
}
type Validator ¶ added in v1.7.0
type Validator interface {
Validate() error
}
Validator is an interface that payload structs can implement to perform self-validation.
type ValidatorCtx ¶ added in v1.7.0
ValidatorCtx is an interface that payload structs can implement to perform self-validation with context.
type WebhookConfig ¶ added in v1.9.0
type WebhookDispatcher ¶ added in v1.9.0
type WebhookDispatcher struct {
// contains filtered or unexported fields
}
func NewWebhookDispatcher ¶ added in v1.9.0
func NewWebhookDispatcher(cfg WebhookConfig) *WebhookDispatcher
func (*WebhookDispatcher) OnChainBlocked ¶ added in v1.9.0
func (w *WebhookDispatcher) OnChainBlocked(ctx context.Context, chainID string, job Job)
func (*WebhookDispatcher) OnDLQThresholdExceeded ¶ added in v1.9.0
func (w *WebhookDispatcher) OnDLQThresholdExceeded(ctx context.Context, dlqCount int64)
func (*WebhookDispatcher) OnJobFailed ¶ added in v1.9.0
func (w *WebhookDispatcher) OnJobFailed(ctx context.Context, job Job, err error)
type WebhookPayload ¶ added in v1.9.0
type WebhookPayload struct {
Event string `json:"event"`
Timestamp time.Time `json:"timestamp"`
ChainID string `json:"chain_id,omitempty"`
JobID string `json:"job_id,omitempty"`
JobType string `json:"job_type,omitempty"`
Error string `json:"error,omitempty"`
DLQCount int64 `json:"dlq_count,omitempty"`
}