lib/runtime.rb in factor-0.5.05 vs lib/runtime.rb in factor-0.5.06

- old
+ new

@@ -4,10 +4,11 @@ require 'securerandom' require 'yaml' require 'eventmachine' require 'uri' require 'faye/websocket' +require 'ostruct' require 'listener' require 'commands/base' module Factor @@ -39,28 +40,21 @@ end end def load(workflow_definition) EM.run do - self.instance_eval(workflow_definition) + instance_eval(workflow_definition) end end - def workflow(key, value) - @workflow_spec[key] = value - @name = value if @key == 'name' - @id = value if @key == 'id' - @description = value if @key == 'description' - end - - def listen(service_id, listener_id, params = {}, &block) - + def listen(service_ref, params = {}, &block) + service_id, listener_id = service_ref.split('::') ws = @connectors[service_id.to_sym].listener(listener_id) handle_on_open(service_id, listener_id, 'Listener', ws, params) - ws.on :close do |event| + ws.on :close do error 'Listener disconnected' if @reconnect warn 'Reconnecting...' sleep 3 ws.open @@ -71,11 +65,11 @@ listener_response = JSON.parse(event.data) case listener_response['type'] when'start_workflow' success "Workflow '#{service_id}::#{listener_id}' triggered" error_handle_call(listener_response, &block) - when 'started' + when 'return' success "Workflow '#{service_id}::#{listener_id}' started" when 'fail' error "Workflow '#{service_id}::#{listener_id}' failed to start" when 'log' listener_response['message'] = " #{listener_response['message']}" @@ -103,16 +97,17 @@ ws.open @sockets << ws end - def run(service_id, action_id, params = {}, &block) + def run(service_ref, params = {}, &block) + service_id, action_id = service_ref.split('::') ws = @connectors[service_id.to_sym].action(action_id) handle_on_open(service_id, action_id, 'Action', ws, params) - ws.on :error do |event| + ws.on :error do error 'Connection dropped while calling action' end ws.on :message do |event| action_response = JSON.parse(event.data) @@ -139,19 +134,19 @@ end private def handle_on_open(service_id, action_id, dsl_type, ws, params) - ws.on :open do |event| + ws.on :open do params.merge!(@credentials[service_id.to_sym] || {}) success "#{dsl_type.capitalize} '#{service_id}::#{action_id}' called" ws.send(params.to_json) end end def error_handle_call(listener_response, &block) - block.call(listener_response['payload']) if block + block.call(OpenStruct.new(listener_response['payload'])) if block rescue => ex error "Error in workflow definition: #{ex.message}" ex.backtrace.each do |line| error " #{line}" end @@ -176,17 +171,7 @@ def log_message(message_info) message_info['instance_id'] = @instance_id message_info['workflow_id'] = @id @logger.call(message_info) if @logger end - - def define_method_in_class(class_ref, class_id, method_id, &block) - class_name = class_id.classify - method_name = method_id.underscore - class_ref.class.instance_eval do - define_method(method_name) do |params = {}, &passed_block| - block.call(class_name, method_name, params, &passed_block) - end - end - end end -end \ No newline at end of file +end