package executor

import "github.com/amonks/run/internal/executor"

Package executor encapsulates running a cancelable function, canceling it, and waiting for it to finish.

Index

Types

type Executor

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

Executor manages the lifecycle of a single cancelable function execution. It supports executing a function, canceling it synchronously, and waiting for it to complete. Each Executor has a unique token for identity comparison, enabling stale-goroutine detection.

func New

func New() *Executor

New creates a new Executor. The Executor is inert until Execute is called.

func (*Executor) Cancel

func (e *Executor) Cancel() error

Cancel cancels the executor's context and blocks until the function exits. It returns the function's error. Cancel is safe to call multiple times; subsequent calls block until the function exits and return the same error.

func (*Executor) Done

func (e *Executor) Done() <-chan struct{}

Done returns a channel that is closed when the executed function exits. Multiple goroutines can select on this channel simultaneously.

func (*Executor) Err

func (e *Executor) Err() error

Err returns the error from the executed function. It is only valid to call Err after Done is closed.

func (*Executor) Execute

func (e *Executor) Execute(ctx context.Context, fn func(context.Context) error)

Execute runs fn in a goroutine with a cancelable context derived from ctx. When fn returns, its error is stored and the Done channel is closed. Execute must be called exactly once per Executor.

func (*Executor) Is

func (e *Executor) Is(other *Executor) bool

Is returns true if other is the same Executor (by token comparison). This is used for stale-goroutine detection: when a task exit message arrives, the caller can check whether the executor that exited is still the current one for that task.