routine

package module
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2019 License: MIT Imports: 11 Imported by: 12

README

routine

GoDoc Go Report Card Build Status Version Coverage Status

go routine control with context, support: Main, Go, Pool and some useful Executors.

Main

package main

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

func foo(ctx context.Context) error {
	log.Println("foo begin")
	log.Println("foo end")
	return errors.New("foo done")
}

func main(){
	err := routine.Main(
		context.TODO(),
		routine.Concurrent(4, routine.ExecutorFunc(foo)),
		routine.Cleanup(routine.ExecutorFunc(func(ctx context.Context) error {
			log.Println("clear")
			return nil
		})),
	)
	if err != nil {
		log.Println("failed:", err)
	}
}

Routine


import "github.com/x-mod/routine"

err := routine.New(execute).Execute(ctx)

Executors


import "github.com/x-mod/routine"

crontab := routine.Crontab("* * * * *", execute)

Enjoy

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 ArgumentsFrom added in v1.1.0

func ArgumentsFrom(ctx context.Context) ([]interface{}, bool)

ArgumentsFrom extract from context

func FromCrontab

func FromCrontab(ctx context.Context) time.Time

FromCrontab current crontab time

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 Main

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

func WithArgments

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

WithArgments inject into context

func WithParent added in v1.2.0

func WithParent(ctx context.Context, parent *Routine) context.Context

Types

type AppendExecutor added in v1.2.6

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

AppendExecutor

func Append added in v1.2.6

func Append(execs ...Executor) *AppendExecutor

Append new

func (*AppendExecutor) Append added in v1.2.6

func (ae *AppendExecutor) Append(execs ...Executor)

func (*AppendExecutor) Execute added in v1.2.6

func (ae *AppendExecutor) Execute(ctx context.Context) error

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 CommandExecutor

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

CommandExecutor struct

func Command

func Command(cmd string, opts ...CommandOpt) *CommandExecutor

Command new

func (*CommandExecutor) Execute

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

Execute implement Executor

type CommandOpt added in v1.2.5

type CommandOpt func(*CommandExecutor)

func ARG added in v1.2.5

func ARG(arg string) CommandOpt

func ENV added in v1.2.5

func ENV(env string) CommandOpt

func Stderr added in v1.2.5

func Stderr(wr io.Writer) CommandOpt

func Stdin added in v1.2.5

func Stdin(rd io.Reader) CommandOpt

func Stdout added in v1.2.5

func Stdout(wr io.Writer) CommandOpt

type ConcurrentExecutor

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

ConcurrentExecutor struct

func Concurrent

func Concurrent(c int, exec Executor) *ConcurrentExecutor

Concurrent new

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 Crontab

func Crontab(plan string, exec Executor) *CrontabExecutor

Crontab new

func (*CrontabExecutor) Everyday added in v1.2.2

func (c *CrontabExecutor) Everyday(flag bool)

func (*CrontabExecutor) Execute

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

Execute implement Executor

func (*CrontabExecutor) IsTimeMuted added in v1.2.2

func (c *CrontabExecutor) IsTimeMuted(tm time.Time) bool

func (*CrontabExecutor) Mute added in v1.2.2

func (c *CrontabExecutor) Mute(begin time.Time, end time.Time)

func (*CrontabExecutor) Weekend added in v1.2.2

func (c *CrontabExecutor) Weekend(flag bool)

func (*CrontabExecutor) Workday added in v1.2.2

func (c *CrontabExecutor) Workday(flag bool)

type CrontabMute added in v1.2.2

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

type DeadlineExecutor

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

DeadlineExecutor struct

func Deadline

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

Deadline new

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 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 GuaranteeExecutor

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

GuaranteeExecutor struct, make sure of none error return

func Guarantee

func Guarantee(exec Executor) *GuaranteeExecutor

Guarantee insure exec NEVER PANIC

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 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 ParallelExecutor added in v1.2.1

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

ParallelExecutor

func Parallel added in v1.2.1

func Parallel(execs ...Executor) *ParallelExecutor

Parallel new

func (*ParallelExecutor) Execute added in v1.2.1

func (pe *ParallelExecutor) Execute(ctx context.Context) error

Execute implement Executor

type RepeatExecutor

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

RepeatExecutor struct

func Repeat

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

Repeat new

func (*RepeatExecutor) Execute

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

Execute implement Executor

type RetryExecutor

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

RetryExecutor struct

func Retry

func Retry(retry int, exec Executor) *RetryExecutor

Retry new

func (*RetryExecutor) Execute

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

Execute implement Executor interface

type Routine

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

func New added in v1.2.0

func New(exec Executor, opts ...Opt) *Routine

func ParentFrom added in v1.2.0

func ParentFrom(ctx context.Context) (*Routine, bool)

func (*Routine) Execute added in v1.2.0

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

func (*Routine) Go

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

func (*Routine) Stop added in v1.2.0

func (r *Routine) Stop()

type TimeoutExecutor

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

TimeoutExecutor struct

func Timeout

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

Timeout new

func (*TimeoutExecutor) Execute

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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