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

#initialize(username, token) ⇒ Engine

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



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

def initialize(username,token)
  @channel_modules=Hash.new
  @workflows = Hash.new
  @message_bus = MessageBus.new(username,token)
  @credentials = Hash.new
  @tags = Hash.new
end

Instance Attribute Details

#channel_modulesObject

Returns the value of attribute channel_modules.



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

def channel_modules
  @channel_modules
end

#message_busObject

Returns the value of attribute message_bus.



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

def message_bus
  @message_bus
end

#workflowsObject

Returns the value of attribute workflows.



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

def workflows
  @workflows
end

Instance Method Details

#call_channel_method(channel_name, class_name, params) ⇒ Object



114
115
116
117
118
# File 'lib/runtime/engine.rb', line 114

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



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/runtime/engine.rb', line 55

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



29
30
31
32
33
34
35
# File 'lib/runtime/engine.rb', line 29

def load_channel filename
  filename = File.absolute_path(File.expand_path(filename.path)) if filename.is_a?(File) # just in case someone passes in a File not a String (i.e. me)
  require filename
  channel_module_name = File.basename(filename).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



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

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



43
44
45
# File 'lib/runtime/engine.rb', line 43

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

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



47
48
49
50
51
52
53
# File 'lib/runtime/engine.rb', line 47

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!



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/runtime/engine.rb', line 69

def start
  begin
    @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?
            action = activity["action"]
            channel = activity["channel"]
            method = activity["method"]
            target = activity["target"]
        
          
            if match(target)
        
              values = message.body.merge(@credentials)
              # this maps the input values passed in with the templated defined in the workflow

              puts "[activity-params] #{activity["params"]}"
              puts "[values] #{values}"
              params = render_template(activity["params"],values)
              puts "[params] #{params}"
              
              event = call_channel_method(channel,method,params)
        
              response_message = message.respond(event.params,event.class.name.split("::").last)
        
        
              @message_bus.send response_message
            end
        
          end
        else
          # workflow doesn't exist
        end
      end
    end
  rescue SystemExit, Interrupt
    "done"
  rescue Exception => ex
    ex
  end
end

#tag(key, value) ⇒ Object



21
22
23
# File 'lib/runtime/engine.rb', line 21

def tag(key,value)
  @tags[key]=value
end