cronalt

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2021 License: MPL-2.0 Imports: 6 Imported by: 0

README

cronalt

Golang job scheduling

  • Runs in-process
  • Type-safe (avoids reflection)
  • Pluggable and extensible

cronalt is inspired by gocron

Install using go modules

go get github.com/ahmedalhulaibi/cronalt@v1.0.0

Getting started

See internal/examples/basic directory for a simple example

Concepts

Job

The job is simply aware of the task it's provided. It is defined as an interface. Jobs can be extended/wrapped using a decorator to extend functionality.

Job Timer

A Job Timer informs the Scheduler how long to wait before triggering the next run for a given Job. The Job Timer is also defined as an interface to allow for irregular scheduling.

Scheduler

The Scheduler orchestrates all Jobs. It starts all the jobs and stops all the jobs. For each job, an individual goroutine is kicked off with its Job Timer informing the routine how the job should be scheduled.

How Do I...?

How do I lock a job in a distributed system (K8s, Nomad, etc.)?

Decorate your job with a locker implementation.

See internal/examples/redsync for an example.

How do I handle an error if my job fails?

Decorate your job with an error handler implementation.

See internal/examples/errorhandler for an example.

How do I stop the scheduler if a job keeps failing?

Decorate all your jobs with a circuit breaker which cancels the parent context. This will stop the entire process, not just the individual routine.

See internal/examples/circuitbreaker for an example

How do I propagate custom fields in context?

Decorate your job with a context decorator.

See internal/examples/runid for an example

How do I dynamically add and remove jobs?

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxConcurrentJobsZero error = fmt.Errorf("maxConcurrentJobs must be greater than zero")
)

Functions

func Every

func Every(d time.Duration) durationTimer

Types

type KeyVal

type KeyVal struct {
	Key string
	Val interface{}
}

type Scheduler

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

func NewScheduler

func NewScheduler(maxConcurrentJobs int, opts ...SchedulerOption) (*Scheduler, error)

func (*Scheduler) Schedule

func (s *Scheduler) Schedule(jt job.Timer, j job.Job) error

Schedule registers a job and uses job function name as the job name

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context)

Start starts all the scheduled jobs in their own go routine and blocks indefinitely or until context is cancelled

type SchedulerOption

type SchedulerOption func(s *Scheduler) *Scheduler

func WithClock

func WithClock(t clock) SchedulerOption

WithClock returns a SchedulerOption to inject a clock, default is time.Now

func WithJobStore

func WithJobStore(js jobStore) SchedulerOption

WithJobStore returns a SchedulerOption to inject a jobStore, default is job.store

func WithLogger

func WithLogger(l logger) SchedulerOption

WithLogger returns a SchedulerOption to inject a logger

func WithWaitGroup

func WithWaitGroup(wg waitGroup) SchedulerOption

WithWaitGroup returns a SchedulerOption to inject a waitgroup, default is sync.WaitGroup This can be used to inject your own instance of sync.WaitGroup

Jump to

Keyboard shortcuts

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