job

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package job defines the public contract every background job module must implement. Jobs are stateless top-level RunFuncs — one call to app.RegisterJob binds a Meta, a typed Config value, and a RunFunc. The RunFunc receives a context.Context that carries a *Ctx (via FromContext) so Run can read its own runtime-editable config without any extra plumbing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateJobs

func ValidateJobs(mods []Module) error

ValidateJobs is called by wick at boot. It checks that each Module's Meta has the minimum required fields (Key, Name, Icon), that Run is non-nil, and that no two jobs share the same Key. A non-nil error means the process should refuse to start.

func WithCtx

func WithCtx(parent context.Context, c *Ctx) context.Context

WithCtx attaches a Ctx to a context.Context. The scheduler calls this before Run so handlers can recover the Ctx via FromContext.

Types

type Ctx

type Ctx struct {
	// contains filtered or unexported fields
}

Ctx is the per-run handle wick passes to a job through context. It carries the owning job's Key and a view onto the configs service so Run() can read its own runtime-editable config without re-plumbing the service down from main.

Construction is internal to wick — jobs receive a ready-made Ctx via job.FromContext(ctx) inside Run.

func FromContext

func FromContext(ctx context.Context) *Ctx

FromContext returns the Ctx attached to ctx, or a no-op Ctx when none is present (e.g. unit tests that call Run directly with a bare context). A no-op Ctx returns "" for every Cfg read, which matches the behavior of a tool that declares no configs.

func NewCtx

func NewCtx(owner string, cfg cfgReader) *Ctx

NewCtx is used by the manager package to build a Ctx before calling Run. Downstream code should never call this — read the existing Ctx with FromContext instead.

func (*Ctx) Cfg

func (c *Ctx) Cfg(key string) string

Cfg returns the current value of a config field declared by this job. Scoped to the active job's Key — reading another owner's config requires CfgOf. Returns "" when the key is not declared or the config service is unavailable.

func (*Ctx) CfgBool

func (c *Ctx) CfgBool(key string) bool

CfgBool returns c.Cfg(key) parsed as bool. "true"/"1"/"yes"/"on" (case-insensitive) count as true; anything else is false.

func (*Ctx) CfgInt

func (c *Ctx) CfgInt(key string) int

CfgInt returns c.Cfg(key) parsed as int. Unparseable or empty values return 0.

func (*Ctx) CfgOf

func (c *Ctx) CfgOf(owner, key string) string

CfgOf reads a config value from another owner (a tool or another job). Intentionally verbose — reserved for cross-owner reads that need a neighbor's endpoint or shared identifier.

func (*Ctx) Owner

func (c *Ctx) Owner() string

Owner returns the job Key this Ctx is scoped to. Useful for logging — Cfg already applies the scope for reads.

type Meta

type Meta struct {
	Key         string
	Name        string
	Description string
	Icon        string
	DefaultCron string
	// DefaultTags works the same as tool.Tool.DefaultTags — seeded on
	// startup, linked to the job's path, and used for tag-based access
	// control in the admin UI.
	DefaultTags []tool.DefaultTag
	// AutoEnable forces Job.Enabled = true on every Bootstrap. Intended
	// for built-in maintenance jobs that ship pre-enabled and have no
	// user-facing toggle (typically combined with DefaultTags carrying
	// the System tag so the row is hidden from non-admin manager UI).
	// Admin-managed jobs leave this false — admins enable from the
	// /manager/jobs UI.
	AutoEnable bool
}

Meta holds the static metadata for a job instance. Downstream code passes one Meta per app.RegisterJob call — duplicating a module with a different Key + Meta registers a second scheduled instance backed by the same RunFunc.

type Module

type Module struct {
	Meta    Meta
	Configs []entity.Config
	Run     RunFunc
}

Module is the internal, fully-resolved registration record wick keeps for every job. It is produced by app.RegisterJob — the Meta, any configs reflected from the paired typed struct, and the RunFunc itself. Downstream code does not construct Module directly.

type RunFunc

type RunFunc func(ctx context.Context) (string, error)

RunFunc is the job-side run signature. It executes the job and returns a markdown-formatted result string. An empty string means "no output to display". Errors should be returned via the error value, not embedded in the result.

ctx carries a *Ctx (via FromContext) so Run can read this instance's runtime config, e.g. job.FromContext(ctx).Cfg("endpoint"). When the job needs process-wide state (a DB handle, an HTTP client, a cache), wrap RunFunc in a factory: func NewRun(db *sql.DB) job.RunFunc { ... }.

Jump to

Keyboard shortcuts

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