routine

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 7, 2019 License: MIT Imports: 13 Imported by: 12

README

routine

job routine for golang program with the following strategies:

  • retry
  • repeat
  • crontab
  • guarantee

more details at the quick start.

Quick Start


import (
	"context"
	"errors"
	"log"
    "time"
    
    "github.com/x-mod/routine"
)

func main(){
	err := routine.Main(context.TODO(), routine.ExecutorFunc(func(ctx context.Context) error {
		log.Println("main executing begin ...")

		ch1 := routine.Go(ctx, routine.Retry(3, routine.ExecutorFunc(func(arg1 context.Context) error {
			log.Println("Go1 retry begin ...", routine.FromRetry(arg1))
			time.Sleep(1 * time.Second)
			log.Println("Go1 retry end")
			return errors.New("Go1 error")
		})))
		log.Println("Go1 result: ", <-ch1)

		ch2 := routine.Go(ctx, routine.Repeat(2, time.Second, routine.ExecutorFunc(func(arg1 context.Context) error {
			log.Println("Go2 repeat begin ...", routine.FromRepeat(arg1))
			time.Sleep(2 * time.Second)
			log.Println("Go2 repeat end")
			return nil
		})))
		log.Println("Go2 result: ", <-ch2)

		routine.Go(ctx, routine.Repeat(2, time.Second, routine.Guarantee(routine.ExecutorFunc(func(arg1 context.Context) error {
			log.Println("Go4 repeat guarantee begin ...")
			log.Println("Go4 repeat guarantee end")
			return errors.New("Go4 failed")
		}))))

		routine.Go(ctx, Crontab("* * * * *", routine.ExecutorFunc(func(arg1 context.Context) error {
			log.Println("Go3 crontab begin ...", routine.FromCrontab(arg1))
			log.Println("Go3 crontab end")
			return nil
		})))

		ch5 := routine.Go(ctx, Repeat(3, time.Second, routine.Command("echo", "hello", "routine")))
		log.Println("Go5 result: ", <-ch5)

		ch6 := routine.Go(ctx, routine.Timeout(3*time.Second, Command("sleep", "6")))
		log.Println("Go6 timeout result: ", <-ch6)

		ch7 := routine.Go(ctx, routine.Deadline(time.Now().Add(time.Second), Command("sleep", "6")))
		log.Println("Go7 deadline result: ", <-ch7)

		log.Println("main executing end")
		return nil
	}), routine.DefaultCancelInterruptors...)
	log.Println("main exit: ", err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrNoneExecutor error
	ErrNoneExecutor = errors.New("none executor")
	//ErrNoneContext error
	ErrNoneContext = errors.New("none context")
	//ErrNonePlan error
	ErrNonePlan = errors.New("none plan")
)
View Source
var DefaultCancelInterruptors []Interruptor

DefaultCancelInterruptors include INT/TERM/KILL signals

Functions

func Debug

func Debug(ctx context.Context, args ...interface{})

Debug ctx

func Error

func Error(ctx context.Context, args ...interface{})

Error ctx

func FromArguments

func FromArguments(ctx context.Context) []interface{}

FromArguments extract from context

func FromConcurrent

func FromConcurrent(ctx context.Context) int

FromConcurrent current num

func FromCrontab

func FromCrontab(ctx context.Context) time.Time

FromCrontab current crontab time

func FromEnviron

func FromEnviron(ctx context.Context) []string

FromEnviron get env

func FromRepeat

func FromRepeat(ctx context.Context) int

FromRepeat current repeated times

func FromRetry

func FromRetry(ctx context.Context) int

FromRetry current retied times

func FromStderr

func FromStderr(ctx context.Context) io.Writer

FromStderr get stderr

func FromStdin

func FromStdin(ctx context.Context) io.Reader

FromStdin get stdin

func FromStdout

func FromStdout(ctx context.Context) io.Writer

FromStdout get stdout

func Go

func Go(ctx context.Context, exec Executor) chan error

Go wrapper for go keyword, use in MAIN function

func Info

func Info(ctx context.Context, args ...interface{})

Info ctx

func Main

func Main(parent context.Context, exec Executor, opts ...Opt) error

Main wrapper for executor with waits & signal interuptors

func NewReadCounter

func NewReadCounter(rd io.Reader) io.Reader

func NewWriteCounter

func NewWriteCounter(wr io.Writer) io.Writer

func Trace

func Trace(ctx context.Context, args ...interface{})

Trace ctx

func Wait

func Wait(ctx context.Context)

Wait should be invoked when Executor implemention use Go

func WaitAdd

func WaitAdd(ctx context.Context, delta int)

WaitAdd if context with sync.WaitGroup, wait.Add

func WaitDone

func WaitDone(ctx context.Context)

WaitDone if context with sync.WaitGroup, wait.Done

func Warn

func Warn(ctx context.Context, args ...interface{})

Warn ctx

func WithArgments

func WithArgments(ctx context.Context, args ...interface{}) context.Context

WithArgments inject into context

func WithEnviron

func WithEnviron(ctx context.Context, key string, value string) context.Context

WithEnviron set env

func WithLogger

func WithLogger(ctx context.Context, logger Logger) context.Context

WithLogger context with logger

func WithStderr

func WithStderr(ctx context.Context, out io.Writer) context.Context

WithStderr set stderr

func WithStdin

func WithStdin(ctx context.Context, in io.Reader) context.Context

WithStdin set stdin

func WithStdout

func WithStdout(ctx context.Context, out io.Writer) context.Context

WithStdout set stdout

func WithWait

func WithWait(ctx context.Context) context.Context

WithWait context with sync.WaitGroup, reset WaitGroup

Types

type CancelInterruptor

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

CancelInterruptor definition

func NewCancelInterruptor

func NewCancelInterruptor(sig syscall.Signal) *CancelInterruptor

NewCancelInterruptor if fn is nil will cancel context

func (*CancelInterruptor) Interrupt

func (c *CancelInterruptor) Interrupt() InterruptHandler

Interrupt inplement the interface

func (*CancelInterruptor) Signal

func (c *CancelInterruptor) Signal() syscall.Signal

Signal inplement the interface

type Code

type Code int32

Code for process exit

const (
	// OK is returned on success.
	OK              Code = 0
	GeneralErr      Code = 1
	MisUseErr       Code = 2
	NotExecutable   Code = 126
	IllegalCommand  Code = 127
	InvalidArgments Code = 128
)

func SignalCode

func SignalCode(sig syscall.Signal) Code

SignalCode signal code

func (Code) String

func (i Code) String() string

func (Code) Value

func (c Code) Value() int32

Value of Code

type CommandExecutor

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

CommandExecutor struct

func (*CommandExecutor) Execute

func (cmd *CommandExecutor) Execute(ctx context.Context) error

Execute implement Executor

type ConcurrentExecutor

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

ConcurrentExecutor struct

func (*ConcurrentExecutor) Execute

func (ce *ConcurrentExecutor) Execute(ctx context.Context) error

Execute implement Executor

type CrontabExecutor

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

CrontabExecutor struct

func (*CrontabExecutor) Execute

func (c *CrontabExecutor) Execute(ctx context.Context) error

Execute implement Executor

type DeadlineExecutor

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

DeadlineExecutor struct

func (*DeadlineExecutor) Execute

func (tm *DeadlineExecutor) Execute(ctx context.Context) error

Execute implement Executor

type Executor

type Executor interface {
	//Execute before stopping make sure all subroutines stopped
	Execute(context.Context) error
}

Executor interface definition

func Command

func Command(cmd string, args ...string) Executor

Command new

func Concurrent

func Concurrent(c int, exec Executor) Executor

Concurrent new

func Crontab

func Crontab(plan string, exec Executor) Executor

Crontab new

func Deadline

func Deadline(d time.Time, exec Executor) Executor

Deadline new

func Guarantee

func Guarantee(exec Executor) Executor

Guarantee new

func Repeat

func Repeat(repeat int, interval time.Duration, exec Executor) Executor

Repeat new

func Report

func Report(ch chan *Result, exec Executor) Executor

Report new

func Retry

func Retry(retry int, exec Executor) Executor

Retry new

func Timeout

func Timeout(d time.Duration, exec Executor) Executor

Timeout new

func UseExecutorMiddleware

func UseExecutorMiddleware(exec Executor, middleware ...ExecutorMiddleware) Executor

UseExecutorMiddleware wraps a Executor in one or more middleware.

type ExecutorFunc

type ExecutorFunc func(context.Context) error

ExecutorFunc definition

func (ExecutorFunc) Execute

func (f ExecutorFunc) Execute(ctx context.Context) error

Execute ExecutorFunc implemention of Executor

type ExecutorMiddleware

type ExecutorMiddleware func(Executor) Executor

ExecutorMiddleware is a function that middlewares can implement to be able to chain.

type GoFunc

type GoFunc func(context.Context, Executor) chan error

GoFunc definition

func (GoFunc) Go

func (f GoFunc) Go(ctx context.Context, exec Executor) chan error

Go RunnerFunc implemention of Runner

type GuaranteeExecutor

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

GuaranteeExecutor struct, make sure of none error return

func (*GuaranteeExecutor) Execute

func (g *GuaranteeExecutor) Execute(ctx context.Context) error

Execute implement Executor interface

type InterruptHandler

type InterruptHandler func(ctx context.Context) (exit bool)

InterruptHandler definition

type Interruptor

type Interruptor interface {
	Signal() syscall.Signal
	Interrupt() InterruptHandler
}

Interruptor definition

type Logger

type Logger interface {
	Debug(args ...interface{})
	Trace(args ...interface{})
	Info(args ...interface{})
	Error(args ...interface{})
	Warn(args ...interface{})
}

Logger declare

type Opt

type Opt func(*options)

Opt interface

func Arguments

func Arguments(args ...interface{}) Opt

Arguments Opt for Main

func Cleanup

func Cleanup(exec Executor) Opt

Cleanup Opt for Main

func Interrupts

func Interrupts(ints ...Interruptor) Opt

Interrupts Opt for Main

func Prepare

func Prepare(exec Executor) Opt

Prepare Opt for Main

type ReadCounter

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

func (*ReadCounter) Count

func (rc *ReadCounter) Count() int

func (*ReadCounter) Read

func (rc *ReadCounter) Read(p []byte) (int, error)

type RepeatExecutor

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

RepeatExecutor struct

func (*RepeatExecutor) Execute

func (r *RepeatExecutor) Execute(ctx context.Context) error

Execute implement Executor

type ReportExecutor

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

ReportExecutor struct

func (*ReportExecutor) Execute

func (re *ReportExecutor) Execute(ctx context.Context) error

Execute implement Executor

type Result

type Result struct {
	Err           error
	Code          int
	Begin         time.Time
	Duration      time.Duration
	ContentLength int
}

Result struct

type RetryExecutor

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

RetryExecutor struct

func (*RetryExecutor) Execute

func (retry *RetryExecutor) Execute(ctx context.Context) error

Execute implement Executor interface

type Routine

type Routine interface {
	Go(context.Context, Executor) chan error
}

Routine for Executors

type TimeoutExecutor

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

TimeoutExecutor struct

func (*TimeoutExecutor) Execute

func (tm *TimeoutExecutor) Execute(ctx context.Context) error

Execute implement Executor

type WriteCounter

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

func (*WriteCounter) Count

func (wc *WriteCounter) Count() int

func (*WriteCounter) Write

func (wc *WriteCounter) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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