grunner

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

README

grunner - run/stop goroutines/tasks securely, recursively.

codecov GoDoc license

s1 := grunner.New()

s1.Defer(func() {
    fmt.Println("s1 stopped 2")
})
s1.Defer(func() {
    fmt.Println("s1 stopped 1")
})

// loop run task until s1 closed.
s1.Loop(func() {
    fmt.Println("s1 run loop task")
    time.Sleep(time.Millisecond*3)
})

// run interval task until s1 closed.
s1.Interval(func() {
    t.Log("s1 run interval task")
}, time.Millisecond)

go func() {
    ticker := time.NewTicker(time.Millisecond*2)
    
    for {
        select {
        case <-s1.C:
            return
        case <-ticker.C:
        	fmt.Println("run ticker task until s1 stopped")
        }
    }
}()

s2 := s1.NewChild()
s2.Defer(func() {
    fmt.Println("s2 stopped")
})

s3 := s2.NewChild()
s3.Defer(func() {
    fmt.Println("s3 stopped")
})

time.Sleep(time.Millisecond * 10)

s1.Stop()

time.Sleep(time.Millisecond * 10)

// s1 run loop task
// s1 run interval task
// s1 run interval task
// run ticker task until s1 stopped
// s1 run interval task
// s1 run loop task
// s1 run interval task
// run ticker task until s1 stopped
// s1 run interval task
// s1 run interval task
// run ticker task until s1 stopped
// s1 run loop task
// s1 run interval task
// s1 run interval task
// run ticker task until s1 stopped
// s1 run interval task
// s1 run loop task
// s1 run interval task
// s1 stopped 1
// s3 stopped
// s2 stopped
// s1 stopped 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runner

type Runner struct {
	// channel to control stop status, stop it by calling Stop().
	C chan struct{}
	// contains filtered or unexported fields
}

Runner the runner status struct.

func New

func New() *Runner

New create a new Runner.

func NewChild

func NewChild(stop chan struct{}) *Runner

NewChild create a new Runner as child of the exists chan, when which is closed the child will be stopped too.

func (*Runner) Defer

func (s *Runner) Defer(task Task)

Defer add task called in desc order when stopper is stopped.

func (*Runner) Interval

func (s *Runner) Interval(task Task, interval time.Duration)

Interval run task at intervals util the stopper is stopped.

func (*Runner) Loop

func (s *Runner) Loop(task Task)

Loop run task util the stopper is stopped. Note, there is not an interval between the executions of two tasks.

func (*Runner) NewChild

func (s *Runner) NewChild() *Runner

NewChild create a new Runner as child of the exists one, when which is stopped the child will be stopped too.

func (*Runner) NewParent

func (s *Runner) NewParent() *Runner

NewParent create a new Runner as parent of the exists one, which will be stopped when the new parent stopped.

func (*Runner) Stop

func (s *Runner) Stop()

Stop close the stopper.

func (*Runner) StopWith

func (s *Runner) StopWith(task Task)

StopWith stop the stopper and execute the task. the same as calling Defer(task) first, and then calling Stop().

type Task

type Task func()

Jump to

Keyboard shortcuts

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