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]
Instance Method Summary collapse
- #delegate_to_service(input) ⇒ Object
- #handle_exception(exception, input) ⇒ Object
- #handle_fulfillable_response(input, handler, response) ⇒ Object
- #handle_request(input) ⇒ Object
- #handle_response(handler, response) ⇒ Object
-
#initialize(service, freddy:, logger:, benchmarker:, exception_notifier:, log_error_as_string:) ⇒ ServiceSpawner
constructor
A new instance of ServiceSpawner.
- #log_processed_request(input, result) ⇒ Object
- #spawn ⇒ Object
Constructor Details
#initialize(service, freddy:, logger:, benchmarker:, exception_notifier:, log_error_as_string:) ⇒ ServiceSpawner
Returns a new instance of ServiceSpawner.
129 130 131 132 133 134 135 136 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 129 def initialize(service, freddy:, logger:, benchmarker:, exception_notifier:, log_error_as_string:) @service = service @freddy = freddy @logger = logger @benchmarker = benchmarker @exception_notifier = exception_notifier @log_error_as_string = log_error_as_string end |
Instance Method Details
#delegate_to_service(input) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 184 def delegate_to_service(input) result = @benchmarker.call(input) { @service.call(input) } unless result.respond_to?(:fulfilled?) log_processed_request(input, result) end result end |
#handle_exception(exception, input) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 206 def handle_exception(exception, input) = [exception.inspect, *exception.backtrace].join("\n") @logger.error(, input) if @exception_notifier @exception_notifier.notify_or_ignore( exception, cgi_data: ENV.to_hash, parameters: input ) end { success: false, error: exception. } end |
#handle_fulfillable_response(input, handler, response) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 149 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 @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
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 173 def handle_request(input) @logger.info 'Received request', input if input.has_key?(:ping) { success: true, pong: 'pong' } else delegate_to_service(input) end rescue StandardError => exception handle_exception(exception, input) end |
#handle_response(handler, response) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 165 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 202 203 204 |
# 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(input) if @log_error_as_string attributes[:error] = attributes[:error].to_s if attributes.has_key?(:error) end @logger.info 'Processed request', attributes end |
#spawn ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/salemove/process_handler/pivot_process.rb', line 138 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 |