worker

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 5 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 worker runs job handlers wherever a worker process is listening.

A Worker subscribes to one job queue (job.queue.{service}.{name}) in a NATS queue group, so when several processes host the same worker each job is delivered to exactly one of them. Handlers run on the subscription's goroutine, one job at a time per process; host more processes to run more jobs concurrently.

w := worker.New(&worker.Options{Service: "media", JobName: "thumbnail", MC: mc, Logger: l})
w.Run(func(j *job.Job) (string, error) {
    var p thumbnailArgs
    if err := j.DecodePayload(&p); err != nil { return "", err }
    return render(p)
})

Run reports started, then complete or error, on job.status.{id}. Subscribe is the raw form for handlers that report their own status.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(j *job.Job) (string, error)

Handler does the work of one job and returns its result text.

type Options

type Options struct {
	Service    string
	JobName    string
	MC         *messenger.Client
	Logger     *logger.Logger
	QueueGroup string
}

Options configure a Worker. MC is required. QueueGroup defaults to messenger.QUEUE_GROUP ("workers"); Logger defaults to a platform/worker logger.

type Worker

type Worker struct {
	Service string `json:"service"`
	JobName string `json:"job_name"`
	// contains filtered or unexported fields
}

Worker takes jobs for one service/name pair from the queue.

func New

func New(o *Options) *Worker

New builds a Worker; nothing is subscribed until Subscribe or Run.

func (*Worker) DispatchStatus

func (w *Worker) DispatchStatus(j *job.Job, opts *job.StatusOptions) error

DispatchStatus publishes a status update for j through the worker's client.

func (*Worker) Run

func (w *Worker) Run(h Handler) (*nats.Subscription, error)

Run subscribes with a handler whose lifecycle the worker reports: started before the handler runs, then complete with its result or error with the failure. A handler panic is reported as an error and does not take the worker down.

func (*Worker) Subject

func (w *Worker) Subject() string

Subject is the queue subject this worker listens on.

func (*Worker) Subscribe

func (w *Worker) Subscribe(cb func(j *job.Job)) (*nats.Subscription, error)

Subscribe joins the queue group and hands every delivered job to cb. The callback is responsible for reporting status (see DispatchStatus).

Jump to

Keyboard shortcuts

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