job

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: Apache-2.0 Imports: 4 Imported by: 1

README

job

Golang library for job scheduling.

About The Project

Job - small library, that provides api for job periodic execution. Package has some benefits unlike known implementations:

  • laconic api
  • context package support
  • easy customization

Usage

Run with specified period:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Period(time.Second))
j.Start()

Run with specified interval (difference between period and interval is explained in section):

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Interval(time.Second))
j.Start()

Run without initial delay:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Delay(0, job.Period(time.Second)))
j.Start()

Run with specified initial delay:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Delay(time.Second, job.Period(time.Second)))
j.Start()

Run at specific time:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.At(time.UnixMilli(1671040090920), job.Period(time.Second)))
j.Start()

Run yearly:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Yearly(time.February, 18, 10, 0, 0))
j.Start()

Run monthly:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Monthly(15, 10, 30, 0))
j.Start()

Run every last day of the month:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Monthly(-1, 0, 0, 0))
j.Start()

Run weekly:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Weekly(time.Monday, 11, 0, 0))
j.Start()

Run daily:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Daily(23, 30, 0))
j.Start()

Run hourly:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Hourly(30, 0))
j.Start()

Making complex timetable:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Timetable(
	job.Monthly(10, 10, 0, 0, 0),
	job.Monthly(25, 10, 0, 0, 0),
))
j.Start()

Using execution context:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Period(time.Second))
j.StartContext(ctx)

Stop job:

j := job.New(func(ctx context.Context) {
	fmt.Println("knock, knock (:")
}, job.Period(time.Second))
j.Start()
//...
j.Stop()

Underwater rocks

Interval vs Period

Interval - duration between last tick and next tick including payload execution time. Period - duration between last tick and next tick excluding payload execution time.

This differance can be shown on the timeline:

|-----delay-----||---payload---||---delay---||-----payload-----|
|-----------interval-----------||-----------interval-----------|

|----delay-----||-------payload-------||----delay-----||---payload---|
|----period----|                       |----period----|
Job restarting

There is no special api for job restart. To get around this limitation, we can create new Job instance with appropriate parameters and start it.

Similar projects

License

Job is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Contact

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtStrategy

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

func At

func At(time time.Time, strategy Strategy) *AtStrategy

func (*AtStrategy) Tick

func (s *AtStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type DailyStrategy

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

func Daily

func Daily(hour int, minute int, second int) DailyStrategy

func (DailyStrategy) Tick

func (s DailyStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type DelayStrategy

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

func Delay

func Delay(delay time.Duration, strategy Strategy) *DelayStrategy

func (*DelayStrategy) Tick

func (s *DelayStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type FunctionStrategy

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

func Function

func Function(f StrategyFunc) FunctionStrategy

func (FunctionStrategy) Tick

func (s FunctionStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type HourlyStrategy

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

func Hourly

func Hourly(minute int, second int) HourlyStrategy

func (HourlyStrategy) Tick

func (s HourlyStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type IntervalStrategy

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

func Interval

func Interval(interval time.Duration) IntervalStrategy

func (IntervalStrategy) Tick

func (s IntervalStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type Job

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

func New

func New(payload Payload, strategy Strategy) (job *Job)

func (*Job) Done

func (j *Job) Done() (done <-chan struct{})

func (*Job) Start

func (j *Job) Start()

func (*Job) StartContext

func (j *Job) StartContext(ctx context.Context)

func (*Job) Stop

func (j *Job) Stop()

func (*Job) StopContext

func (j *Job) StopContext(ctx context.Context)

type MonthlyStrategy

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

func Monthly

func Monthly(day int, hour int, minute int, second int) MonthlyStrategy

func (MonthlyStrategy) Tick

func (s MonthlyStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type Payload

type Payload func(ctx context.Context)

type PeriodStrategy

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

func Period

func Period(period time.Duration) PeriodStrategy

func (PeriodStrategy) Tick

func (s PeriodStrategy) Tick(_ time.Time) (nextTickTime time.Time)

type Strategy

type Strategy interface {
	Tick(lastTickTime time.Time) (nextTickTime time.Time)
}

type StrategyFunc

type StrategyFunc func(lastTickTime time.Time) (nextTickTime time.Time)

type TimetableStrategy

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

func Timetable

func Timetable(timetable ...Strategy) TimetableStrategy

func (TimetableStrategy) Tick

func (s TimetableStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type WeeklyStrategy

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

func Weekly

func Weekly(day time.Weekday, hour int, minute int, second int) WeeklyStrategy

func (WeeklyStrategy) Tick

func (s WeeklyStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

type YearlyStrategy

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

func Yearly

func Yearly(month time.Month, day int, hour int, minute int, second int) YearlyStrategy

func (YearlyStrategy) Tick

func (s YearlyStrategy) Tick(lastTickTime time.Time) (nextTickTime time.Time)

Jump to

Keyboard shortcuts

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