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.



137
138
139
140
141
142
143
144
# File 'lib/salemove/process_handler/pivot_process.rb', line 137

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



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

def delegate_to_service(input)
  result = @benchmarker.call(input) { @service.call(input) }
  if !result.respond_to?(:fulfilled?)
    log_processed_request(input, result)
  end

  result
end

#handle_exception(e, input) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/salemove/process_handler/pivot_process.rb', line 213

def handle_exception(e, input)
  @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.message }
end

#handle_fulfillable_response(input, handler, response) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/salemove/process_handler/pivot_process.rb', line 157

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



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

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

#handle_response(handler, response) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/salemove/process_handler/pivot_process.rb', line 173

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



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

def log_processed_request(input, result)
  attributes = result
    .select {|k, _| PROCESSED_REQUEST_LOG_KEYS.include?(k)}
    .merge(type: input[:type])
    .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



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

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