Class: PatientHttp::Sidekiq::CallbackWorker Private

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
lib/patient_http/sidekiq/callback_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 that invokes callback services for HTTP request results.

This worker receives serialized Response or Error data and invokes the appropriate callback service method (+on_complete+ or on_error).

Callback services are plain Ruby classes that define on_complete and on_error instance methods:

Examples:

Callback service

class MyCallback
  def on_complete(response)
    # Handle successful response
    User.find(response.callback_args[:user_id]).update!(data: response.json)
  end

  def on_error(error)
    # Handle request error
    Rails.logger.error("Request failed: #{error.message}")
  end
end

Instance Method Summary collapse

Instance Method Details

#perform(data, result_type, callback_service_name) ⇒ Object

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.

Perform the callback invocation.

Parameters:

  • data (Hash)

    Response or Error data (possibly a storage reference)

  • result_type (String)

    "response" or "error" indicating the type of result

  • callback_service_name (String)

    Fully qualified callback service class name



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/patient_http/sidekiq/callback_worker.rb', line 69

def perform(data, result_type, callback_service_name)
  callback_service_class = PatientHttp::ClassHelper.resolve_class_name(callback_service_name)
  callback_service = callback_service_class.new

  # Fetch from external storage if needed
  ref_data = Sidekiq.external_storage.storage_ref?(data) ? data : nil
  actual_data = ref_data ? Sidekiq.external_storage.fetch(data) : data
  actual_data = Sidekiq.decrypt(actual_data)

  if result_type == "response"
    response = PatientHttp::Response.load(actual_data)
    PatientHttp::Sidekiq.invoke_completion_callbacks(response)
    callback_service.on_complete(response)
  elsif result_type == "error"
    error = PatientHttp::Error.load(actual_data)
    PatientHttp::Sidekiq.invoke_error_callbacks(error)
    callback_service.on_error(error)
  else
    raise ArgumentError, "Unknown result_type: #{result_type}"
  end

  # Only delete the stored payload after the callback succeeds so that
  # Sidekiq retries can still fetch it. Failed jobs are cleaned up by
  # the sidekiq_retries_exhausted hook.
  Sidekiq.external_storage.delete(ref_data) if ref_data
end