runner

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package runner is the loop every worker runs: register with the backend, lease jobs, run the handler of the job's kind while renewing the lease, and report how it ended. It knows nothing about providers; a provider package registers handlers for its kinds with Handle.

The fail-safes a worker contributes: the attempt runs under the backend's time limit; a lost lease or a cancel stops the handler; a handler that panics or returns an unclassified error ends the attempt as a retry (the backend bounds the retries); a worker that is shutting down gives its jobs back instead of leaving them to time out. A worker has no database: everything it learns or must remember goes through the backend.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPermanent

func IsPermanent(err error) bool

IsPermanent reports whether err was marked with Permanent.

func IsRetryable

func IsRetryable(err error) (bool, time.Duration)

IsRetryable reports whether err was marked with Retryable, and the wait it asked for.

func Permanent

func Permanent(err error) error

Permanent marks err as one that no retry can fix.

func Retryable

func Retryable(err error, after time.Duration) error

Retryable marks err as temporary; after is the wait the provider asked for (0: none).

Types

type Handler

type Handler func(ctx context.Context, job *Job) (result any, err error)

Handler runs one job. It returns the result (anything JSON-encodable, or nil) or an error. Return Permanent(err) for a problem trying again cannot fix (a wrong password, a revoked token, a bad request), Retryable(err, after) for a temporary one with a wait the provider asked for; any other error counts as temporary. The error text reaches the administrator and sometimes the person: never put a credential in it. Stop when ctx ends.

type Job

type Job struct {
	workerapi.Job
	// Config is the administrator's configuration of the job's kind, as it was when the job started.
	Config workerapi.KindConfig
	// contains filtered or unexported fields
}

Job is what a handler works on.

func (*Job) Progress

func (j *Job) Progress(v any)

Progress reports how far the job is (any JSON-encodable value the person can be shown). It is sent with the next heartbeat.

func (*Job) SaveCredentials

func (j *Job) SaveCredentials(ctx context.Context, v any) error

SaveCredentials hands the backend credentials the job renewed (a refreshed OAuth token pair) and returns once they are stored. Do it before using them: a refresh token works once.

type Options

type Options struct {
	ID          string // unique per process; empty: random
	Version     string
	Concurrency int // jobs run at once by this process (default 2)
	// Poll is how long a lease request waits for a job (default workerapi.MaxWaitSeconds).
	Poll time.Duration
	// ConfigEvery is how often the configuration is re-read (default 15 s).
	ConfigEvery time.Duration
}

Options configure a Runner.

type Runner

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

Runner is one worker process.

func New

func New(client *workerapi.Client, opt Options) *Runner

New returns a Runner. Register the handlers with Handle before Run.

func (*Runner) Handle

func (r *Runner) Handle(kind string, h Handler)

Handle registers the handler of a kind of job.

func (*Runner) ID

func (r *Runner) ID() string

ID is the name this process registered under.

func (*Runner) Ready

func (r *Runner) Ready(maxAge time.Duration) error

Ready reports whether the worker is registered and has heard from the backend within maxAge: the readiness check of the worker's health listener. A worker that waits for a job is in contact all the time (it asks, and the backend answers when there is none), so a long silence means the backend or the link is down.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) error

Run works until ctx ends. It returns only an error that no waiting will fix.

Jump to

Keyboard shortcuts

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