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 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 (*Worker) DispatchStatus ¶
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.