runner

package
v1.16.102 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package runner provides interruptable goroutines.

task := runner.Go(func(shouldStop runner.S) error {
  // NEVER returns nil in this function
  // do setup
  // defer func(){
  //   // do teardown
  // }
  zeroErr := errors.New("no error")
  for {
    // do some work
    var err error
    if err != nil {
      return err
    }
    if shouldStop() {
      break
    }
  }
  return zeroErr // any errors?
})

// meanwhile...
// stop the task
task.Stop()

// wait for it to stop (or time out)
select {
  case <-task.StopChan():
    // stopped
  case <-time.After(1 * time.Second):
    log.Fatalln("task didn't stop quickly enough")
}

// check errors
if task.Err() != nil {
  log.Fatalln("task failed:", task.Err())
}

Index

Constants

View Source
const (
	FALSE int32 = 0
	TRUE  int32 = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type S

type S func() bool

S is a function that will return true if the goroutine should stop executing.

type Task

type Task struct {
	ID string
	// contains filtered or unexported fields
}

Task represents an interruptable goroutine.

func Go

func Go(fn func(S) error) *Task

Go executes the function in a goroutine and returns a Task capable of stopping the execution.

func (*Task) Err

func (t *Task) Err() error

Err gets the error returned by the goroutine.

func (*Task) Running

func (t *Task) Running() bool

Running gets whether the goroutine is running or not.

func (*Task) Stop

func (t *Task) Stop()

Stop tells the goroutine to stop.

func (*Task) StopChan

func (t *Task) StopChan() <-chan struct{}

StopChan gets the stop channel for this task. Reading from this channel will block while the task is running, and will unblock once the task has stopped (because the channel gets closed).

Jump to

Keyboard shortcuts

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