machine

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2020 License: MIT Imports: 10 Imported by: 13

README

Go GoDoc Codacy Badge Codacy Badge Gitter chat

Machine is a library for creating data workflows. These workflows can be either very concise or quite complex, even allowing for cycles for flows that need retry or self healing mechanisms.

Installation

  go get -u github.com/whitaker-io/machine

Usage Overview

Usage

Basic receive -> process -> send Flow

  // fifo controls whether or not the data is processed in order of receipt
  fifo := false
  // bufferSize controls the go channel buffer size, set to 0 for unbuffered channels
  bufferSize := 0

  machineInstance := New(uuid.New().String(), "machine_name", fifo, func(c context.Context) chan []map[string]interface{} {
    return channel
  }).Then(
    NewVertex(uuid.New().String(), "unique_vertex_name", fifo, func(m map[string]interface{}) error {
      
      // ...do some processing
      
      return nil
    }).
    Terminate(
      NewTermination(uuid.New().String(), "unique_termination_name", !fifo, func(list []map[string]interface{}) error {

        // send the data somewhere
        
        return nil
      }),
    ),

  // Build take an int buffer size and some Recorder functions to allow for state management or logging.
  // Having a Recorder requires the use of a Deep Copy operation which can be expensive depending on
  // the data being processed
  ).Build(bufferSize, func(vertexID, operation string, payload []*Packet) {})

  if err := machineInstance.Run(context.Background()); err != nil {
    // Run will return an error in the case that one of the paths is not terminated
    panic(err)
  }

Machine can also duplicate the data and send it down multiple paths

  // fifo controls whether or not the data is processed in order of reciept
  fifo := false
  // bufferSize controls the go channel buffer size, set to 0 for unbuffered channels
  bufferSize := 0

  machineInstance := New(uuid.New().String(), "machine_name", fifo, func(c context.Context) chan []map[string]interface{} {
    return channel
  }).Route(
    NewRouter(uuid.New().String(), "unique_router_name", fifo, RouterDuplicate).
      TerminateLeft(
        NewTermination(uuid.New().String(), "unique_termination_name", !fifo, func(list []map[string]interface{}) error {

          // send a copy of the data somewhere

          return nil
        }),
      ).
      TerminateRight(
        NewTermination(uuid.New().String(), "unique_termination_name", !fifo, func(list []map[string]interface{}) error {

          // send a copy of the data somewhere else

          return nil
        }),
      ),

  // Build take an int buffer size and some Recorder functions to allow for state management or logging.
  // Having a Recorder requires the use of a Deep Copy operation which can be expensive depending on
  // the data being processed
  ).Build(bufferSize, func(vertexID, operation string, payload []*Packet) {})

  if err := machineInstance.Run(context.Background()); err != nil {
    // Run will return an error in the case that one of the paths is not terminated
    panic(err)
  }

Incase of errors you can also route the errors down their own path with more complex flows having retry loops

  // fifo controls whether or not the data is processed in order of reciept
  fifo := false
  // bufferSize controls the go channel buffer size, set to 0 for unbuffered channels
  bufferSize := 0

  machineInstance := New(uuid.New().String(), "machine_name", fifo, func(c context.Context) chan []map[string]interface{} {
    return channel
  }).Then(
    NewVertex(uuid.New().String(), "unique_vertex_name", fifo, func(m map[string]interface{}) error {
      var err error

      // ...do some processing

      return err
    }).Route(
      NewRouter(uuid.New().String(), "unique_router_name", fifo, RouterError).
        TerminateLeft(
          NewTermination(uuid.New().String(), "unique_termination_name", !fifo, func(list []map[string]interface{}) error {

            // send successful data somewhere

            return nil
          }),
        ).
        TerminateRight(
          NewTermination(uuid.New().String(), "unique_termination_name", !fifo, func(list []map[string]interface{}) error {

            // send erroneous data somewhere else

            return nil
          }),
        ),
    ),

  // Build take an int buffer size and some Recorder functions to allow for state management or logging.
  // Having a Recorder requires the use of a Deep Copy operation which can be expensive depending on
  // the data being processed
  ).Build(bufferSize, func(vertexID, operation string, payload []*Packet) {})

  if err := machineInstance.Run(context.Background()); err != nil {
    // Run will return an error in the case that one of the paths is not terminated
    panic(err)
  }

Routing based on filtering the data is also possible with RouterRule

  // fifo controls whether or not the data is processed in order of reciept
  fifo := false
  // bufferSize controls the go channel buffer size, set to 0 for unbuffered channels
  bufferSize := 0

  machineInstance := New(uuid.New().String(), "machine_name", fifo, func(c context.Context) chan []map[string]interface{} {
    return channel
  }).Then(
    NewVertex(uuid.New().String(), "unique_vertex_name", fifo, func(m map[string]interface{}) error {
      var err error

      // ...do some processing

      return err
    }).Route(
      NewRouter(uuid.New().String(), "unique_router_name", fifo, RouterRule(func(m map[string]interface{}) bool {
        return len(m) > 0
      }).Handler).
        TerminateLeft(
          NewTermination(uuid.New().String(), "unique_termination_name", !fifo, func(list []map[string]interface{}) error {

            // send correct data somewhere

            return nil
          }),
        ).
        TerminateRight(
          NewTermination(uuid.New().String(), "unique_termination_name", !fifo, func(list []map[string]interface{}) error {

            // send bad data somewhere else

            return nil
          }),
        ),
    ),

  // Build take an int buffer size and some Recorder functions to allow for state management or logging.
  // Having a Recorder requires the use of a Deep Copy operation which can be expensive depending on
  // the data being processed
  ).Build(bufferSize, func(vertexID, operation string, payload []*Packet) {})

  if err := machineInstance.Run(context.Background()); err != nil {
    // Run will return an error in the case that one of the paths is not terminated
    panic(err)
  }

License

Machine is provided under the MIT License.

The MIT License (MIT)

Copyright (c) 2020 Jonathan Whitaker

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Applicative added in v0.3.0

type Applicative func(typed.Typed) error

Applicative type for applying a change to a context

type Builder

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

Builder builder type for starting a machine

func New

func New(id string, r Retriever, options ...*Option) *Builder

New func for providing an instance of Builder

func (*Builder) ID added in v0.2.0

func (m *Builder) ID() string

ID func to return the ID for the machine

func (*Builder) Inject added in v0.2.0

func (m *Builder) Inject(ctx context.Context, events map[string][]*Packet)

Inject func for injecting events into the machine

func (*Builder) Route

func (m *Builder) Route(r *Splitter) *Builder

Route func for sending the payload to a router

func (*Builder) Run added in v0.1.5

func (m *Builder) Run(ctx context.Context, recorders ...func(string, string, string, []*Packet)) error

Run func for starting the machine

func (*Builder) Terminate

func (m *Builder) Terminate(t *Transmission) *Builder

Terminate func for sending the payload to a cap

func (*Builder) Then

func (m *Builder) Then(v *Vertex) *Builder

Then func for sending the payload to a processor

type Option added in v0.2.0

type Option struct {
	FIFO       *bool
	Idempotent *bool
	BufferSize *int
	Span       *bool
	Metrics    *bool
}

Option type for holding machine settings

type Packet

type Packet struct {
	ID    string
	Data  typed.Typed
	Error error
	// contains filtered or unexported fields
}

Packet type that holds information traveling through the machine

type Retriever added in v0.3.0

type Retriever func(context.Context) chan []typed.Typed

Retriever type for providing the data to flow into the system

type Sender added in v0.3.0

type Sender func([]typed.Typed) error

Sender type for ending a chain and returning an error if exists

type SplitHandler added in v0.3.0

type SplitHandler func(list []*Packet) (a, b []*Packet)

SplitHandler func for splitting a payload into 2

var (
	// SplitDuplicate is a RouteHandler that sends data to both outputs
	SplitDuplicate SplitHandler = func(payload []*Packet) (a, b []*Packet) {
		a = []*Packet{}
		b = []*Packet{}

		for _, packet := range payload {
			a = append(a, packet)
			b = append(b, packet)
		}

		return a, b
	}

	// SplitError is a RouteHandler for splitting errors from successes
	SplitError SplitHandler = func(payload []*Packet) (s, f []*Packet) {
		s = []*Packet{}
		f = []*Packet{}

		for _, packet := range payload {
			if packet.Error != nil {
				f = append(f, packet)
			} else {
				s = append(s, packet)
			}
		}

		return s, f
	}
)

type SplitRule added in v0.3.0

type SplitRule func(typed.Typed) bool

SplitRule type for validating a context at the beginning of a Machine

func (SplitRule) Handler added in v0.3.0

func (r SplitRule) Handler(payload []*Packet) (t, f []*Packet)

Handler func for providing a RouteHandler

type Splitter added in v0.3.0

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

Splitter builder type for adding a router to the machine

func NewSplitter added in v0.3.0

func NewSplitter(id string, s SplitHandler) *Splitter

NewSplitter func for providing an instance of RouterBuilder

func (*Splitter) RouteLeft added in v0.3.0

func (m *Splitter) RouteLeft(left *Splitter) *Splitter

RouteLeft func for sending the payload to a router

func (*Splitter) RouteRight added in v0.3.0

func (m *Splitter) RouteRight(right *Splitter) *Splitter

RouteRight func for sending the payload to a router

func (*Splitter) TerminateLeft added in v0.3.0

func (m *Splitter) TerminateLeft(t *Transmission) *Splitter

TerminateLeft func for sending the payload to a cap

func (*Splitter) TerminateRight added in v0.3.0

func (m *Splitter) TerminateRight(t *Transmission) *Splitter

TerminateRight func for sending the payload to a cap

func (*Splitter) ThenLeft added in v0.3.0

func (m *Splitter) ThenLeft(left *Vertex) *Splitter

ThenLeft func for sending the payload to a processor

func (*Splitter) ThenRight added in v0.3.0

func (m *Splitter) ThenRight(right *Vertex) *Splitter

ThenRight func for sending the payload to a processor

type Transmission added in v0.3.0

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

Transmission builder type for adding a termination to the machine

func NewTransmission added in v0.3.0

func NewTransmission(id string, s Sender) *Transmission

NewTransmission func for providing an instance of TerminationBuilder

type Vertex added in v0.3.0

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

Vertex builder type for adding a processor to the machine

func NewVertex

func NewVertex(id string, a Applicative) *Vertex

NewVertex func for providing an instance of VertexBuilder

func (*Vertex) Route added in v0.3.0

func (m *Vertex) Route(r *Splitter) *Vertex

Route func for sending the payload to a router

func (*Vertex) Terminate added in v0.3.0

func (m *Vertex) Terminate(t *Transmission) *Vertex

Terminate func for sending the payload to a cap

func (*Vertex) Then added in v0.3.0

func (m *Vertex) Then(v *Vertex) *Vertex

Then func for sending the payload to a processor

Directories

Path Synopsis
cmd module
common module
components
edge
http module
pubsub module
telemetry module

Jump to

Keyboard shortcuts

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