bg

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 12 Imported by: 1

Documentation

Overview

Package bg aims to manage all background jobs run on spine. This allows to keep the number of background tasks in control and drain them properly when it is time to stop a spine.

Background jobs will typically be potentially long operations, such as uploading files, sending emails or push notifications. It can also be used for services that run infinitely like heartbeat signals or stats worker.

Package bg guarantee that a dispatched job will be started even a registry is being asked to drain right after. However, there is a slim chance that Stop() is called before Start().

Index

Constants

This section is empty.

Variables

View Source
var ErrDrain = errors.New("registry is draining")

ErrDrain is the error returned when a new job attempts to be started during and the registry is draining

View Source
var ErrDup = errors.New("job has already been registered")

ErrDup is the error returned when a new job has already been registered

Functions

func BG

func BG(parent context.Context, f func(ctx context.Context)) error

func Dispatch

func Dispatch(ctx context.Context, j Job) error

Dispatch calls `Dispatch` on the context `Registry`

func RegWithContext

func RegWithContext(ctx context.Context, r *Reg) context.Context

RegWithContext returns a copy of parent in which `Reg` is stored

Types

type Job

type Job interface {
	Start()
	Stop()
}

Job is a an interface to implement to be a background job

type Reg

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

Reg (registry) holds a list of running jobs

func NewReg

func NewReg(service string, ctx context.Context) *Reg

NewReg builds a new registry

func RegFromContext

func RegFromContext(ctx context.Context) *Reg

RegFromContext returns a `Reg` instance associated with `ctx`, or a new `Reg` if no existing `Reg` instance could be found.

func (*Reg) Dispatch

func (r *Reg) Dispatch(j Job) error

Dispatch registers the given job and runs it in background

func (*Reg) Drain

func (r *Reg) Drain()

Drain sends a Stop() signal to all registered jobs and rejects new jobs

type Task

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

Task is a background job for simple long running tasks

func NewTask

func NewTask(f func()) *Task

NewTask returns a new Task

func (*Task) Start

func (t *Task) Start()

func (*Task) Stop

func (t *Task) Stop()

Jump to

Keyboard shortcuts

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