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, synchronous: false, callback_args: nil, raise_error_responses: false, request_id: nil) ⇒ String
private
Execute the request directly on the async processor.
Class Method Details
.execute(request, callback:, sidekiq_job: nil, synchronous: false, callback_args: nil, raise_error_responses: false, request_id: 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. It must be called from within a Sidekiq job context (the sidekiq_job parameter is required). 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.
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 |
# File 'lib/patient_http/sidekiq/request_executor.rb', line 37 def execute( request, callback:, sidekiq_job: nil, synchronous: false, callback_args: nil, raise_error_responses: false, request_id: nil ) sidekiq_job = validate_sidekiq_job(sidekiq_job) config = PatientHttp::Sidekiq.configuration task_handler = TaskHandler.new(sidekiq_job) 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: config.max_redirects ) # Run the request inline if Sidekiq::Testing.inline! is enabled if synchronous || async_disabled? PatientHttp::SynchronousExecutor.new( task, config: config, on_complete: ->(response) { PatientHttp::Sidekiq.invoke_completion_callbacks(response) }, on_error: ->(error) { PatientHttp::Sidekiq.invoke_error_callbacks(error) } ).call return task.id end # Check if processor is running processor = PatientHttp::Sidekiq.processor unless processor&.running? raise PatientHttp::NotRunningError.new("Cannot enqueue request: processor is not running") end processor.enqueue(task) task.id end |