routine

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 15 Imported by: 12

README

routine

GoDoc Go Report Card Build Status Version Coverage Status

Routine Architecture

Quick Start

package main

import (
	"log"
	"context"

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

func main(){
	if err := routine.Main(
		context.TODO(),
		routine.Command("echo", routine.ARG("hello routine!")),
	); err != nil {
		log.Fatal(err)
	}
}

Or you can just clone the repo, then running go run quickstart/main.go.

Main Routine

The most functional feature is providing the Main function abstraction, you can use the routine.Main to wrap your main function logic very quickly.

package main

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

func MainGo(ctx context.Context) error {
	log.Println("this is the Main Go func")
	return nil
}
func ChildGo(ctx context.Context) error {
	log.Println("this is the Child Go func")
	return nil
}
func prepareGo(ctx context.Context) error {
	log.Println("this is the prepare Go func")
	return nil
}
func cleanupGo(ctx context.Context) error {
	log.Println("this is the Clean Go func")
	return nil
}

func main(){
	log.Println(
		routine.Main(
			context.TODO(),
			//main Go
			routine.ExecutorFunc(MainGo),
			//prpare Go
			routine.Prepare(routine.ExecutorFunc(prepareGo)),
			//cleanup Go
			routine.Cleanup(routine.ExecutorFunc(cleanupGo)),
			routine.Go(routine.ExecutorFunc(ChildGo)),//child Go
			routine.Go(routine.ExecutorFunc(ChildGo)),
			routine.Go(routine.ExecutorFunc(ChildGo)),
			//signals
			routine.Signal(syscall.SIGINT, routine.SigHandler(func() {
				os.Exit(1)
			})),
		),
	)
}

Routine

create and control your own routine by routine.New.


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

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

Executors

The package provides many useful executor adapters for you:

  • guarantee
  • timeout & deadline
  • retry & repeat
  • concurrent
  • crontab
  • parallel & sequence
  • command
  • profiling

with these executor adapters, you can building the most complex goroutine logic.


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

//timeout
timeout := routine.Timeout(time.Minute, exec)

//retry
retry := routine.Retry(3, exec)

//repeat
repeat := routine.Repeat(10, time.Second, exec)

//concurrent
concurrent := routine.Concurrent(4, exec)

//schedule executor
crontab := routine.Crontab("* * * * *", exec)

//command
command := routine.Command("echo", routine.ARG("hello routine!"))

//parallel
parallel := routine.Parallel(exec1, exec2, exec3, ...)

//sequence
sequece := routine.Append(exec1, exec2, exec3, ...)

Enjoy

More details, please check the example and trace it.

$: go run example/main.go

# trace go routine & tasks
$: go tool trace  trace.out

Then you can check the tasks like this:

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")
)

Functions

func ArgumentsFrom added in v1.1.0

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

ArgumentsFrom extract from context

func Child added in v1.4.0

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

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

type Opt func(*options)

Opt interface

func Arguments

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

Arguments Opt for Main

func CancelSignals added in v1.3.0

func CancelSignals(sigs ...os.Signal) Opt

CancelSignals Opt for Main

func Cleanup

func Cleanup(exec Executor) Opt

Cleanup Opt for Main

func Go

func Go(exec Executor) Opt

Execute Opt for Main

func Prepare

func Prepare(exec Executor) Opt

Prepare Opt for Main

func Signal added in v1.3.0

func Signal(sig os.Signal, handler SigHandler) Opt

Signal Opt for Main

func Trace

func Trace(wr io.Writer) Opt

Trace support

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 ProfilingExecutor added in v1.2.10

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

Profiling

func Profiling added in v1.2.10

func Profiling(addr string) *ProfilingExecutor

func (*ProfilingExecutor) Execute added in v1.2.10

func (d *ProfilingExecutor) Execute(ctx context.Context) error

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
}

Routine Definition

func New added in v1.2.0

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

New a routine instance

func ParentFrom added in v1.2.0

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

func (*Routine) Close added in v1.3.2

func (r *Routine) Close() <-chan struct{}

Stop trigger the routine to stop

func (*Routine) Execute added in v1.2.0

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

Execute running the routine

func (*Routine) Serving added in v1.3.2

func (r *Routine) Serving() <-chan struct{}

type SigHandler added in v1.3.0

type SigHandler func()

type SigTrap added in v1.3.0

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

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