Class: PatientHttp::SolidQueue::RequestExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_http/solid_queue/request_executor.rb

Overview

Helper methods for executing HTTP requests asynchronously.

Class Method Summary collapse

Class Method Details

.execute(request, callback:, active_job_data: nil, synchronous: false, callback_args: nil, raise_error_responses: false, request_id: nil, processor_name: nil) ⇒ String

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.

Execute the request directly on the async processor.

Parameters:

  • request (PatientHttp::Request)

    the HTTP request to execute

  • callback (Class, String)

    Callback service class or its fully qualified class name

  • active_job_data (Hash, nil) (defaults to: nil)

    Active Job serialized hash with "job_class" and "arguments" keys

  • synchronous (Boolean) (defaults to: false)

    If true, runs the request inline (for testing)

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

    Arguments to pass to callback

  • raise_error_responses (Boolean) (defaults to: false)

    If true, treats non-2xx responses as errors

  • request_id (String, nil) (defaults to: nil)

    Unique request ID for tracking

  • processor_name (Symbol, String, nil) (defaults to: nil)

    Name of the processor profile to run the request on. Defaults to the request's own processor name or :default.

Returns:

  • (String)

    the request ID



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/patient_http/solid_queue/request_executor.rb', line 21

def execute(
  request,
  callback:,
  active_job_data: nil,
  synchronous: false,
  callback_args: nil,
  raise_error_responses: false,
  request_id: nil,
  processor_name: nil
)
  active_job_data = validate_active_job_data(active_job_data)
  task_handler = TaskHandler.new(active_job_data)
  config = PatientHttp::SolidQueue.configuration

  # Resolve the processor profile up front so the task is built with
  # the options of the processor that will run it. An unknown name
  # falls back to the base configuration here and is reported below.
  name = (processor_name || request.processor || :default).to_sym
  profile_config = config.processor_profiles.key?(name) ? config.processor_config(name) : config

  task = PatientHttp::RequestTask.new(
    request: request,
    task_handler: task_handler,
    callback: callback,
    callback_args: callback_args,
    raise_error_responses: raise_error_responses,
    id: request_id,
    default_max_redirects: profile_config.max_redirects
  )

  if synchronous || async_disabled?
    PatientHttp::SynchronousExecutor.new(
      task,
      config: profile_config,
      on_complete: ->(response) { PatientHttp::SolidQueue.invoke_completion_callbacks(response) },
      on_error: ->(error) { PatientHttp::SolidQueue.invoke_error_callbacks(error) }
    ).call
    return task.id
  end

  # Look up the named processor. An unknown name raises so the job
  # lands in Active Job's retry mechanism instead of being dropped;
  # this covers rolling deploys where an old process has not
  # configured a new profile yet.
  processor = PatientHttp::SolidQueue.processor(name)
  if processor.nil? && !config.processor_profiles.key?(name)
    raise PatientHttp::UnknownProcessorError, "No processor profile configured for #{name.inspect}"
  end

  unless processor&.running?
    raise PatientHttp::NotRunningError, "Cannot enqueue request: processor is not running"
  end

  # Advisory capacity check before enqueueing. A real enqueue writes
  # the durable registry record before the authoritative capacity
  # check, so a full processor would pay a database insert and delete
  # just to be rejected. This peek rejects for free; the race where
  # capacity fills after the peek falls through to the normal
  # rejection path.
  unless processor.capacity_available?
    raise PatientHttp::MaxCapacityError,
      "Cannot enqueue request: processor #{name} is at max capacity (#{processor.config.max_connections} connections)"
  end

  processor.enqueue(task)
  task.id
end