routine

package module
v0.0.0-...-7060071 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 15 Imported by: 0

README

routine

GoDoc Go Report Card Build Status Version Coverage Status

Routine Architecture

Quick Start

package main

import (
	"log"
	"context"

	"github.com/quirkystrud/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/quirkystrud/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/quirkystrud/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/quirkystrud/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

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

ArgumentsFrom extract from context

func Child

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

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

Types

type AppendExecutor

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

AppendExecutor

func Append

func Append(execs ...Executor) *AppendExecutor

Append new

func (*AppendExecutor) Append

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

func (*AppendExecutor) Execute

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

type CommandOpt func(*CommandExecutor)

func ARG

func ARG(arg string) CommandOpt

func ENV

func ENV(env string) CommandOpt

func Stderr

func Stderr(wr io.Writer) CommandOpt

func Stdin

func Stdin(rd io.Reader) CommandOpt

func Stdout

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

func (c *CrontabExecutor) Everyday(flag bool)

func (*CrontabExecutor) Execute

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

Execute implement Executor

func (*CrontabExecutor) IsTimeMuted

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

func (*CrontabExecutor) Mute

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

func (*CrontabExecutor) Weekend

func (c *CrontabExecutor) Weekend(flag bool)

func (*CrontabExecutor) Workday

func (c *CrontabExecutor) Workday(flag bool)

type CrontabMute

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

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

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

Signal Opt for Main

func Trace

func Trace(wr io.Writer) Opt

Trace support

type ParallelExecutor

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

ParallelExecutor

func Parallel

func Parallel(execs ...Executor) *ParallelExecutor

Parallel new

func (*ParallelExecutor) Execute

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

Execute implement Executor

type ProfilingExecutor

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

Profiling

func Profiling

func Profiling(addr string) *ProfilingExecutor

func (*ProfilingExecutor) Execute

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

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

New a routine instance

func ParentFrom

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

func (*Routine) Close

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

Stop trigger the routine to stop

func (*Routine) Execute

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

Execute running the routine

func (*Routine) Serving

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

type SigHandler

type SigHandler func()

type SigTrap

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