Data & infrastructure/Quiver: Jobs & Schedule

Quiver: Jobs & Schedule

Run typed background jobs, recurring schedules, and transactional enqueues with Quiver on memory, SQLite, Postgres, or Amazon SQS.

quivergithub.com/fletchhq/fletch/quiver· fletch docs show quiver

On this page

Quiver is Fletch’s standalone background job and scheduling package. The core quiver module uses only the Go standard library and includes an in-memory driver; durable backends live in separate submodules so your binary only pulls in the driver you run.

Packages

Package Backend
github.com/fletchhq/fletch/quiver Stdlib-only core API, worker runtime, scheduler, and in-memory driver
github.com/fletchhq/fletch/quiver/sqlite Pure-Go SQLite queue and schedule store
github.com/fletchhq/fletch/quiver/postgres pgxpool backend using FOR UPDATE SKIP LOCKED and LISTEN/NOTIFY
github.com/fletchhq/fletch/quiver/sqs Amazon SQS backend using aws-sdk-go-v2

Defining Jobs and Handlers

Every job payload is a Go struct implementing quiver.JobArgs (a single Kind() string method). Implement quiver.JobArgsWithDefaults when a job should carry its own default queue, priority, retry limit, timeout, backoff, or uniqueness window.

package mail

import (
	"time"

	"github.com/fletchhq/fletch/quiver"
)

type SendDigest struct {
	UserID int64
}

func (SendDigest) Kind() string { return "send_digest" }

func (SendDigest) Defaults() quiver.JobDefaults {
	return quiver.JobDefaults{
		Queue:       "mail",
		Priority:    2,
		MaxAttempts: 5,
		Timeout:     time.Minute,
	}
}

quiver.JobDefaults supports Queue, Priority (1 to 4, where 1 runs first), MaxAttempts, Timeout, Backoff, UniqueKey, and UniqueFor.

You can scaffold a new job type and handler inside a module with the CLI:

fletch make job mail send_digest

Register handlers either as plain functions with quiver.Handle[T] or as structs implementing quiver.Worker[T] (Work(ctx, *quiver.Job[T]) error) with quiver.HandleWorker[T].

quiver.Handle[SendDigest](q, func(ctx context.Context, job *quiver.Job[SendDigest]) error {
	return mailer.SendWeeklyDigest(ctx, job.Args.UserID)
})
type DigestWorker struct {
	Mailer *Mailer
}

func (w *DigestWorker) Work(ctx context.Context, job *quiver.Job[SendDigest]) error {
	return w.Mailer.SendWeeklyDigest(ctx, job.Args.UserID)
}

quiver.HandleWorker[SendDigest](q, &DigestWorker{Mailer: mailer})

Enqueueing Jobs

Use quiver.Enqueue for a single job, quiver.EnqueueMany for a batch, and quiver.EnqueueTx to enqueue atomically inside an open database transaction.

err := quiver.Enqueue(ctx, q, SendDigest{UserID: userID},
	quiver.OnQueue("mail"),
	quiver.In(5*time.Minute),
	quiver.Tags("digest", "weekly"),
	quiver.Meta("source", "signup"),
)
err := quiver.EnqueueTx(ctx, q, tx, SendDigest{UserID: userID},
	quiver.Unique("digest:42", quiver.UniqueFor(time.Hour)),
)

quiver.EnqueueTx accepts a Nock transaction, *sql.Tx, or pgx.Tx. When using quiver/sqs, configure quiver.WithOutbox(db) so transactional enqueues commit to a SQL outbox table and relay to SQS asynchronously.

Enqueue Options

Option Effect
quiver.OnQueue(name) Routes the job to name, overriding Defaults().Queue
quiver.In(delay) Delays first execution by delay
quiver.Tags(...) Attaches string tags to the job
quiver.Meta(...) Attaches metadata key-value pairs to the job
quiver.Unique(key, quiver.UniqueFor(d)) Prevents duplicate enqueues sharing key during window d

Outcomes, Retries, and Idempotency

A handler’s return value controls what Quiver does next:

  • Returning nil marks the job completed.
  • Returning any regular error, or panicking inside the handler, retries the job using quiver.WithBackoff (or quiver.DefaultBackoff) up to MaxAttempts.
  • Returning quiver.Discard(err) marks the job failed immediately without further retries.
  • Returning quiver.Snooze(d) reschedules the job to run again after d without counting against MaxAttempts.

Queues, Leases, and Middleware

Configure isolated concurrency lanes per queue with quiver.Queues and quiver.QueueConfig, or set worker-wide defaults with functional options:

q := quiver.New(driver,
	quiver.Queues(
		quiver.QueueConfig{Name: "critical", Concurrency: 4},
		quiver.QueueConfig{Name: "mail", Concurrency: 2},
	),
	quiver.WithVisibilityTimeout(2*time.Minute),
	quiver.WithHeartbeat(30*time.Second),
	quiver.WithRetention(24*time.Hour),
	quiver.Use(quiver.Recover(), quiver.Logger(slog.Default())),
	quiver.WithObserver(provider.QuiverObserver()),
)

Available queue and worker options:

  • quiver.Queues(quiver.QueueConfig{Name, Concurrency}...) for per-queue worker pools
  • quiver.WithConcurrency for default worker concurrency
  • quiver.WithVisibilityTimeout for lease duration before a stalled job becomes claimable again
  • quiver.WithHeartbeat to extend leases automatically while long-running handlers execute
  • quiver.WithRetention to control how long finished jobs are kept (default 24h)
  • quiver.WithBackoff to override quiver.DefaultBackoff
  • quiver.Use(...) to wrap job execution with middleware such as quiver.Recover() and quiver.Logger(slog.Default())
  • quiver.WithObserver(provider.QuiverObserver()) to emit queue and job telemetry

Schedules

Quiver supports two scheduling styles. Across multiple processes sharing the same backing store, each scheduled occurrence fires once.

  • quiver.Schedule[T](q, name, spec, func() T, opts...) enqueues a real typed JobArgs job when the schedule ticks, so the run inherits queues, retries, timeouts, and job history.
  • quiver.Every(q, name, spec, fn) runs a function directly on the schedule without retry tracking.

Schedule specs:

  • quiver.Interval(d) runs on a fixed time.Duration cadence.
  • quiver.Cron(expr) evaluates a cron expression in UTC.
  • quiver.CronIn(expr, loc) evaluates a cron expression in loc with DST-safe transitions.
loc, _ := time.LoadLocation("America/Denver")

quiver.Schedule[SendDigest](q, "daily-digest",
	quiver.CronIn("0 8 * * *", loc),
	func() SendDigest { return SendDigest{UserID: 0} },
)

quiver.Every(q, "prune-temp", quiver.Interval(15*time.Minute), func(ctx context.Context) error {
	return pruneTempFiles(ctx)
})

Drivers and Capabilities

Every backend implements the core quiver.Driver interface and opts into additional behavior through capability interfaces:

  • TxDriver for EnqueueTx
  • UniqueDriver for Unique deduplication
  • ScheduleDriver for coordinated schedules across processes
  • AdminDriver and Manager for CLI inspection, stats, retries, cancellation, and pausing
  • Waker for push wakeups (such as Postgres LISTEN/NOTIFY)
  • Describer for exposing driver metadata to tooling and the Dev Dashboard
Driver Core + Admin + Unique + Waker + Describer + Manager Transactional Enqueue Coordinated Schedules
quiver (memory) Native Native Native
quiver/sqlite Native Native Native
quiver/postgres Native Native Native
quiver/sqs Native (SQS) Via quiver.WithOutbox(db) Via quiver.WithScheduleDriver(...)

Operating and Testing

Start a dedicated worker process from your compiled app binary, selecting the queues it should drain:

./my-app work --queues=critical,mail

Inspect and control queues from the CLI:

./my-app jobs list
./my-app jobs stats
./my-app jobs retry
./my-app jobs cancel
./my-app jobs pause
./my-app jobs resume

During local development, the Herd Dev Dashboard includes a Jobs tab for viewing queue state, inspecting failures, and triggering actions directly.

Testing with a Virtual Clock

In tests using testkit, advance the virtual clock and flush due jobs synchronously instead of sleeping:

tk.Clock.Advance(25 * time.Hour)
tk.Jobs.RunDue(ctx)

This exercises delayed jobs (quiver.In), retry backoffs, quiver.Snooze, and recurring schedules deterministically.