Class: Factor::Runtime

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

Overview

Runtime class is the magic of the server

Defined Under Namespace

Classes: DeepStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connectors, credentials) ⇒ Runtime

Returns a new instance of Runtime.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/runtime.rb', line 19

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 = {}
  flat_hash(connectors).each do |key, connector_url|
    @connectors[key] = Listener.new(connector_url)
  end

  @credentials = {}
  credentials.each do |connector_id, credential_settings|
    @credentials[connector_id] = credential_settings
  end
end

Instance Attribute Details

#connectorsObject

Returns the value of attribute connectors.



17
18
19
# File 'lib/runtime.rb', line 17

def connectors
  @connectors
end

#credentialsObject

Returns the value of attribute credentials.



17
18
19
# File 'lib/runtime.rb', line 17

def credentials
  @credentials
end

#descriptionObject

Returns the value of attribute description.



17
18
19
# File 'lib/runtime.rb', line 17

def description
  @description
end

#idObject

Returns the value of attribute id.



17
18
19
# File 'lib/runtime.rb', line 17

def id
  @id
end

#instance_idObject

Returns the value of attribute instance_id.



17
18
19
# File 'lib/runtime.rb', line 17

def instance_id
  @instance_id
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/runtime.rb', line 17

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



17
18
19
# File 'lib/runtime.rb', line 17

def name
  @name
end

Instance Method Details

#listen(service_ref, params = {}, &block) ⇒ Object



49
50
51
52
53
54
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
# File 'lib/runtime.rb', line 49

def listen(service_ref, params = {}, &block)
  service_map = service_ref.split('::') 
  service_id = service_map.first
  listener_id = service_map.last
  service_key = service_map[0..-2].map{|k| k.to_sym}

  ws = @connectors[service_key].listener(listener_id)

  handle_on_open(service_ref, 'Listener', ws, params)

  ws.on :close do
    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 'return'
      success "Workflow '#{service_ref}' started"
    when 'fail'
      error "Workflow '#{service_ref}' failed to start"
    when 'log'
      listener_response['message'] = "  #{listener_response['message']}"
      log_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.message == 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



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

def load(workflow_definition)
  EM.run do
    instance_eval(workflow_definition)
  end
end

#run(service_ref, params = {}, &block) ⇒ Object



106
107
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
140
141
142
# File 'lib/runtime.rb', line 106

def run(service_ref, params = {}, &block)
  service_map = service_ref.split('::') 
  service_id = service_map.first
  action_id = service_map.last
  service_key = service_map[0..-2].map{|k| k.to_sym}

  ws = @connectors[service_key].action(action_id)

  handle_on_open(service_ref, 'Action', ws, params)

  ws.on :error do
    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_ref}' responded"
      error_handle_call(action_response, &block)
    when 'fail'
      ws.close
      error "  #{action_response['message']}"
      error "Action '#{service_ref}' failed"
    when 'log'
      action_response['message'] = "  #{action_response['message']}"
      log_message(action_response)
    else
      error "Unknown action response: #{action_response}"
    end
  end

  ws.open

  @sockets << ws
end