Class: Factor::Runtime::Workflow
- Inherits:
-
Object
- Object
- Factor::Runtime::Workflow
- Defined in:
- lib/runtime/workflow.rb
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.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #error(message) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(connectors, credentials, options = {}) ⇒ Workflow
constructor
A new instance of Workflow.
- #listen(service_ref, params = {}, &block) ⇒ Object
- #load(workflow_definition) ⇒ Object
- #run(service_ref, params = {}, &block) ⇒ Object
- #success(message) ⇒ Object
- #warn(message) ⇒ Object
- #workflow(service_ref, &block) ⇒ Object
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, ={}) @workflow_spec = {} @workflows = {} @instance_id = SecureRandom.hex(3) @reconnect = true @logger = [:logger] if [:logger] @connectors = Factor::Common.flat_hash(connectors) @credentials = credentials end |
Instance Attribute Details
#connectors ⇒ Object
Returns the value of attribute connectors.
15 16 17 |
# File 'lib/runtime/workflow.rb', line 15 def connectors @connectors end |
#credentials ⇒ Object
Returns the value of attribute credentials.
15 16 17 |
# File 'lib/runtime/workflow.rb', line 15 def credentials @credentials end |
#description ⇒ Object
Returns the value of attribute description.
15 16 17 |
# File 'lib/runtime/workflow.rb', line 15 def description @description end |
#id ⇒ Object
Returns the value of attribute id.
15 16 17 |
# File 'lib/runtime/workflow.rb', line 15 def id @id end |
#instance_id ⇒ Object
Returns the value of attribute instance_id.
15 16 17 |
# File 'lib/runtime/workflow.rb', line 15 def instance_id @instance_id end |
#name ⇒ Object
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() @logger.error end |
#info(message) ⇒ Object
157 158 159 |
# File 'lib/runtime/workflow.rb', line 157 def info() @logger.info 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() @logger.success end |
#warn(message) ⇒ Object
161 162 163 |
# File 'lib/runtime/workflow.rb', line 161 def warn() @logger.warn 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 |