GithubHelp home page GithubHelp logo

process's Introduction

Process

Travis-CI - Travis-CI status

Join the chat at https://gitter.im/jgordijn/process A small framework to define long running (persistent)processes within Akka.

Getting process

If you're using SBT, add the following lines to your build file:

resolvers += "processFramework at bintray" at "https://dl.bintray.com/jgordijn/maven/"

// Latest working with akka 2.3.x
libraryDependencies += "processframework" %% "process" % "0.1.16"

// akka 2.4.x
libraryDependencies += "processframework" %% "process" % "0.1.17"

// Change in organization name since 0.1.23
libraryDependencies += "com.github.jgordijn" %% "process" % "0.1.23"

For Maven and other build tools, you can visit search.maven.org

Intro

If you tried to write a long running (persistent) process in Akka, you will find out that this will result in a lot of messages handling in a single actor. It is not trivial to see what the process flow is and if there are parallel parts. Primary reason is that different steps are implemented in a single class. This library tries to make life easier by doing 2 things:

  1. Create a separate class for each step in the process flow
  2. Create a process class that describes how the steps are linked together

Steps

A process consists of different steps that are performed one after the other or in parallel. With Process you define every step in its own class.

case class DemoState(demoed: Boolean)
class DemoStep(demoService: ActorRef)(implicit val context: ActorContext)
    extends ProcessStep[DemoState] {

  // Code to execute when the step should perform it's task
  def execute()(implicit stepActor: ActorRef): Execution = state => {
    demoService ! Command(state.demoed)
  }

  // This catches the responses from the async execute action.
  // It emits Process.Event to the process.
  def receiveCommand: CommandToEvent = {
    case ReplyFromDemoService =>
      Demoed
  }

  // The updateState function handles events and changes the state. It
  // should mark the step as done when the event is the last event for
  // this step.
  def updateState: UpdateFunction = {
    case Demoed => { state =>
      markDone()
      state.copy(demoed = true)
    }
  }
}

Process

The main goal of the process is to create a flow of steps. It is possible to chain different steps, so that they are performed in sequence. It is also possible to parallelize steps.

class DemoProcess(demoService: ActorRef) extends Process[DemoState] {
  // implicit ExecutionContext is needed
  import context.dispatcher
  var state = DemoState(false)
  val step1 = new DemoStep(demoService)
  // Subflows can be created
  val subflow1 = subStep1 ~> subStep2
  val subflow2 = subStepX ~> subStepY

  // process defines the complete process
  val process = step1 ~> step2 ~> Par(subflow1, subflow2) ~> step3

  def receiveCommand = {
    case CommandToStartProcess =>
      process.run()
  }
}

PersistentProcess

Long running processes should survive restarts. Therefore it should persist the events and automatically restart when the process is created. It is almost as easy as changing Process[S] to PersistentProcess[S] (where S is the type of the state). The only difference is that you need to specify a persistenceId.

class DemoProcess(demoService: ActorRef) extends PersistentProcess[DemoState] {
  val persistenceId = "demoProcess"
  // implicit ExecutionContext is needed
  import context.dispatcher
  var state = DemoState(false)
  val step1 = new DemoStep(demoService)
  // Subflows can be created
  val subflow1 = subStep1 ~> subStep2
  val subflow2 = subStepX ~> subStepY

  // process defines the complete process
  val process = step1 ~> step2 ~> Par(subflow1, subflow2) ~> step3

  def receiveCommand = {
    case CommandToStartProcess =>
      process.run()
  }
}

process's People

Contributors

jgordijn avatar gvandeglind avatar rwiskerke avatar jaapterwoerds avatar gitter-badger avatar

Stargazers

Willie Ferguson avatar Marko Milenković avatar Ismail Chaida avatar Stanislav Eprikov avatar Stuart Saltzman avatar Yan avatar Yuta Okamoto avatar Eron Wright avatar Sean Glover avatar Dan Di Spaltro avatar Eric Loots avatar Mathias Bogaert avatar Andrey Mikheev avatar Andrej Golovnin avatar lteqgt avatar Lutz Hühnken avatar Patrick Lim avatar Jisoo Park avatar Jonathan Billaud avatar  avatar Ivan Doroshenko avatar Choucri FAHED avatar Lukasz Stefaniak avatar David Roon avatar Julian Tescher avatar 5hun Yanaura avatar Phil Andrew avatar Ivano Pagano avatar Yennick Trevels avatar

Watchers

 avatar Amadou Cisse avatar Alex Kogan avatar Roman Kellner avatar  avatar Stuart Saltzman avatar  avatar

process's Issues

Functional extension: model the process visually

Just a rough idea...maybe this deserves it's own project repo. Nevertheless, the idea is that once we can visualise the process..we could probably also turn it around and model the process visually and then have a process underneath that does the actual work.

Functional extension: runtime visualisation of a running process

this is also still a rough idea...and it would probably deserve it's own project repo...the idea is to be able to see (visually) what the state is of a particular running process instance, so which of it's process steps have completed and which are running, etc.

Tricky api in (Persistent)Proces

Both PersistenProcess and Process traits have the abstract

 def process: ProcessStep[State]

This has the distinct disadvantage that when implementing this trait you have the option to define a function which returns a different object each time, which is not what you would expect.

Let just change it to:

 val process: ProcessStep[State]

@jgordijn what do you think?

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.