Documentation
¶
Index ¶
- Constants
- Variables
- func AggregateChainStats(jobs []Job) map[string]int64
- func ExtractTraceID(ctx context.Context) string
- 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) 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 Handler
- type HandlerFunc
- type Job
- type JobFilter
- type Logger
- type Metrics
- type NonRetryableError
- 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 Option
- func WithConcurrency(n int) 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 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
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 ExtractTraceID ¶
ExtractTraceID extracts trace ID from context.
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) 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 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 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 Option ¶
type Option func(*Engine)
func WithConcurrency ¶
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 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)
// 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.