Module: PatientHttp::Sidekiq

Defined in:
lib/patient_http/sidekiq.rb,
lib/patient_http/sidekiq/stats.rb,
lib/patient_http/sidekiq/web_ui.rb,
lib/patient_http/sidekiq/context.rb,
lib/patient_http/sidekiq/task_handler.rb,
lib/patient_http/sidekiq/task_monitor.rb,
lib/patient_http/sidekiq/configuration.rb,
lib/patient_http/sidekiq/request_worker.rb,
lib/patient_http/sidekiq/callback_worker.rb,
lib/patient_http/sidekiq/lifecycle_hooks.rb,
lib/patient_http/sidekiq/request_executor.rb,
lib/patient_http/sidekiq/processor_observer.rb,
lib/patient_http/sidekiq/task_monitor_thread.rb

Defined Under Namespace

Classes: CallbackWorker, Configuration, Context, LifecycleHooks, ProcessorObserver, RequestExecutor, RequestWorker, Stats, TaskHandler, TaskMonitor, TaskMonitorThread, WebUI

Constant Summary collapse

VERSION =
File.read(File.expand_path("../../../VERSION", __FILE__)).strip

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationConfiguration

Ensure configuration is initialized

Returns:



109
110
111
# File 'lib/patient_http/sidekiq.rb', line 109

def configuration
  @configuration ||= Configuration.new
end

.processorPatientHttp::Processor?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the processor instance (internal accessor)

Returns:

  • (PatientHttp::Processor, nil)


344
345
346
# File 'lib/patient_http/sidekiq.rb', line 344

def processor
  @processor
end

Class Method Details

.after_completion {|response| ... } ⇒ Object

Add a callback to be executed after a successful request completion.

Yields:

  • (response)

    block to execute after an HTTP request completes

Yield Parameters:

  • response (PatientHttp::Response)

    the HTTP response



125
126
127
# File 'lib/patient_http/sidekiq.rb', line 125

def after_completion(&block)
  @after_completion_callbacks << block
end

.after_error {|error| ... } ⇒ Object

Add a callback to be executed after a request error.

Yields:

  • (error)

    block to execute after an HTTP request errors

Yield Parameters:

  • error (PatientHttp::Error)

    information about the error that was raised



133
134
135
# File 'lib/patient_http/sidekiq.rb', line 133

def after_error(&block)
  @after_error_callbacks << block
end

.append_middlewarevoid

This method returns an undefined value.

Add Sidekiq middleware for context handling. The middleware is already added during initialization. You can call this method again to append the middleware if needed to insert it after other middleware. If you need further control, you can manually add the PatientHttp::Sidekiq::Context::Middleware middleware yourself.



144
145
146
147
148
149
150
# File 'lib/patient_http/sidekiq.rb', line 144

def append_middleware
  ::Sidekiq.configure_server do |config|
    config.server_middleware do |chain|
      chain.add PatientHttp::Sidekiq::Context::Middleware
    end
  end
end

.configure {|Configuration| ... } ⇒ Configuration

Configure the gem with a block

Yields:

Returns:



98
99
100
101
102
103
104
105
# File 'lib/patient_http/sidekiq.rb', line 98

def configure
  configuration = Configuration.new
  yield(configuration) if block_given?
  @configuration = configuration
  @external_storage = nil
  register_handler
  configuration
end

.decrypt(data) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decrypt data using the configured encryptor.

Parameters:

  • data (Hash)

    the data to decrypt

Returns:

  • (Hash)

    the decrypted data (or original if not encrypted)



203
204
205
# File 'lib/patient_http/sidekiq.rb', line 203

def decrypt(data)
  configuration.encryptor.decrypt(data)
end

.draining?Boolean

Check if the processor is draining (not accepting new requests but still processing in-flight ones).

Returns:

  • (Boolean)


163
164
165
# File 'lib/patient_http/sidekiq.rb', line 163

def draining?
  !!@processor&.draining?
end

.encrypt(data) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encrypt data using the configured encryptor.

Parameters:

  • data (Hash)

    the data to encrypt

Returns:

  • (Hash)

    the encrypted data (or original if no encryption configured)



194
195
196
# File 'lib/patient_http/sidekiq.rb', line 194

def encrypt(data)
  configuration.encryptor.encrypt(data)
end

.execute(request, callback:, callback_args: nil, raise_error_responses: false) ⇒ String

Execute an async HTTP request.

Parameters:

  • request (PatientHttp::Request)

    the HTTP request to execute

  • callback (Class, String)

    Callback service class with on_complete and on_error instance methods, or its fully qualified class name.

  • callback_args (#to_h, nil) (defaults to: nil)

    Arguments to pass to callback via the PatientHttp::Response/PatientHttp::Error object. Must respond to to_h and contain only JSON-native types (nil, true, false, String, Integer, Float, Array, Hash). All hash keys will be converted to strings for serialization. Access via response.callback_args or error.callback_args using symbol or string keys.

  • raise_error_responses (Boolean) (defaults to: false)

    If true, treats non-2xx responses as errors and calls on_error instead of on_complete. Defaults to false.

Returns:

  • (String)

    the request ID



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/patient_http/sidekiq.rb', line 220

def execute(request, callback:, callback_args: nil, raise_error_responses: false)
  PatientHttp::CallbackValidator.validate!(callback)
  callback_name = callback.is_a?(Class) ? callback.name : callback.to_s
  callback_args = PatientHttp::CallbackValidator.validate_callback_args(callback_args)
  request_id = SecureRandom.uuid

  encrypted = encrypt(request.as_json)

  data = if external_storage.enabled?
    external_storage.store(encrypted, max_size: configuration.payload_store_threshold)
  else
    encrypted
  end

  RequestWorker.perform_async(data, callback_name, raise_error_responses, callback_args, request_id)

  request_id
end

.external_storagePatientHttp::ExternalStorage

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get an ExternalStorage instance for storing and fetching payloads.

Returns:

  • (PatientHttp::ExternalStorage)


185
186
187
# File 'lib/patient_http/sidekiq.rb', line 185

def external_storage
  @external_storage ||= PatientHttp::ExternalStorage.new(configuration)
end

.invoke_completion_callbacks(response) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Invoke the registered completion callbacks

Parameters:

  • response (PatientHttp::Response)

    the HTTP response



323
324
325
326
327
# File 'lib/patient_http/sidekiq.rb', line 323

def invoke_completion_callbacks(response)
  @after_completion_callbacks.each do |callback|
    callback.call(response)
  end
end

.invoke_error_callbacks(error) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Invoke the registered error callbacks

Parameters:

  • error (PatientHttp::Error)

    information about the error that was raised



334
335
336
337
338
# File 'lib/patient_http/sidekiq.rb', line 334

def invoke_error_callbacks(error)
  @after_error_callbacks.each do |callback|
    callback.call(error)
  end
end

.quietvoid

This method returns an undefined value.

Signal the processor to drain (stop accepting new requests)



277
278
279
280
281
282
283
# File 'lib/patient_http/sidekiq.rb', line 277

def quiet
  @lifecycle_mutex.synchronize do
    return unless running?

    @processor.drain
  end
end

.register_handlervoid

This method returns an undefined value.

Register Sidekiq as the request handler for processing HTTP requests. This is called automatically when the processor starts or you call PatientHttp::Sidekiq.configure.



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/patient_http/sidekiq.rb', line 243

def register_handler
  @request_handler ||= lambda do |request:, callback:, raise_error_responses:, callback_args:|
    execute(
      request,
      callback: callback,
      raise_error_responses: raise_error_responses,
      callback_args: callback_args
    )
  end

  PatientHttp.register_handler(@request_handler)
end

.reset!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Reset all state (useful for testing)



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/patient_http/sidekiq.rb', line 306

def reset!
  @lifecycle_mutex.synchronize do
    @processor&.stop(timeout: 0)
    @processor = nil
  end
  @configuration = nil
  @external_storage = nil
  @after_completion_callbacks = []
  @after_error_callbacks = []
  PatientHttp.unregister_handler(@request_handler) if @request_handler
end

.reset_configuration!Configuration

Reset configuration to defaults (useful for testing)

Returns:



115
116
117
118
119
# File 'lib/patient_http/sidekiq.rb', line 115

def reset_configuration!
  @configuration = nil
  @external_storage = nil
  configuration
end

.running?Boolean

Check if the processor is running.

Returns:

  • (Boolean)


155
156
157
# File 'lib/patient_http/sidekiq.rb', line 155

def running?
  !!@processor&.running?
end

.startvoid

This method returns an undefined value.

Start the processor



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/patient_http/sidekiq.rb', line 259

def start
  @lifecycle_mutex.synchronize do
    return if @processor && !@processor.stopped?

    @processor = PatientHttp::Processor.new(configuration)
    @processor.observe(ProcessorObserver.new(@processor))
    configuration.observers.each do |observer|
      @processor.observe(observer)
    end
    @processor.start
  end

  register_handler
end

.stop(timeout: nil) ⇒ void

This method returns an undefined value.

Stop the processor gracefully

Parameters:

  • timeout (Float, nil) (defaults to: nil)

    maximum time to wait for in-flight requests to complete



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/patient_http/sidekiq.rb', line 289

def stop(timeout: nil)
  if @request_handler
    PatientHttp.unregister_handler(@request_handler)
  end

  @lifecycle_mutex.synchronize do
    return unless @processor

    @processor.stop(timeout: timeout)
    @processor = nil
  end
end

.stopped?Boolean

Check if the processor is stopped or has not been started.

Returns:

  • (Boolean)


177
178
179
# File 'lib/patient_http/sidekiq.rb', line 177

def stopped?
  @processor.nil? || @processor.stopped?
end

.stopping?Boolean

Check if the processor is in the process of stopping.

Returns:

  • (Boolean)


170
171
172
# File 'lib/patient_http/sidekiq.rb', line 170

def stopping?
  !!@processor&.stopping?
end