plugins

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2020 License: Apache-2.0 Imports: 10 Imported by: 2

Documentation

Overview

Package plugins provides the functionality to subscribe, and publish message according to the plugin.proto. The information are passed in to the plugin via environment variables. The server discovers plugins in the provided plugins folder, and starts these in its context.

Index

Examples

Constants

View Source
const (
	// ErrUnknown ...
	ErrUnknown = iota
	// ErrParse ...
	ErrParse
)

Variables

View Source
var (
	// ErrPluginAuthentication ...
	ErrPluginAuthentication = errors.New("plugin: failed to authenticate")
)

Functions

This section is empty.

Types

type Context

type Context interface {
	// Message should return the message of the current context.
	Message() *pb.Message
	// Send should send a new message with in the current context.
	Send(*pb.Message) error
	// AsyncSend should asynchronously send a new message in the current context.
	AsyncSend(msg *pb.Message)
	// Context should return the current execution context.
	Context() context.Context
}

Context is providing information and operations upon the reply execution context.

type Event

type Event interface{}

Event symbolizes events that can occur in the plugin. Though they maybe triggered from somewhere else.

type MessageError

type MessageError struct {
	Code int
	Msg  string
}

MessageError represents an error that may occurs

func (MessageError) Error

func (e MessageError) Error() string

returns

type Opt

type Opt func(*Opts)

Opt is an Option

type Opts

type Opts struct{}

Opts are the available options

type Plugin

type Plugin struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Plugin describes a plugin in general. It should not be instantiated directly.

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

env := runtime.Env()
plugin, _ := WithContext(ctx, env)

// here you can interact with the plugin data

if err := plugin.Wait(); err != nil {
	panic(err)
}
Output:

func WithContext

func WithContext(ctx context.Context, env *runtime.Environment, opts ...Opt) (*Plugin, context.Context)

WithContext is creating a new plugin and a context to run operations in routines. When the context is canceled, all concurrent operations are canceled.

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

env := runtime.Env()
plugin, _ := WithContext(ctx, env)

if err := plugin.Wait(); err != nil {
	panic(err)
}
Output:

func (*Plugin) AsyncReplyWithFunc

func (p *Plugin) AsyncReplyWithFunc(fn SubscribeFunc, funcs ...filters.FilterFunc) error

AsyncReplyWithFunc is a wrapper function to provide a message handler function which will asynchronously reply to the messages received by a plugin. The difference between this and the synchronous ReplyWithFunc wrapper is that while both receive messages in sequence, ReplyWithFunc will block on reading the subsequent message from the inbox while the current message is being handled.

func (*Plugin) Log

func (p *Plugin) Log() *log.Entry

Log is returning the logger to log to the log formatter.

func (*Plugin) PublishInbox

func (p *Plugin) PublishInbox(msg *pb.Message) error

PublishInbox is publishing message to the inbox in the controller.

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

env := runtime.Env()
plugin, _ := WithContext(ctx, env)

msg := &pb.Message{
	Text: "foo != bar",
}

if err := plugin.PublishInbox(msg); err != nil {
	panic(err)
}
Output:

func (*Plugin) PublishOutbox

func (p *Plugin) PublishOutbox(msg *pb.Message) error

PublishOutbox is publishing message to the outbox in the controller.

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

env := runtime.Env()
plugin, _ := WithContext(ctx, env)

msg := &pb.Message{
	Text: "foo != bar",
}

if err := plugin.PublishOutbox(msg); err != nil {
	panic(err)
}
Output:

func (*Plugin) ReplyWithFunc

func (p *Plugin) ReplyWithFunc(fn SubscribeFunc, funcs ...filters.FilterFunc) error

ReplyWithFunc is a wrapper function to provide a function which may send replies to received message for this plugin.

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

env := runtime.Env()
plugin, _ := WithContext(ctx, env)

err := plugin.ReplyWithFunc(func(ctx Context) error {
	log.Printf("received message: %v", ctx.Message())

	return nil
})
if err != nil {
	log.Fatal(err)
}

if err := plugin.Wait(); err != nil {
	panic(err)
}
Output:

func (*Plugin) SubscribeInbox

func (p *Plugin) SubscribeInbox(funcs ...filters.FilterFunc) <-chan Event

SubscribeInbox is subscribing to the inbox of messages. This if for plugins that want to consume the message that other plugins publish to Autobot.

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

env := runtime.Env()
plugin, rctx := WithContext(ctx, env)

for {
	select {
	case e := <-plugin.SubscribeInbox():
		fmt.Printf("received a new message: %v", e)
	case <-rctx.Done():
		return
	}
}
Output:

func (*Plugin) SubscribeOutbox

func (p *Plugin) SubscribeOutbox(funcs ...filters.FilterFunc) <-chan Event

SubscribeOutbox is subscribing to the outbox of messages. These are the message that ought to be published to an external service (e.g. Slack, MS Teams).

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

env := runtime.Env()
plugin, rctx := WithContext(ctx, env)

for {
	select {
	case e := <-plugin.SubscribeOutbox():
		fmt.Printf("received message to be send out: %v", e)
	case <-rctx.Done():
		return
	}
}
Output:

func (*Plugin) Wait

func (p *Plugin) Wait() error

Wait is waiting for the underlying WaitGroup. All run go routines are hold here to before one exists with an error. Then the provided context is canceled.

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

env := runtime.Env()
plugin, _ := WithContext(ctx, env)

if err := plugin.Wait(); err != nil {
	panic(err)
}
Output:

type SubscribeFunc

type SubscribeFunc = func(Context) error

SubscribeFunc ...

Directories

Path Synopsis
Package filters provides functionality to filter messages.
Package filters provides functionality to filter messages.
Package runtime contains functionality for runtime information of a plugin.
Package runtime contains functionality for runtime information of a plugin.

Jump to

Keyboard shortcuts

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