Class: Salemove::ProcessHandler::PivotProcess::ServiceSpawner
- Inherits:
-
Object
- Object
- Salemove::ProcessHandler::PivotProcess::ServiceSpawner
- Defined in:
- lib/salemove/process_handler/pivot_process.rb
Constant Summary collapse
- PROCESSED_REQUEST_LOG_KEYS =
[:error, :success]
Class Method Summary collapse
Instance Method Summary collapse
- #delegate_to_service(input) ⇒ Object
- #handle_exception(e, input) ⇒ Object
- #handle_fulfillable_response(input, handler, response) ⇒ Object
- #handle_request(input) ⇒ Object
- #handle_response(handler, response) ⇒ Object
-
#initialize(service, freddy, exception_notifier) ⇒ ServiceSpawner
constructor
A new instance of ServiceSpawner.
- #log_processed_request(input, result) ⇒ Object
- #spawn ⇒ Object
Constructor Details
#initialize(service, freddy, exception_notifier) ⇒ ServiceSpawner
Returns a new instance of ServiceSpawner.
133 134 135 136 137 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 133 def initialize(service, freddy, exception_notifier) @service = service @freddy = freddy @exception_notifier = exception_notifier end |
Class Method Details
.spawn(service, freddy, exception_notifier) ⇒ Object
129 130 131 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 129 def self.spawn(service, freddy, exception_notifier) new(service, freddy, exception_notifier).spawn end |
Instance Method Details
#delegate_to_service(input) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 185 def delegate_to_service(input) result = PivotProcess.benchmark(input) { @service.call(input) } if !result.respond_to?(:fulfilled?) log_processed_request(input, result) end result end |
#handle_exception(e, input) ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 203 def handle_exception(e, input) PivotProcess.logger.error(e.inspect + "\n" + e.backtrace.join("\n"), PivotProcess.trace_information) if @exception_notifier @exception_notifier.notify_or_ignore(e, cgi_data: ENV.to_hash, parameters: input) end { success: false, error: e. } end |
#handle_fulfillable_response(input, handler, response) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 150 def handle_fulfillable_response(input, handler, response) timeout = response.respond_to?(:timeout) && response.timeout || DEFAULT_FULFILLABLE_TIMEOUT Timeout::timeout(timeout) do while true if response.fulfilled? log_processed_request(input, response.value) return handle_response(handler, response.value) end sleep 0.001 end end rescue Timeout::Error PivotProcess.logger.error "Fullfillable response was not fulfilled in #{timeout} seconds", input handle_response(handler, success: false, error: "Fulfillable response was not fulfilled") end |
#handle_request(input) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 174 def handle_request(input) PivotProcess.logger.info "Received request", PivotProcess.trace_information.merge(input) if input.has_key?(:ping) { success: true, pong: 'pong' } else delegate_to_service(input) end rescue => exception handle_exception(exception, input) end |
#handle_response(handler, response) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 166 def handle_response(handler, response) if response.is_a?(Hash) && (response[:success] == false || response[:error]) handler.error(response) else handler.success(response) end end |
#log_processed_request(input, result) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 194 def log_processed_request(input, result) attributes = result .select {|k, _| PROCESSED_REQUEST_LOG_KEYS.include?(k)} .merge(type: input[:type]) .merge(PivotProcess.trace_information) PivotProcess.logger.info "Processed request", attributes end |
#spawn ⇒ Object
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 139 def spawn @freddy.respond_to(@service.class::QUEUE) do |input, handler| response = handle_request(input) if response.respond_to?(:fulfilled?) handle_fulfillable_response(input, handler, response) else handle_response(handler, response) end end end |