process

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

View Source
const ContextKeyManager = "gohacks/process@v1"

Key used to store the instance in the context's user value.

Variables

View Source
var ErrValueNotManager = errors.Base("value is not Manager")

Signalled if the instance associated with the context key is not of type Manager.

Functions

func SetManager added in v1.0.6

func SetManager(ctx context.Context, inst Manager) (context.Context, error)

Set Manager stores the instance in the context map.

func SetManagerIfAbsent added in v1.0.6

func SetManagerIfAbsent(ctx context.Context, inst Manager) (context.Context, error)

SetManagerIfAbsent sets only if not already present.

func WithManager added in v1.0.6

func WithManager(ctx context.Context, fn func(Manager))

WithManager calls fn with the instance or fallback.

Types

type CallbackFn

type CallbackFn func(*State)

Callback function.

type Config

type Config struct {
	Name      string                // Pretty name.
	Interval  types.Duration        // `RunEvery` time interval.
	Function  CallbackFn            // `Action` callback.
	OnStart   CallbackFn            // `Start` callback.
	OnStop    CallbackFn            // `Stop` callback.
	OnQuery   QueryFn               // `Query` callback.
	Responder responder.Respondable // Responder object.
}

Process configuration structure.

func NewConfig added in v1.0.5

func NewConfig() *Config

Create a default process configuration.

type Manager

type Manager interface {
	Logger() logger.Logger
	SetContext(context.Context)
	SetLogger(logger.Logger)
	Context() context.Context
	Create(*Config) *Process
	Add(*Process)
	Find(string) (*Process, bool)
	Run(string) bool
	Stop(string) bool
	StopAll() StopAllResults
	Processes() []*Process
	Count() int
}

Process manager structure.

To use,

1) Create a new process manager:

```go

procmgr := process.NewManager()

```

2) Create your process configuration:

```go

conf := &process.Config{
  Name:     "Windows 95",
  Interval: 10, // seconds
  Function: func(state *State) {
    // Crash or something.
  }
}

```

3) Create the process itself.

```go

proc := procmgr.Create(conf)

```

4) Run the process.

```go

procmgr.Run("Windows 95")

```

/or/

```go

proc.Run()

```

Manager is optional, as you can create processes directly.

func FromManager added in v1.0.6

func FromManager(ctx context.Context) Manager

FromManager returns the instance or the fallback.

func GetManager added in v1.0.6

func GetManager(ctx context.Context) (Manager, error)

Get the logger from the given context.

Will return ErrValueNotManager if the value in the context is not of type Manager.

func MustGetManager added in v1.0.6

func MustGetManager(ctx context.Context) Manager

Attempt to get the instance from the given context. Panics if the operation fails.

func NewManager

func NewManager() Manager

Create a new process manager.

func NewManagerWithContext

func NewManagerWithContext(parent context.Context) Manager

Create a new process manager with a given parent context.

func TryGetManager added in v1.0.6

func TryGetManager(ctx context.Context) (Manager, bool)

TryGetManager returns the instance and true if present and typed.

type Process

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

Process structure.

To use:

1) Create a config:

```go

conf := &process.Config{
  Name:     "Windows 95",
  Interval: 10,        // 10 seconds.
  Function: func(state *State) {
    // Crash or something.
  },
}

```

2) Create a process:

```go

proc := process.NewProcess(conf)

```

3) Run the process:

```go

go proc.Run()

```

4) Send data to the process:

```go

proc.Send("Blue Screen of Death")

```

5) Read data from the process:

```go

data := proc.Receive()

```

6) Stop the process

```go

proc.Stop()

```

will stop the process.

func NewProcess

func NewProcess(config *Config) *Process

Create a new process with the given configuration.

func NewProcessWithContext

func NewProcessWithContext(parent context.Context, config *Config) *Process

Create a new process with the given configuration and parent context.

func (*Process) Context added in v0.1.1

func (p *Process) Context() context.Context

Return the context for the process.

func (*Process) Invoke added in v1.0.5

func (p *Process) Invoke(event events.Event) events.Event

func (*Process) Name

func (p *Process) Name() string

func (*Process) Query added in v0.1.1

func (p *Process) Query(arg any) any

Query the running process.

This allows interaction with the process's base object without using `Action`.

func (*Process) RespondsTo added in v1.0.5

func (p *Process) RespondsTo(event events.Event) bool

func (*Process) Run

func (p *Process) Run() bool

Run the process with its action taking place on a continuous loop.

Returns 'true' if the process has been started, or 'false' if it is already running.

func (*Process) Running

func (p *Process) Running() bool

Is the process running?

func (*Process) Stop

func (p *Process) Stop() bool

Stop the process.

Returns 'true' if the process was successfully stopped, or 'false' if it was not running.

func (*Process) Type added in v1.0.5

func (p *Process) Type() string

type QueryFn added in v0.1.1

type QueryFn func(any) any

type State

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

Internal state for processes.

func (*State) Context added in v0.1.1

func (ps *State) Context() context.Context

Return the context for the parent process.

func (*State) Invoke added in v1.0.5

func (ps *State) Invoke(event events.Event) (events.Event, bool)

func (*State) Logger

func (ps *State) Logger() logger.Logger

func (*State) RespondsTo added in v1.0.5

func (ps *State) RespondsTo(event events.Event) bool

type StopAllResults added in v1.0.5

type StopAllResults map[string]bool

Jump to

Keyboard shortcuts

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