task

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 13 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrProgramExiting = errors.New("program exiting")

Functions

func OnProgramExit

func OnProgramExit(about string, fn func())

func RootContext

func RootContext() context.Context

func RootContextCanceled

func RootContextCanceled() <-chan struct{}

func WaitExit

func WaitExit(shutdownTimeout int)

WaitExit waits for a signal to shutdown the program, and then waits for all tasks to finish, up to the given timeout.

If the timeout is exceeded, it prints a list of all tasks that were still running when the timeout was reached, and their current tree of subtasks.

Types

type Callback

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

type Dependencies added in v0.7.0

type Dependencies[T comparable] struct {
	// contains filtered or unexported fields
}

func NewDependencies added in v0.7.0

func NewDependencies[T comparable]() *Dependencies[T]

func (*Dependencies[T]) Add added in v0.7.0

func (w *Dependencies[T]) Add(ele T)

func (*Dependencies[T]) Delete added in v0.7.0

func (w *Dependencies[T]) Delete(ele T)

func (*Dependencies[T]) Range added in v0.7.0

func (w *Dependencies[T]) Range(yield func(T) bool)

func (*Dependencies[T]) Wait added in v0.7.0

func (w *Dependencies[T]) Wait(ctx context.Context) error

type Parent

type Parent interface {
	Context() context.Context
	// Subtask returns a new subtask with the given name, derived from the parent's context.
	//
	// This should not be called after Finish is called on the task or its parent task.
	Subtask(name string, needFinish bool) *Task
	Name() string
	Finish(reason any)
	OnCancel(name string, f func())
}

type Task

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

Task controls objects' lifetime.

Objects that uses a Task should implement the TaskStarter and the TaskFinisher interface.

Use Task.Finish to stop all subtasks of the Task.

func RootTask

func RootTask(name string, needFinish bool) *Task

RootTask returns a new Task with the given name, derived from the root context.

func (*Task) Context

func (t *Task) Context() context.Context

func (*Task) Finish

func (t *Task) Finish(reason any)

Finish marks the task as finished, with the given reason (if any).

func (*Task) FinishAndWait

func (t *Task) FinishAndWait(reason any)

FinishAndWait cancel all subtasks and wait for them to finish, then marks the task as finished, with the given reason (if any).

func (*Task) FinishCause

func (t *Task) FinishCause() error

FinishCause returns the reason / error that caused the task to be finished.

func (*Task) MarshalText

func (t *Task) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Task) Name

func (t *Task) Name() string

func (*Task) OnCancel

func (t *Task) OnCancel(about string, fn func())

OnCancel calls fn when the task is canceled.

It should not be called after Finish is called.

func (*Task) OnFinished

func (t *Task) OnFinished(about string, fn func())

OnFinished calls fn when the task is canceled and all subtasks are finished.

It should not be called after Finish is called.

func (*Task) String

func (t *Task) String() string

String returns the full name of the task.

func (*Task) Subtask

func (t *Task) Subtask(name string, needFinish bool) *Task

Subtask returns a new subtask with the given name, derived from the parent's context.

This should not be called after Finish is called on the task or its parent task.

type TaskFinisher

type TaskFinisher interface {
	Finish(reason any)
}

type TaskStarter

type TaskStarter interface {
	// Start starts the object that implements TaskStarter,
	// and returns an error if it fails to start.
	//
	// callerSubtask.Finish must be called when start fails or the object is finished.
	Start(parent Parent) gperr.Error
	Task() *Task
}

Jump to

Keyboard shortcuts

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