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, messenger, exception_notifier) ⇒ ServiceSpawner
constructor
A new instance of ServiceSpawner.
- #log_processed_request(input, result) ⇒ Object
- #spawn ⇒ Object
Constructor Details
#initialize(service, messenger, exception_notifier) ⇒ ServiceSpawner
Returns a new instance of ServiceSpawner.
121 122 123 124 125 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 121 def initialize(service, messenger, exception_notifier) @service = service @messenger = messenger @exception_notifier = exception_notifier end |
Class Method Details
.spawn(service, messenger, exception_notifier) ⇒ Object
117 118 119 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 117 def self.spawn(service, messenger, exception_notifier) new(service, messenger, exception_notifier).spawn end |
Instance Method Details
#delegate_to_service(input) ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 175 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
192 193 194 195 196 197 198 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 192 def handle_exception(e, input) PivotProcess.logger.error(e.inspect + "\n" + e.backtrace.join("\n"), request_id: input[:request_id]) 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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 140 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
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 164 def handle_request(input) PivotProcess.logger.debug "Received request", 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
156 157 158 159 160 161 162 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 156 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
184 185 186 187 188 189 190 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 184 def log_processed_request(input, result) attributes = result .select {|k, _| PROCESSED_REQUEST_LOG_KEYS.include?(k)} .merge(request_id: input[:request_id], type: input[:type]) PivotProcess.logger.info "Processed request", attributes end |
#spawn ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 127 def spawn @messenger.respond_to(@service.class::QUEUE) do |input, handler| request_id = SecureRandom.hex(5) response = handle_request(input.merge(request_id: request_id)) if response.respond_to?(:fulfilled?) handle_fulfillable_response(input, handler, response) else handle_response(handler, response) end end end |