Class: PatientHttp::Sidekiq::RequestExecutor
- Inherits:
-
Object
- Object
- PatientHttp::Sidekiq::RequestExecutor
- Defined in:
- lib/patient_http/sidekiq/request_executor.rb
Overview
Helper methods for executing HTTP requests asynchronously.
Class Method Summary collapse
-
.execute(request, callback:, sidekiq_job: nil, task_handler: nil, synchronous: false, callback_args: nil, raise_error_responses: false, request_id: nil, processor_name: nil) ⇒ String
private
Execute the request directly on the async processor.
Class Method Details
.execute(request, callback:, sidekiq_job: nil, task_handler: 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.
This method enqueues the request directly to the async processor. Used internally by RequestWorker.
When the request completes, the callback's on_complete method is called with
a Response object. If an error occurs (network error, timeout, or non-2xx response
if raise_error_responses is true), the on_error method is called with an Error object.
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/patient_http/sidekiq/request_executor.rb', line 41 def execute( request, callback:, sidekiq_job: nil, task_handler: nil, synchronous: false, callback_args: nil, raise_error_responses: false, request_id: nil, processor_name: nil ) task_handler ||= TaskHandler.new(validate_sidekiq_job(sidekiq_job)) config = PatientHttp::Sidekiq.configuration # Look up the named processor and the effective configuration for its # profile, so per-processor overrides apply to the request itself. # A running processor already holds the built profile configuration. name = (processor_name || request.processor || :default).to_sym processor = PatientHttp::Sidekiq.processor(name) profile_declared = config.processor_profiles.key?(name) task_config = processor&.config || (profile_declared ? 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: task_config.max_redirects ) # Run the request inline if Sidekiq::Testing.inline! is enabled if synchronous || async_disabled? PatientHttp::SynchronousExecutor.new( task, config: task_config, on_complete: ->(response) { PatientHttp::Sidekiq.invoke_completion_callbacks(response) }, on_error: ->(error) { PatientHttp::Sidekiq.invoke_error_callbacks(error) } ).call return task.id end # An unknown name raises so the job lands in Sidekiq's retry # mechanism instead of being dropped; this covers rolling deploys # where an old process has not configured a new profile yet. if processor.nil? && !profile_declared raise PatientHttp::UnknownProcessorError.new("No processor profile configured for #{name.inspect}") end unless processor&.running? raise PatientHttp::NotRunningError.new("Cannot enqueue request: processor is not running") end # Advisory capacity check before enqueueing. A real enqueue pays for # durable registration before the authoritative capacity check, so a # full processor would cost several Redis round trips 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? PatientHttp::Sidekiq.stats.record_capacity_exceeded(processor_name: name) raise PatientHttp::MaxCapacityError.new( "Cannot enqueue request: processor #{name} is at max capacity (#{processor.config.max_connections} connections)" ) end processor.enqueue(task) task.id end |