Module: Piper::Actor

Defined in:
lib/piper/actor.rb

Overview

Piper::Actor module provides a shell for a spawned Piper Push Cache task actor. Piper Push Cache supports process flows where the individual tasks in a flow can be implemented as a separate application that is spawned when it is needed. The spawned application is expected to continue to run and accept input on stdin. Output is over stdout. Both the input and output are JSON strings. This module takes care of the parsing and serialization.

The JSON format for input is a JSON object with an ‘id’ field and a ‘rec’ field. The output expected is an ‘id’ field, ‘rec’ field, and a ‘link’ field that identifies the link to follow.

Class Method Summary collapse

Class Method Details

.processObject

Starts a JSON processing loop that reads from $stdin. The proc provided with the call should call ship() to continue on with the processing.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/piper/actor.rb', line 20

def self.process()
  Oj::load($stdin, :mode => :strict) { |json|
    begin
      id = json['id']
      rec = json['rec']
      yield(id, rec)
    rescue Exception => e
      ship(id, { 'kind' => 'Error', 'message' => e.message, 'class' => e.class.to_s }, 'error')
    end
  }
end

.ship(id, rec, link) ⇒ Object

Ships or sends a response to the calling task in the Piper Flow.



33
34
35
36
# File 'lib/piper/actor.rb', line 33

def self.ship(id, rec, link)
  $stdout.puts Oj::dump({ 'id' => id, 'rec' => rec, 'link' => link }, :mode => :strict )
  $stdout.flush()
end