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
- Variables
- func NewID() string
- func QueueSubject(service, name string) string
- func RequestSubject(service, name string) string
- func StatusSubject(id string) string
- func SubscribeRequests(mc *messenger.Client, cb func(*Job)) (*nats.Subscription, error)
- func SubscribeStatuses(mc *messenger.Client, cb func(*Status)) (*nats.Subscription, error)
- type Job
- func (j *Job) DecodePayload(v interface{}) error
- func (j *Job) Dispatch(mc *messenger.Client) error
- func (j *Job) DispatchStatus(mc *messenger.Client, opts *StatusOptions) error
- func (j *Job) Enqueue(mc *messenger.Client) error
- func (j *Job) QueueSubject() string
- func (j *Job) RequestSubject() string
- func (j *Job) StatusSubject() string
- func (j *Job) SubscribeStatus(mc *messenger.Client, cb func(*Status)) (*nats.Subscription, error)
- type State
- type Status
- type StatusOptions
Constants ¶
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 ¶
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 ¶
QueueSubject is where workers for service/name take jobs from.
func RequestSubject ¶
RequestSubject is where producers publish a job for service/name.
func StatusSubject ¶
StatusSubject carries the status updates of one job.
func SubscribeRequests ¶
SubscribeRequests delivers every job request to cb; a queue relay uses it.
func SubscribeStatuses ¶
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 (*Job) DecodePayload ¶
DecodePayload converts the generic decoded payload into v (a pointer to the producer's payload type) by round-tripping it through JSON.
func (*Job) DispatchStatus ¶
func (j *Job) DispatchStatus(mc *messenger.Client, opts *StatusOptions) error
DispatchStatus publishes a status update for the job.
func (*Job) QueueSubject ¶
QueueSubject is the subject Enqueue publishes this job on.
func (*Job) RequestSubject ¶
RequestSubject is the subject Dispatch publishes this job on.
func (*Job) StatusSubject ¶
StatusSubject is the subject this job's status updates travel on.
func (*Job) SubscribeStatus ¶
SubscribeStatus delivers every status update for this job to cb. Callers unsubscribe through the returned subscription when they stop caring.
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.