Class: Factor::Runtime::Engine
- Inherits:
-
Object
- Object
- Factor::Runtime::Engine
- Defined in:
- lib/runtime/engine.rb
Instance Attribute Summary collapse
-
#channel_modules ⇒ Object
Returns the value of attribute channel_modules.
-
#message_bus ⇒ Object
Returns the value of attribute message_bus.
-
#workflows ⇒ Object
Returns the value of attribute workflows.
Instance Method Summary collapse
- #call_channel_method(channel_name, class_name, params) ⇒ Object
-
#initialize(username, token) ⇒ Engine
constructor
Engine needs modules that contain the code, workflows to run, and message bus for communication.
- #launch(workflow, params) ⇒ Object
-
#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.
- #load_credentials(credentials) ⇒ Object
-
#load_workflow(workflow) ⇒ Object
adds the workflow to the workflows list the object must be a Workflow type.
- #logs(routing_key = "#", &code) ⇒ Object
-
#start ⇒ Object
start your engines.
- #tag(key, value) ⇒ Object
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_modules ⇒ Object
Returns the value of attribute channel_modules.
10 11 12 |
# File 'lib/runtime/engine.rb', line 10 def channel_modules @channel_modules end |
#message_bus ⇒ Object
Returns the value of attribute message_bus.
10 11 12 |
# File 'lib/runtime/engine.rb', line 10 def @message_bus end |
#workflows ⇒ Object
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.new .position << "start" .workflow=workflow .add_values params .workflow_instance_id= instance_id @message_bus.send_and_close 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.(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 || code.call end end end |
#start ⇒ Object
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 || if @workflows.include? .workflow workflow = @workflows[.workflow] activity = workflow.get_activity(.position) if !activity.nil? action = activity["action"] channel = activity["channel"] method = activity["method"] target = activity["target"] if match(target) values = .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) = .respond(event.params,event.class.name.split("::").last) @message_bus.send 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 |