Categorygithub.com/chapsuk/worker
modulepackage
1.0.0
Repository: https://github.com/chapsuk/worker.git
Documentation: pkg.go.dev

# README

Worker

GoDoc Build Status codecov Go Report Card codebeat badge

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 worker.By* schedule functions. Supporting cron schedule spec format by robfig/cron parser.
  • Control concurrent execution around multiple instances by worker.WithLock. See existing lockers
  • Observe a job execution time duration with worker.SetObserever. Friendly for prometheus/client_golang package.
  • Graceful stop, wait until all running jobs was completed.

Example

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

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

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

Lockers

You can use redis locks for controll exclusive job execution:

l := locker.NewRedis(radix.Client, "job_lock_name", locker.RedisLockTTL(time.Minute))

w := worker.
        New(func(context.Context) {}).
        WithLock(l)

// Job will be executed only if `job_lock_name` redis key not exists.
w.Run(context.Background())

# Packages

No description provided by the author

# Functions

ByCronSchedule returns job wrapper func for run job by cron schedule using robfig/cron parser for parse cron spec.
ByTicker returns func which run Worker by ticker each period duration.
ByTimer returns job wrapper func for run job each period duration after previous run completed.
New returns new worker with target job.
NewGroup yield new workers group.
WithLock returns func with call Worker in lock.

# Structs

Group of workers controlling background jobs execution allows graceful stop all running background jobs.
Worker is builder for job with optional schedule and exclusive control.

# Interfaces

Locker interface.

# Type aliases

Job is target background job.
LockFunc is job wrapper for control exclusive execution.
ObserveFunc given execution job time duration seconds.
ScheduleFunc is job wrapper for implement job run schedule.