daemon

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT Imports: 8 Imported by: 0

README

daemon

Build Status the logic to manage the background processes GoDoc

install

$ go get github.com/go-4devs/daemon@latest

basic usage

package main

import (
	"context"
	"log"
	"time"
	
	"github.com/go-4devs/daemon"
)


func main() {
    ctx := context.Background()
	logErr := daemon.NewMiddleware(func(ctx context.Context, next daemon.Run) error {
	    err := next(ctx)
	    if err != nil{	    	
	       log.Println(err)
	    }
	    return err
	},nil)
    manager := daemon.NewManager(daemon.WithMMiddleware(logErr))
    
    job := daemon.NewJob(func(ctx context.Context) error {
        // my some job
        return nil
    })
    //do job immediately and run once a minute
    manager.DoCtx(ctx, job, daemon.WithFreq(time.Minute))
    
    //do another job after 1 hour and run once a second
    manager.DoCtx(ctx, job, daemon.WithDelay(time.Hour), daemon.WithFreq(time.Second))
    
    //single run job
    jobWithClose := daemon.NewJob(func(ctx context.Context) error {
        // my some logic
        return daemon.StopJob(nil)
    }, daemon.WithJClose(func(ctx context.Context) error {
    	// do some logic after job stop
        return nil
    }))
   
    manager.DoCtx(ctx, jobWithClose)
    
    // wait all jobs
    manager.Wait()
}

Documentation

Overview

Package daemon for the run job background and manage them.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DelayJob

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

DelayJob update delay next Run job

func StopJob

func StopJob(err error) error

StopJob stop job

Types

type Handle

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

Handle middleware interface

type JOption

type JOption func(*job)

JOption configure job

func WithJClose

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

WithJClose set func which execute after job stop

func WithJMiddleware

func WithJMiddleware(f ...Middleware) JOption

WithJMiddleware add middleware to job

func WithJName

func WithJName(name string) JOption

WithJName set job name

func WithJOption

func WithJOption(opts ...Option) JOption

WithJOption set options job

type Job

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

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

NewJob create new job with options

Example
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()
Output:

Example (Delay)

run job after delay once ignore freq

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()
Output:

Example (Option)

all option may replace then DoCtx

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()
Output:

Example (Stop)
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()
Output:

Example (WithClose)
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()
Output:

Example (WithMiddleware)
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()
Output:

type MOption

type MOption func(*manager)

MOption configure manager

func WithHandleCloseErr

func WithHandleCloseErr(f func(err error)) MOption

WithHandleCloseErr Handle close err

func WithMMiddleware

func WithMMiddleware(m ...Middleware) MOption

WithMMiddleware set middleware to manager

type Manager

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

Manager monitoring do job

func NewManager

func NewManager(opts ...MOption) Manager

NewManager create new manager with options

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

m.DoCtx(ctx, 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()
Output:

Example (WithClose)
ctx := context.Background()
m := NewManager()
defer func() {
	_ = m.Close()
}()

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

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

Example (WithOptions)
ctx := context.Background()

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(ctx, j, WithFreq(time.Minute))
Output:

type Middleware

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

Middleware midleware to set manager or job

func NewMiddleware

func NewMiddleware(run Handle, close Handle) Middleware

NewMiddleware create new Middleware

func RetryMiddleware

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

RetryMiddleware set retry job and change return after max retry

type Option

type Option func(*config)

Option for the do job

func WithDelay

func WithDelay(delay time.Duration) Option

WithDelay set delay Run job

func WithFreq

func WithFreq(freq time.Duration) Option

WithFreq set Frequency Run job

func WithHandleErr

func WithHandleErr(f func(err error)) Option

WithHandleErr replace Handle errors

func WithSchedule added in v0.2.0

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

WithSchedule set delay and frequency Run job

type Run

type Run func(ctx context.Context) error

Run init function for the change state

type Setting

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

Setting job settings

type Timer

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

Timer for the Run job

func NewTicker

func NewTicker(freq time.Duration) Timer

NewTicker create new ticker based on time.ticker

Jump to

Keyboard shortcuts

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