Class: Factor::Runtime::Workflow

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connectors, credentials, options = {}) ⇒ Workflow

Returns a new instance of Workflow.



17
18
19
20
21
22
23
24
25
26
# File 'lib/runtime/workflow.rb', line 17

def initialize(connectors, credentials, options={})
  @workflow_spec  = {}
  @workflows      = {}
  @instance_id    = SecureRandom.hex(3)
  @reconnect      = true
  @logger         = options[:logger] if options[:logger]

  @connectors = Factor::Common.flat_hash(connectors)
  @credentials = credentials
end

Instance Attribute Details

#connectorsObject

Returns the value of attribute connectors.



15
16
17
# File 'lib/runtime/workflow.rb', line 15

def connectors
  @connectors
end

#credentialsObject

Returns the value of attribute credentials.



15
16
17
# File 'lib/runtime/workflow.rb', line 15

def credentials
  @credentials
end

#descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/runtime/workflow.rb', line 15

def description
  @description
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/runtime/workflow.rb', line 15

def id
  @id
end

#instance_idObject

Returns the value of attribute instance_id.



15
16
17
# File 'lib/runtime/workflow.rb', line 15

def instance_id
  @instance_id
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/runtime/workflow.rb', line 15

def name
  @name
end

Instance Method Details

#error(message) ⇒ Object



179
180
181
# File 'lib/runtime/workflow.rb', line 179

def error(message)
  @logger.error message
end

#info(message) ⇒ Object



171
172
173
# File 'lib/runtime/workflow.rb', line 171

def info(message)
  @logger.info message
end

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



37
38
39
40
41
42
43
44
45
46
47
48
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
# File 'lib/runtime/workflow.rb', line 37

def listen(service_ref, params = {}, &block)
  address = ServiceAddress.new(service_ref)
  e = ExecHandler.new(service_ref, params)
  connector_url = @connectors[address.namespace]

  unless connector_url
    error "Listener '#{address}' not found"
    e.fail_block.call({}) if e.fail_block
  else
    caller = ServiceCaller.new(connector_url)

    caller.on :close do
      error "Listener '#{address}' disconnected"
    end

    caller.on :open do
      info "Listener '#{address}' starting"
    end

    caller.on :retry do |retry_info|
      if retry_info
        details = " ("
        details << "retry #{retry_info[:count]}"
        details << ", offline for #{retry_info[:offline_duration]} seconds" if retry_info[:offline_duration] > 0
        details << ")"
      end
      warn "Listener '#{address}' reconnecting#{details || ''}"
    end

    caller.on :error do
      error "Listener '#{address}' dropped the connection"
    end

    caller.on :return do |data|
      success "Listener '#{address}' started"
    end

    caller.on :start_workflow do |data|
      success "Listener '#{address}' triggered"
      block.call(Factor::Common.simple_object_convert(data)) if block
    end

    caller.on :fail do |info|
      error "Listener '#{address}' failed"
      e.fail_block.call(action_response) if e.fail_block
    end

    caller.on :log do |log_info|
      @logger.log log_info[:status], log_info
    end

    service_credentials = @credentials[address.service.to_sym] || {}
    caller.listen(address.id,params.merge(service_credentials))
  end
  e
end

#load(workflow_definition) ⇒ Object



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

def load(workflow_definition)
  begin
    EM.run do
      instance_eval(workflow_definition)
    end
  rescue Interrupt
  end
end

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



100
101
102
103
104
105
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/runtime/workflow.rb', line 100

def run(service_ref, params = {}, &block)
  address = ServiceAddress.new(service_ref)
  e = ExecHandler.new(service_ref, params)

  if address.workflow?
    workflow_address = address.workflow_address
    workflow = @workflows[workflow_address]

    if workflow
      success "Workflow '#{workflow_address}' starting"
      content = Factor::Common.simple_object_convert(params)
      workflow.call(content)
      success "Workflow '#{workflow_address}' started"
    else
      error "Workflow '#{workflow_address}' not found"
      e.fail_block.call({}) if e.fail_block
    end
  else
    connector_url = @connectors[address.namespace]

    unless connector_url
      error "Action '#{address}' not found"
      e.fail_block.call({}) if e.fail_block
    else
      returned = false
      failed = false
      caller = ServiceCaller.new(connector_url)

      caller.on :open do
        info "Action '#{address}' starting"
      end

      caller.on :error do
        error "Action '#{address}' dropped the connection"
      end

      caller.on :return do |data|
        returned = true
        success "Action '#{address}' responded"
        caller.close
        block.call(Factor::Common.simple_object_convert(data)) if block
      end

      caller.on :close do
        if !returned && !failed
          error "Action '#{address}' disconnected"
          e.fail_block.call({}) if e.fail_block
        end
      end

      caller.on :fail do |info|
        failed = true
        error "Action '#{address}' failed"
        e.fail_block.call(info) if e.fail_block
      end

      caller.on :log do |log_info|
        @logger.log log_info[:status], log_info
      end

      service_credentials = @credentials[address.service.to_sym] || {}
      caller.action(address.id,params.merge(service_credentials))
    end
  end
  e
end

#success(message) ⇒ Object



167
168
169
# File 'lib/runtime/workflow.rb', line 167

def success(message)
  @logger.success message
end

#warn(message) ⇒ Object



175
176
177
# File 'lib/runtime/workflow.rb', line 175

def warn(message)
  @logger.warn message
end

#workflow(service_ref, &block) ⇒ Object



94
95
96
97
98
# File 'lib/runtime/workflow.rb', line 94

def workflow(service_ref, &block)
  address = ServiceAddress.new(service_ref)
  @workflows ||= {}
  @workflows[address] = block
end