Class: Factor::Runtime
- Inherits:
-
Object
- Object
- Factor::Runtime
- Defined in:
- lib/runtime.rb
Overview
Runtime class is the magic of the server
Instance Attribute Summary collapse
-
#connectors ⇒ Object
Returns the value of attribute connectors.
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
-
#instance_id ⇒ Object
Returns the value of attribute instance_id.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(connectors, credentials) ⇒ Runtime
constructor
A new instance of Runtime.
- #listen(service_id, listener_id, params = {}, &block) ⇒ Object
- #load(workflow_definition) ⇒ Object
- #run(service_id, action_id, params = {}, &block) ⇒ Object
- #workflow(key, value) ⇒ Object
Constructor Details
#initialize(connectors, credentials) ⇒ Runtime
Returns a new instance of Runtime.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/runtime.rb', line 18 def initialize(connectors, credentials) @workflow_spec = {} @sockets = [] @instance_id = SecureRandom.hex(3) @reconnect = true trap 'SIGINT' do info "Exiting '#{@instance_id}'" @reconnect = false @sockets.each { |s| s.close } exit end @connectors = {} connectors.each do |connector_id, connector_url| @connectors[connector_id] = Listener.new(connector_url) end @credentials = {} credentials.each do |connector_id, credential_settings| @credentials[connector_id] = credential_settings end end |
Instance Attribute Details
#connectors ⇒ Object
Returns the value of attribute connectors.
16 17 18 |
# File 'lib/runtime.rb', line 16 def connectors @connectors end |
#credentials ⇒ Object
Returns the value of attribute credentials.
16 17 18 |
# File 'lib/runtime.rb', line 16 def credentials @credentials end |
#description ⇒ Object
Returns the value of attribute description.
16 17 18 |
# File 'lib/runtime.rb', line 16 def description @description end |
#id ⇒ Object
Returns the value of attribute id.
16 17 18 |
# File 'lib/runtime.rb', line 16 def id @id end |
#instance_id ⇒ Object
Returns the value of attribute instance_id.
16 17 18 |
# File 'lib/runtime.rb', line 16 def instance_id @instance_id end |
#logger ⇒ Object
Returns the value of attribute logger.
16 17 18 |
# File 'lib/runtime.rb', line 16 def logger @logger end |
#name ⇒ Object
Returns the value of attribute name.
16 17 18 |
# File 'lib/runtime.rb', line 16 def name @name end |
Instance Method Details
#listen(service_id, listener_id, params = {}, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 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 98 99 100 101 102 103 104 105 106 |
# File 'lib/runtime.rb', line 55 def listen(service_id, listener_id, params = {}, &block) ws = @connectors[service_id.to_sym].listener(listener_id) handle_on_open(service_id, listener_id, 'Listener', ws, params) ws.on :close do |event| error 'Listener disconnected' if @reconnect warn 'Reconnecting...' sleep 3 ws.open end end ws.on :message do |event| 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' 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']}" (listener_response) else error "Unknown listener response: #{listener_response}" end end ws.on :retry do |event| warn event[:message] end ws.on :error do |event| err = 'Error during WebSocket handshake: Unexpected response code: 401' if event. == err error "Sorry but you don't have access to this listener, | either because your token is invalid or your plan doesn't | support this listener" else error 'Failure in WebSocket connection to connector service' end end ws.open @sockets << ws end |
#load(workflow_definition) ⇒ Object
42 43 44 45 46 |
# File 'lib/runtime.rb', line 42 def load(workflow_definition) EM.run do self.instance_eval(workflow_definition) end end |
#run(service_id, action_id, params = {}, &block) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/runtime.rb', line 108 def run(service_id, action_id, params = {}, &block) ws = @connectors[service_id.to_sym].action(action_id) handle_on_open(service_id, action_id, 'Action', ws, params) ws.on :error do |event| error 'Connection dropped while calling action' end ws.on :message do |event| action_response = JSON.parse(event.data) case action_response['type'] when 'return' ws.close success "Action '#{service_id}::#{action_id}' responded" error_handle_call(action_response, &block) when 'fail' ws.close error " #{action_response['message']}" error "Action '#{service_id}::#{action_id}' failed" when 'log' action_response['message'] = " #{action_response['message']}" (action_response) else error "Unknown action response: #{action_response}" end end ws.open @sockets << ws end |
#workflow(key, value) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/runtime.rb', line 48 def workflow(key, value) @workflow_spec[key] = value @name = value if @key == 'name' @id = value if @key == 'id' @description = value if @key == 'description' end |