middleware

package
v0.2.3-danish-a1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package middleware provides a flexible middleware system for customizing job processing behavior. It implements a chain-of-responsibility pattern where each middleware can perform actions before and after job processing, or modify the processing itself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultHandler

func DefaultHandler(ctx context.Context, jobCtx *job.JobContext) error

DefaultHandler is the base handler that executes the job's Process method. This is always the last handler in the chain and actually processes the job. It simply calls the Process method of the job implementation.

Types

type HandlerFunc

type HandlerFunc func(ctx context.Context, jobCtx *job.JobContext) error

HandlerFunc defines the function signature for processing a job. It takes a context and job context, and returns an error if processing fails. This is the core type that middlewares wrap and enhance.

func Chain

func Chain(middlewares ...Middleware) HandlerFunc

Chain creates a new HandlerFunc by chaining the given middlewares together. The middlewares are executed in the order they appear in the slice, with each middleware wrapping all subsequent middlewares. The DefaultHandler is automatically added as the final handler in the chain.

Example usage:

handler := Chain(
    LoggingMiddleware(logger),    // Executes first
    MetricsMiddleware(),          // Executes second
    ValidationMiddleware(),       // Executes third
)   // DefaultHandler executes last

The execution flow for a single job would be: LoggingMiddleware

→ MetricsMiddleware
  → ValidationMiddleware
    → DefaultHandler (executes job.Process)
  ← ValidationMiddleware
← MetricsMiddleware

← LoggingMiddleware

type Middleware

type Middleware func(next HandlerFunc) HandlerFunc

Middleware defines a function that wraps one HandlerFunc to create a new one. This allows middleware to perform actions before and after the wrapped handler executes, modify the context or job, handle errors, or even prevent the handler from executing.

Example middleware that logs job execution:

func LoggingMiddleware(logger Logger) Middleware {
    return func(next HandlerFunc) HandlerFunc {
        return func(ctx context.Context, jobCtx *job.JobContext) error {
            logger.Info("Starting job", "id", jobCtx.JobID)
            err := next(ctx, jobCtx)
            if err != nil {
                logger.Error("Job failed", "id", jobCtx.JobID, "error", err)
            }
            return err
        }
    }
}

func ConditionalSkipMiddleware

func ConditionalSkipMiddleware(shouldSkip func(*job.JobContext) bool) Middleware

ConditionalSkipMiddleware creates a middleware that conditionally skips job execution.

This middleware evaluates each job against the provided condition function and skips processing if the condition returns true. This can be used to implement filtering, rate limiting, or time-based execution rules.

Parameters:

  • shouldSkip: A function that takes a JobContext and returns true if the job should be skipped

Returns:

  • A middleware function that can be added to the processing chain

func LoggingMiddleware

func LoggingMiddleware(logger logger.Logger) Middleware

LoggingMiddleware creates a middleware that logs job execution details.

This middleware logs when jobs start and finish processing, along with relevant context such as job ID, queue name, and timestamps. On failure, it logs the error details for debugging.

Parameters:

  • logger: A logger instance that implements the logger.Logger interface

Returns:

  • A middleware function that can be added to the processing chain

Jump to

Keyboard shortcuts

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