unique

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package unique validates unique-job options, selects the identifying argument fields and computes the period bucket. The canonical form and the hash are computed once, in SQL, by foreman_canonical_json, so every language that enqueues through SQL gets the same key; Go never hashes.

id, err := unique.Identity("email", "default", args, unique.Options{ByArgs: true}, now)
mask := unique.Mask(opts.States()) // "11111000"

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidOptions is wrapped by every validation error of Options.
	ErrInvalidOptions = errors.New("unique: invalid options")
	// ErrArgsNotCanonical reports a non-integral number in the unique
	// identity; foreman_canonical_json rejects it (SQLSTATE 22023).
	ErrArgsNotCanonical = errors.New("foreman: unique args not canonical")
)

Errors of unique jobs.

DefaultStates are the states covered when ByState is empty.

Functions

func Identity

func Identity(kind, queue string, args []byte, o Options, now time.Time) ([]byte, error)

Identity builds the identity object whose canonical form Postgres hashes into unique_key: {"kind", "args"} plus "queue" when ByQueue and "period" when ByPeriod. args must be a JSON value; with ByArgFields it must be an object. Raw argument bytes are copied verbatim so numbers keep full precision.

func Mask

func Mask(states []job.State) string

Mask returns the bit(8) literal for states; position i is job.AllStates[i].

func ParseMask

func ParseMask(mask string) []job.State

ParseMask is the inverse of Mask; unknown or unset bits are ignored.

func PeriodBucket

func PeriodBucket(now time.Time, period time.Duration) int64

PeriodBucket returns the start of the period containing now, in integer Unix seconds: floor(now / period) * period.

Types

type Options

type Options struct {
	ByArgs      bool
	ByArgFields []string
	ByPeriod    time.Duration
	ByQueue     bool
	ByState     []job.State
}

Options selects what makes a job unique.

func (Options) States

func (o Options) States() []job.State

States returns ByState, or DefaultStates when it is empty.

func (Options) Validate

func (o Options) Validate() error

Validate reports every problem with o at once.

Jump to

Keyboard shortcuts

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