Class: PatientHttp::Sidekiq::RequestWorker Private

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
lib/patient_http/sidekiq/request_worker.rb

Overview

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

Sidekiq worker for executing HTTP requests asynchronously.

This worker is enqueued when calling PatientHttp::Sidekiq.get, PatientHttp::Sidekiq.post, etc. It allows HTTP requests to be made from anywhere in your code (not just Sidekiq jobs) while still processing them through the async HTTP processor.

When the request completes, the specified callback service’s on_complete or on_error method is invoked via CallbackWorker.

Instance Method Summary collapse

Instance Method Details

#perform(data, callback_service_name, raise_error_responses, callback_args, request_id) ⇒ 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.

Perform the HTTP request.

Parameters:

  • data (Hash)

    Request data (possibly a storage reference) with keys:

    • “http_method” [String] HTTP method (get, post, put, patch, delete)

    • “url” [String] The request URL

    • “headers” [Hash] Request headers

    • “body” [String, nil] Request body

    • “timeout” [Numeric, nil] Request timeout

    • “max_redirects” [Integer, nil] Maximum redirects to follow

  • callback_service_name (String)

    Fully qualified callback service class name

  • raise_error_responses (Boolean, nil)

    Whether to treat non-2xx responses as errors; defaults to the global config if nil

  • callback_args (Hash, nil)

    Arguments to pass to the callback

  • request_id (String, nil)

    Unique request ID for tracking



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/patient_http/sidekiq/request_worker.rb', line 33

def perform(data, callback_service_name, raise_error_responses, callback_args, request_id)
  # Fetch from external storage if needed
  ref_data = PatientHttp::ExternalStorage.storage_ref?(data) ? data : nil
  actual_data = ref_data ? Sidekiq.external_storage.fetch(data) : data
  actual_data = Sidekiq.decrypt(actual_data)

  request = PatientHttp::Request.load(actual_data)
  sidekiq_job = Sidekiq::Context.current_job

  begin
    RequestExecutor.execute(
      request,
      callback: callback_service_name,
      raise_error_responses: raise_error_responses,
      callback_args: callback_args,
      sidekiq_job: sidekiq_job,
      request_id: request_id
    )
  ensure
    Sidekiq.external_storage.delete(ref_data) if ref_data
  end
end