job

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package job defines the unit of asynchronous work that a service hands to workers over NATS, the status a worker reports back, and the subjects the exchange runs on. Package worker is the receiving side.

Subjects, all inside the tenant account the messenger client is bound to:

job.request.{service}.{name}  a producer asks for a job to run (Dispatch)
job.queue.{service}.{name}    a job offered to the worker queue group;
                              exactly one subscribed worker receives it
job.status.{id}               a worker reports progress for one job

A queue relay (package queue is one) subscribes to every request, records it, and republishes it with Enqueue. A producer that does not need that bookkeeping can call Enqueue itself and skip the relay.

Index

Constants

View Source
const (
	// RequestWildcard matches every job request; a queue relay subscribes here.
	RequestWildcard = "job.request.*.*"
	// StatusWildcard matches every status update; a queue relay records them.
	StatusWildcard = "job.status.*"
)

Variables

View Source
var ErrNotConnected = errors.New("job: messenger client is not connected")

ErrNotConnected is returned when the messenger client has no NATS connection.

Functions

func NewID

func NewID() string

NewID returns a fresh job identifier (a ULID, so IDs sort by creation time).

func QueueSubject

func QueueSubject(service, name string) string

QueueSubject is where workers for service/name take jobs from.

func RequestSubject

func RequestSubject(service, name string) string

RequestSubject is where producers publish a job for service/name.

func StatusSubject

func StatusSubject(id string) string

StatusSubject carries the status updates of one job.

func SubscribeRequests

func SubscribeRequests(mc *messenger.Client, cb func(*Job)) (*nats.Subscription, error)

SubscribeRequests delivers every job request to cb; a queue relay uses it.

func SubscribeStatuses

func SubscribeStatuses(mc *messenger.Client, cb func(*Status)) (*nats.Subscription, error)

SubscribeStatuses delivers every status update of every job to cb.

Types

type Job

type Job struct {
	ID      string      `json:"id"`
	Service string      `json:"service"`
	Name    string      `json:"name"`
	Payload interface{} `json:"payload"`
}

Job is the request that travels from producer to worker. Payload is any JSON-encodable value; on the receiving side it decodes to the generic JSON types, so workers call DecodePayload to get their own struct back.

func New

func New(service, name string, payload interface{}) *Job

New builds a job request for service/name with a fresh ID.

func (*Job) DecodePayload

func (j *Job) DecodePayload(v interface{}) error

DecodePayload converts the generic decoded payload into v (a pointer to the producer's payload type) by round-tripping it through JSON.

func (*Job) Dispatch

func (j *Job) Dispatch(mc *messenger.Client) error

Dispatch publishes the job request for a queue relay to pick up.

func (*Job) DispatchStatus

func (j *Job) DispatchStatus(mc *messenger.Client, opts *StatusOptions) error

DispatchStatus publishes a status update for the job.

func (*Job) Enqueue

func (j *Job) Enqueue(mc *messenger.Client) error

Enqueue offers the job directly to the worker queue, bypassing any relay.

func (*Job) QueueSubject

func (j *Job) QueueSubject() string

QueueSubject is the subject Enqueue publishes this job on.

func (*Job) RequestSubject

func (j *Job) RequestSubject() string

RequestSubject is the subject Dispatch publishes this job on.

func (*Job) StatusSubject

func (j *Job) StatusSubject() string

StatusSubject is the subject this job's status updates travel on.

func (*Job) SubscribeStatus

func (j *Job) SubscribeStatus(mc *messenger.Client, cb func(*Status)) (*nats.Subscription, error)

SubscribeStatus delivers every status update for this job to cb. Callers unsubscribe through the returned subscription when they stop caring.

type State

type State string

State is the lifecycle stage a Status reports.

const (
	StateWaiting   State = "waiting"
	StateStarted   State = "started"
	StateComplete  State = "complete"
	StateCancelled State = "cancelled"
	StateError     State = "error"
)

type Status

type Status struct {
	JobID     string    `json:"job_id"`
	Service   string    `json:"service"`
	JobName   string    `json:"job_name"`
	Timestamp time.Time `json:"timestamp"`
	State     State     `json:"state"`
	Running   bool      `json:"running"`
	Result    string    `json:"result"`
	Errors    []string  `json:"errors,omitempty"`
}

Status is one progress report for a job, published on job.status.{id}.

func NewStatus

func NewStatus(j *Job, opts *StatusOptions) *Status

NewStatus stamps a status update for the job.

type StatusOptions

type StatusOptions struct {
	State   State
	Running bool
	Result  string
	Errors  []error
}

StatusOptions is what a worker supplies for one status update.

Jump to

Keyboard shortcuts

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