daemon: github.com/go-4devs/daemon Index | Examples | Files

package daemon

import "github.com/go-4devs/daemon"

Package daemon for the run job background and manage them.

Index

Examples

Package Files

doc.go error.go job.go manager.go middleware.go option.go timer.go

func DelayJob Uses

func DelayJob(d time.Duration, err error) error

DelayJob update delay next Run job

func GetDelayedJob Uses

func GetDelayedJob(err error) (time.Duration, bool)

GetDelayedJob get delay job

func IsStoppedJob Uses

func IsStoppedJob(err error) bool

IsStoppedJob check stopped job

func StopJob Uses

func StopJob(err error) error

StopJob stop job

type Handle Uses

type Handle func(ctx context.Context, next Run) error

Handle middleware interface

var DefaultHandle Handle = func(ctx context.Context, next Run) error {
    return next(ctx)
}

type JOption Uses

type JOption func(*job)

JOption configure job

func WithJClose Uses

func WithJClose(cl func(ctx context.Context) error) JOption

WithJClose set func which execute after job stop

func WithJMiddleware Uses

func WithJMiddleware(f ...Middleware) JOption

WithJMiddleware add middleware to job

func WithJName Uses

func WithJName(name string) JOption

WithJName set job name

func WithJOption Uses

func WithJOption(opts ...Option) JOption

WithJOption set options job

type Job Uses

type Job interface {
    RunCtx(ctx context.Context) error
    CloseCtx(ctx context.Context) error
    fmt.Stringer
    Configure(opts ...Option) Setting
}

Job interface for the do by manager

func NewJob Uses

func NewJob(r Run, opts ...JOption) Job

NewJob create new job with options

Code:

ctx := context.Background()
m := NewManager()

j := NewJob(func(ctx context.Context) error {
    // do some
    return nil
}, WithJName("my awesome job"))

m.DoCtx(ctx, j)

m.Wait()

run job after delay once ignore freq

Code:

ctx := context.Background()
m := NewManager()

j := NewJob(func(ctx context.Context) error {
    // do some
    return DelayJob(time.Hour, errors.New("some reason"))
})

m.DoCtx(ctx, j)

m.Wait()

all option may replace then DoCtx

Code:

ctx := context.Background()
m := NewManager()

j := NewJob(func(ctx context.Context) error {
    // do some
    return nil
},
    // set freq run job
    WithJOption(WithFreq(time.Minute)),
    // set delay to start job
    WithJOption(WithDelay(time.Minute)),
)

m.DoCtx(ctx, j)

m.Wait()

Code:

ctx := context.Background()
m := NewManager()

j := NewJob(func(ctx context.Context) error {
    // do some
    return StopJob(errors.New("some reason"))
})

m.DoCtx(ctx, j)

m.Wait()

Code:

ctx := context.Background()
m := NewManager()

j := NewJob(func(ctx context.Context) error {
    // do some
    return nil
}, WithJClose(func(ctx context.Context) error {
    // do some after job stop
    return nil
}))

m.DoCtx(ctx, j)

m.Wait()

Code:

ctx := context.Background()
m := NewManager()
mw := NewMiddleware(func(ctx context.Context, next Run) error {
    // do some before run func
    err := next(ctx)
    // do some after run func
    return err
}, func(ctx context.Context, next Run) error {
    // do some before close func
    err := next(ctx)
    // do some after close func
    return err
})
// middleware execute only run func
mwr := NewMiddleware(func(ctx context.Context, next Run) error {
    return next(ctx)
}, nil)

j := NewJob(func(ctx context.Context) error {
    // do some
    return nil
}, WithJMiddleware(mw, mwr))

m.DoCtx(ctx, j)

m.Wait()

type MOption Uses

type MOption func(*manager)

MOption configure manager

func WithHandleCloseErr Uses

func WithHandleCloseErr(f func(err error)) MOption

WithHandleCloseErr Handle close err

func WithMMiddleware Uses

func WithMMiddleware(m ...Middleware) MOption

WithMMiddleware set middleware to manager

type Manager Uses

type Manager interface {
    DoCtx(ctx context.Context, j Job, o ...Option)
    Wait()
    io.Closer
}

Manager monitoring do job

func NewManager Uses

func NewManager(opts ...MOption) Manager

NewManager create new manager with options

Code:

m := NewManager()
j := NewJob(func(ctx context.Context) error {
    // do some job
    return nil
}, WithJName("awesome job"))

m.DoCtx(context.Background(), j,
    // set frequency run job
    WithFreq(time.Minute),
    // set delay for first run job
    WithDelay(time.Second),
    // set handler if run job return err
    WithHandleErr(func(err error) {
        log.Println(err)
    }),
)
m.Wait()

Code:

m := NewManager()
defer func() {
    _ = m.Close()
}()

j := NewJob(func(ctx context.Context) error {
    // do some job
    return nil
}, WithJName("awesome job"))

m.DoCtx(context.Background(), j, WithFreq(time.Minute))
exDone := make(chan struct{})
// some blocked process
<-exDone

Code:


middleware := NewMiddleware(func(ctx context.Context, next Run) error {
    // do some before run all job
    err := next(ctx)
    // do some after run all job
    return err
}, func(ctx context.Context, next Run) error {
    // do some before close job
    err := next(ctx)
    // do some after close job
    return err
})
m := NewManager(
    WithMMiddleware(middleware),
    WithHandleCloseErr(func(err error) {
        // do some if close return err
        log.Println(err)
    }),
)

j := NewJob(func(ctx context.Context) error {
    // do some job
    return nil
}, WithJName("awesome job"))

m.DoCtx(context.Background(), j, WithFreq(time.Minute))

type Middleware Uses

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

Middleware midleware to set manager or job

func NewMiddleware Uses

func NewMiddleware(run, clFnc Handle) Middleware

NewMiddleware create new Middleware

func RetryMiddleware Uses

func RetryMiddleware(max uint8, handleRetry func(err error) error) Middleware

RetryMiddleware set retry job and change return after max retry

type Option Uses

type Option func(*config)

Option for the do job

func WithDelay Uses

func WithDelay(delay time.Duration) Option

WithDelay set delay Run job

func WithFreq Uses

func WithFreq(freq time.Duration) Option

WithFreq set Frequency Run job

func WithHandleErr Uses

func WithHandleErr(f func(err error)) Option

WithHandleErr replace Handle errors

func WithSchedule Uses

func WithSchedule(next func(time.Time) time.Duration) Option

WithSchedule set delay and frequency Run job

type Run Uses

type Run func(ctx context.Context) error

Run init function for the change state

type Setting Uses

type Setting interface {
    IsProcessed(err error) bool
    Do() <-chan time.Time
    Reload(s func(time.Time) time.Duration)
}

Setting job settings

type Timer Uses

type Timer interface {
    Tick() <-chan time.Time
    Reset(d time.Duration)
    Stop()
}

Timer for the Run job

func NewTicker Uses

func NewTicker(freq time.Duration) Timer

NewTicker create new ticker based on time.ticker

Package daemon imports 8 packages (graph). Updated 2019-07-19. Refresh now. Tools for package owners.