ravenworker

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2019 License: Apache-2.0 Imports: 21 Imported by: 0

README

Raven-worker package

Description

Raven-worker is used as the base of every worker in the Raven framework.
A worker is a building block that can perform a specific task in a streaming data-flow.
A data-stream that is configured from one or multiple workers, is called a flow.
The goal of a flow is to filter, extract, enrich and store streaming data to easily manage all your (streaming) data.

Worker types

Workers come in three different types: extract, transform and load workers. These types are based on the graph theory where each worker represents a node.
Consider any Raven flow a directed acyclic graph.

About Workers

  • Workers need to be as simple as possible, only do their job. Everything else will be handled by Raven.
  • If an error occurs, just panic. Raven will handle monitoring and restarting workers. The Raven Worker base package will use a sequential backoff algorithm for handling incidental errors.
  • If there isn't enough work, for a longer period, just stop. Raven will monitor backlogs and start new workers when necessary.

How to use

The following functions are exposed by the package:

New

This initializes the worker.

For the Raven framework, a worker needs three variables to work with:

  • ravenworker.WithRavenURL to connect to the Raven framework. Multiple values can be used here, as it will use them in a round robin configuration.
  • ravenworker.WithWorkerID to identify itself and get new jobs.
  • ravenworker.WithFlowID to get the right jobs for the flow it belongs to and therefore receive the right events (messages) to process.

The ravenworker.DefaultEnvironment() method can be used for convenience, to load the configuration from environment variables.

Example:

	c, err := ravenworker.New(
        ravenworker.DefaultEnvironment(),
    )

    if err != nil {
        // handle error
    }

The ravenworkworker.CustomEnvironment can be used when environment variables are not available. CustomEnvironment takes three string arguments to set the raven workflow url, flow ID and worker ID.

Example:

	c, err := ravenworker.New(
        ravenworker.Customenvironment("localhost:8023", "568e8bee-aca8-40c2-bfed-504ce103d4b6", "b557f6b3-b436-4635-9ae0-010fed184168"),
    )

    if err != nil {
        // handle error
    }

Note: when specifying the raven workflow url, leave out any scheme (http/https) in the string. Raven-worker uses the capnproto protocol and the function will add the proper scheme for you.

Now you can use the methods:

Consume

When a worker is of type transform or load, use Consume to retrieve the message from the stream.

Example:

    ref, err := c.Consume(context.Background())
    if err != nil {
        // handle error
    }
Get

Get will retrieve the actual message.

Example:

    msg, err := c.Get(ref)
    if err != nil {
        // handle error
    }
Ack

Ack will acknowledge the message and proceeds the flow.

Example:

    msg, err := c.Ack(ref, WithMessage(msg), WithFilter())
    if err != nil {
        // handle error
    }
Produce

When a worker is of type transform or load, use Produce to put the new message or ack the message.
The actual content (payload) is stored in message.Content which takes a byte array. Convenience functions like JsonContent (which encodes the object to json byte array) exists.

Example:

    message := NewMessage()
    message.Content = JsonContent(obj)

    if err := c.Produce(message); err != nil {
        // handle error
    }

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLogger = NewDefaultLogger(os.Getenv("RAVEN_LOG"), os.Getenv("WORKER_ID"))
View Source
var EmptyMessage = Message{}

Functions

func IsNotFoundErr added in v0.2.2

func IsNotFoundErr(err error) bool

func NewDefaultLogger added in v0.2.3

func NewDefaultLogger(endpoint, id string) *defaultLogger

NewDefaultLogger creates a JSON logger which outputs to an http endpoint. provide an empty string as endpoint to log to stdout.

func NewLogUploader added in v0.2.3

func NewLogUploader(ctx context.Context, endpoint string) (*logUploader, error)

Types

type AckOptionFunc added in v0.2.2

type AckOptionFunc func(r *ackRequest) error

func WithFilter added in v0.2.2

func WithFilter() AckOptionFunc

WithFilter will keep the flow from processing further.

func WithMessage added in v0.2.2

func WithMessage(message Message) AckOptionFunc

WithFilter will keep the flow from processing further.

type BackOffFunc added in v0.2.2

type BackOffFunc func() backoff.BackOff

type Config

type Config struct {
	WorkerID uuid.UUID

	FlowID uuid.UUID
	// contains filtered or unexported fields
}

type Content added in v0.2.2

type Content []byte

func JsonContent added in v0.2.2

func JsonContent(v interface{}) Content

func StringContent added in v0.2.2

func StringContent(s string) Content

type DefaultWorker added in v0.2.2

type DefaultWorker struct {
	Config
	// contains filtered or unexported fields
}

func (*DefaultWorker) Ack added in v0.2.2

func (c *DefaultWorker) Ack(ref Reference, options ...AckOptionFunc) error

Ack will acknowledge the message, only consumed messages are allowed.

WithFilter() will filter further processing

func (*DefaultWorker) Close added in v0.2.3

func (w *DefaultWorker) Close() error

func (*DefaultWorker) Consume added in v0.2.2

func (c *DefaultWorker) Consume(ctx context.Context) (Reference, error)

Consume retrieves a message from workflow. When there are no messages available, it will retry with a constant interval.

Messages can be retrieved by using Get(message)

Messages can be acknowledged by using Ack(ref)

ref, err := w.Consume()
if err !=nil {
    panic(err)
}

message, err := w.Get(ref)
if err !=nil {
    panic(err)
}

err := w.Ack(*message)
if err !=nil {
    panic(err)
}

Use this function for the 'transform' and 'load' worker types. blocks until it receives a message or 'consumeTimeout' expires.

func (*DefaultWorker) Get added in v0.2.2

func (c *DefaultWorker) Get(ref Reference) (Message, error)

Get will retrieve the event for reference.

msg,  err := Get(ref)
if err != nil {
    // handle error
}

func (*DefaultWorker) Produce added in v0.2.2

func (c *DefaultWorker) Produce(message Message) error

Produce will store a new message in the queue. If an error occurs it will retry using exponential backoff strategy.

 message := NewMessage()
     .Content([]byte("test"))

err := w.Produce(message)
if err != nil {
    panic (err)
}

type EventID added in v0.2.2

type EventID string

func (EventID) String added in v0.2.2

func (e EventID) String() string

type LogCloser added in v0.2.3

type LogCloser interface {
	Close() error
}

type Logger added in v0.2.2

type Logger interface {
	Debugf(msg string, args ...interface{})
	Infof(msg string, args ...interface{})
	Errorf(msg string, args ...interface{})
	Fatalf(msg string, args ...interface{})
}

type Message

type Message struct {
	MetaData []Metadata

	Content Content
}

func NewMessage added in v0.2.2

func NewMessage() Message

NewMessage will return a new empty Message struct

message := NewMessage()

func (Message) MarshalJSON added in v0.2.2

func (r Message) MarshalJSON() ([]byte, error)

func (*Message) UnmarshalJSON added in v0.2.2

func (r *Message) UnmarshalJSON(data []byte) error

type Metadata added in v0.2.2

type Metadata struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type NullLogCloser added in v0.2.3

type NullLogCloser struct {
}

func (NullLogCloser) Close added in v0.2.3

func (NullLogCloser) Close() error

type OptionFunc added in v0.2.2

type OptionFunc func(*Config) error

func CustomEnvironment added in v0.2.2

func CustomEnvironment(ravenURL, flowID, workerID string) OptionFunc

CustomEnvironment returns the optionFunc and takes 'ravenURL', 'flowID' and 'workerID' as string arguments.

func DefaultEnvironment added in v0.2.2

func DefaultEnvironment() OptionFunc

DefaultEnvironment returns the optionFunc that expects 'RAVEN_URL', 'FLOW_ID' and 'WORKER_ID' as environmental variables 'CONSUME_TIMEOUT' will override the default if set. DefaultLogger is set as the logger.

func WithBackOff added in v0.2.2

func WithBackOff(fn BackOffFunc) OptionFunc

func WithCloser added in v0.2.3

func WithCloser(closer io.Closer) OptionFunc

WithCloser adds an 'io.Closer' to the list.

func WithConsumeTimeout added in v0.2.3

func WithConsumeTimeout(s string) OptionFunc

WithConsumeTimeout time frame to wait for a new message. not setting this equals wait forever.

func WithFlowID added in v0.2.2

func WithFlowID(s string) (OptionFunc, error)

func WithLogger added in v0.2.2

func WithLogger(l Logger) (OptionFunc, error)

func WithMaxIntake added in v0.2.3

func WithMaxIntake(num string) OptionFunc

WithMaxIntake ingest messages until maxIntake is reached.

func WithRavenURL added in v0.2.2

func WithRavenURL(urlStr string) (OptionFunc, error)

func WithWorkerID added in v0.2.2

func WithWorkerID(s string) (OptionFunc, error)

type Reference

type Reference struct {
	AckID   string `json:"ack_id"`
	EventID string `json:"event_id"`
}

type Worker added in v0.2.2

type Worker interface {
	Consume(ctx context.Context) (Reference, error)
	Get(Reference) (Message, error)
	Ack(Reference, ...AckOptionFunc) error
	Produce(Message) error
	Close() error
}

func New added in v0.2.2

func New(opts ...OptionFunc) (Worker, error)

New returns a new configured Raven Worker client

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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