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



165
166
167
# File 'lib/runtime/workflow.rb', line 165

def error(message)
  @logger.error message
end

#info(message) ⇒ Object



157
158
159
# File 'lib/runtime/workflow.rb', line 157

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
# 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]

  if !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['payload']))
    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

    caller.listen(address.id,params)
  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



99
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
# File 'lib/runtime/workflow.rb', line 99

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]
    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|
      success "Action '#{address}' responded"
      caller.close
      block.call(Factor::Common.simple_object_convert(data['payload']))
    end

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

    caller.on :fail do |info|
      error "Action '#{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

    caller.action(address.id,params)
  end
  e
end

#success(message) ⇒ Object



153
154
155
# File 'lib/runtime/workflow.rb', line 153

def success(message)
  @logger.success message
end

#warn(message) ⇒ Object



161
162
163
# File 'lib/runtime/workflow.rb', line 161

def warn(message)
  @logger.warn message
end

#workflow(service_ref, &block) ⇒ Object



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

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