Class: Salemove::ProcessHandler::PivotProcess::ServiceSpawner

Inherits:
Object
  • Object
show all
Defined in:
lib/salemove/process_handler/pivot_process.rb

Constant Summary collapse

PROCESSED_REQUEST_LOG_KEYS =
[:error, :success]

Instance Method Summary collapse

Constructor Details

#initialize(service, freddy:, logger:, benchmarker:, exception_notifier:, log_error_as_string:) ⇒ ServiceSpawner

Returns a new instance of ServiceSpawner.



145
146
147
148
149
150
151
152
# File 'lib/salemove/process_handler/pivot_process.rb', line 145

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



200
201
202
203
204
205
206
207
208
# File 'lib/salemove/process_handler/pivot_process.rb', line 200

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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/salemove/process_handler/pivot_process.rb', line 223

def handle_exception(exception, input)
  message = [exception.inspect, *exception.backtrace].join("\n")
   = PivotProcess.trace_information.merge(input)

  @logger.error(message, )

  if @exception_notifier
    @exception_notifier.notify_or_ignore(
      exception,
      cgi_data: ENV.to_hash,
      parameters: input
    )
  end

  { success: false, error: exception.message }
end

#handle_fulfillable_response(input, handler, response) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/salemove/process_handler/pivot_process.rb', line 165

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



189
190
191
192
193
194
195
196
197
198
# File 'lib/salemove/process_handler/pivot_process.rb', line 189

def handle_request(input)
  @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 StandardError => exception
  handle_exception(exception, input)
end

#handle_response(handler, response) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/salemove/process_handler/pivot_process.rb', line 181

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



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/salemove/process_handler/pivot_process.rb', line 210

def log_processed_request(input, result)
  attributes = result
    .select { |k, _| PROCESSED_REQUEST_LOG_KEYS.include?(k) }
    .merge(input)
    .merge(PivotProcess.trace_information)

  if @log_error_as_string
    attributes[:error] = attributes[:error].to_s if attributes.has_key?(:error)
  end

  @logger.info 'Processed request', attributes
end

#spawnObject



154
155
156
157
158
159
160
161
162
163
# File 'lib/salemove/process_handler/pivot_process.rb', line 154

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