Class: Factor::Runtime::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/runtime/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEngine

Engine needs modules that contain the code, workflows to run, and message bus for communication



12
13
14
15
16
17
# File 'lib/runtime/engine.rb', line 12

def initialize
  @channel_modules=Hash.new
  @workflows = Hash.new
  @message_bus ||= MessageBus.new
  @credentials = Hash.new
end

Instance Attribute Details

#channel_modulesObject

Returns the value of attribute channel_modules.



9
10
11
# File 'lib/runtime/engine.rb', line 9

def channel_modules
  @channel_modules
end

#message_busObject

Returns the value of attribute message_bus.



9
10
11
# File 'lib/runtime/engine.rb', line 9

def message_bus
  @message_bus
end

#workflowsObject

Returns the value of attribute workflows.



9
10
11
# File 'lib/runtime/engine.rb', line 9

def workflows
  @workflows
end

Instance Method Details

#call_channel_method(channel_name, class_name, params) ⇒ Object



100
101
102
103
104
# File 'lib/runtime/engine.rb', line 100

def call_channel_method(channel_name,class_name,params)
  channel_module = @channel_modules[channel_name]
  command = channel_module.const_get(class_name)
  command.new.do_work(params)
end

#launch(workflow, params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/runtime/engine.rb', line 50

def launch workflow, params
  instance_id=SecureRandom.hex
  @message_bus.start do
    message = Message.new
    message.position << "start"
    message.workflow=workflow
    message.add_values params
    message.workflow_instance_id= instance_id
    @message_bus.send_and_close message
  end
  instance_id
end

#load_channel(filename) ⇒ Object

load the channel by referencing the .rb file the filename is lowercase with “_” for spaces and the module inside must be camal cased to match once loaded it is in the channel_modules Hash



23
24
25
26
27
28
29
30
# File 'lib/runtime/engine.rb', line 23

def load_channel filename
  file=File.new filename
  require file
  channel_module_name = File.basename(file).gsub('.rb','').split('_').map{|ea| ea.capitalize}.join('')
  channel_module= self.class.const_get(channel_module_name)

  @channel_modules[channel_module.definition["module"]]=channel_module  
end

#load_credentials(credentials) ⇒ Object



32
33
34
# File 'lib/runtime/engine.rb', line 32

def load_credentials credentials
  @credentials["credentials"] = credentials
end

#load_workflow(workflow) ⇒ Object

adds the workflow to the workflows list the object must be a Workflow type



38
39
40
# File 'lib/runtime/engine.rb', line 38

def load_workflow workflow
  @workflows[workflow.name] = workflow
end

#logs(routing_key = "#", &code) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/runtime/engine.rb', line 42

def logs routing_key="#", &code 
  @message_bus.start do
    @message_bus.listen routing_key do |message|
      code.call message
    end
  end
end

#startObject

start your engines. vroom vrooooom!



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/runtime/engine.rb', line 64

def start
  @message_bus.start do
    @message_bus.listen do |message|
      if @workflows.include? message.workflow
        workflow = @workflows[message.workflow]
        activity = workflow.get_activity(message.position)
        if !activity.nil?
          # puts "[activity] #{activity.to_s}"
          action = activity["action"]
          channel = activity["channel"]
          method = activity["method"]
        
          values = message.body.merge(@credentials)
          puts "[values] #{values}"
          # this maps the input values passed in with the templated defined in the workflow
          params = Hash.new
          activity["params"].each do |key,template|
            params[key]=Mustache.render(template,values)
          end
          puts "[calling] #{channel}::#{method} (#{params.to_s})"
          event = call_channel_method(channel,method,params)
        
          response_message = message.respond(event.params,event.class.name.split("::").last)
        
        
          @message_bus.send response_message
        
        end
      else
        # workflow doesn't exist
      end
    end
  end
end