workers

package module
v0.0.0-...-8e4b344 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2019 License: MIT Imports: 5 Imported by: 1

README

Worker

GoDoc Build Status codecov Go Report Card codebeat badge GolangCI

Package worker adding the abstraction layer around background jobs, allows make a job periodically, observe execution time and to control concurrent execution.

Group of workers allows to control jobs start time and wait until all runned workers finished when we need stop all jobs.

Features

  • Scheduling, use one from existing workers.By* schedule functions. Supporting cron schedule spec format by robfig/cron parser.
  • Graceful stop, wait until all running jobs was completed.

Example

wg := workers.NewGroup(context.Background())
wg.Add(
    workers.
        New(func(context.Context) {}).
        ByTicker(time.Second),

    workers.
        New(func(context.Context) {}).
        ByTimer(time.Second),

    workers.
        New(func(context.Context) {}).
        ByCronSpec("@every 1s"),
)
wg.Run()

See more examples here

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrGroupStopped = errors.New("group is already stopped")

ErrGroupStopped group error message already stopped

Functions

This section is empty.

Types

type Group

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

Group of workers controlling background jobs execution allows graceful stop all running background jobs

func NewGroup

func NewGroup(ctx context.Context) *Group

NewGroup yield new workers group

func (*Group) Add

func (g *Group) Add(workers ...*Worker) error

Add workers to group, if group runned then start worker immediately

func (*Group) AddGroup

func (g *Group) AddGroup(groups ...*Group) error

AddGroup add groups to group as child

func (*Group) OnDemand

func (g *Group) OnDemand(worker *Worker) *onDemand

OnDemand link worker with group and then run by on demand

func (*Group) Run

func (g *Group) Run()

Run starting each worker in separate goroutine with wait.Group control

func (*Group) Stop

func (g *Group) Stop()

Stop cancel workers context

func (*Group) Wait

func (g *Group) Wait(ctx context.Context) (err error)

Wait until all runned workers was completed. Be careful! It can be deadlock if some worker hanging

type Job

type Job func(context.Context)

Job is target background job

type LockFunc

type LockFunc func(context.Context, Job) Job

LockFunc is job wrapper for control exclusive execution

func WithLock

func WithLock(l Locker) LockFunc

WithLock returns func with call Worker in lock

type Locker

type Locker interface {
	Lock() error
	Unlock()
}

Locker interface

type ScheduleFunc

type ScheduleFunc func(context.Context, Job) Job

ScheduleFunc is job wrapper for implement job run schedule

func ByCronSchedule

func ByCronSchedule(schedule string) ScheduleFunc

ByCronSchedule returns job wrapper func for run job by cron schedule using robfig/cron parser for parse cron spec. If schedule spec not valid throw panic, shit happens.

func ByTicker

func ByTicker(period time.Duration) ScheduleFunc

ByTicker returns func which run Worker by ticker each period duration

func ByTimer

func ByTimer(period time.Duration) ScheduleFunc

ByTimer returns job wrapper func for run job each period duration after previous run completed

type Worker

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

Worker is builder for job with optional schedule and exclusive control

func New

func New(job Job) *Worker

New returns new worker with target job

func (*Worker) ByCronSpec

func (w *Worker) ByCronSpec(spec string) *Worker

ByCronSpec set schedule job wrapper by cron spec

func (*Worker) BySchedule

func (w *Worker) BySchedule(s ScheduleFunc) *Worker

BySchedule set schedule wrapper func for job

func (*Worker) ByTicker

func (w *Worker) ByTicker(period time.Duration) *Worker

ByTicker set schedule ticker job wrapper with period

func (*Worker) ByTimer

func (w *Worker) ByTimer(period time.Duration) *Worker

ByTimer set schedule timer job wrapper with period

func (*Worker) Run

func (w *Worker) Run(ctx context.Context)

Run job, wrap job to lock and schedule wrappers

func (*Worker) RunOnce

func (w *Worker) RunOnce(ctx context.Context)

RunOnce job, wrap job to lock

func (*Worker) SetImmediately

func (w *Worker) SetImmediately(executeOnRun bool) *Worker

SetImmediately set execute job on Run setting

func (*Worker) WithDone

func (w *Worker) WithDone(done func()) *Worker

WithDone set job with defer custom function

func (*Worker) WithLock

func (w *Worker) WithLock(l Locker) *Worker

WithLock set job lock wrapper

Directories

Path Synopsis
examples
chain command
grace command
locker command
ondemand command
simple command

Jump to

Keyboard shortcuts

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