Giter VIP home page Giter VIP logo

machine's Introduction

Go GoDoc Codacy Badge Codacy Badge

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.


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.

machine's People

Contributors

jonathan-whitaker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.