Class: DockerSwarm::Connection
- Inherits:
-
Object
- Object
- DockerSwarm::Connection
- Defined in:
- lib/docker_swarm/connection.rb
Constant Summary collapse
- IDEMPOTENT_METHODS =
HTTP methods safe to retry automatically. POST/PATCH are excluded because replaying after a partial failure can create duplicate resources.
%i[get head put delete options].freeze
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#socket_path ⇒ Object
readonly
Returns the value of attribute socket_path.
Instance Method Summary collapse
-
#initialize(socket_path, logger) ⇒ Connection
constructor
A new instance of Connection.
- #request(options = {}) ⇒ Object
Constructor Details
#initialize(socket_path, logger) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 |
# File 'lib/docker_swarm/connection.rb', line 11 def initialize(socket_path, logger) @socket_path = socket_path @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/docker_swarm/connection.rb', line 9 def logger @logger end |
#socket_path ⇒ Object (readonly)
Returns the value of attribute socket_path.
9 10 11 |
# File 'lib/docker_swarm/connection.rb', line 9 def socket_path @socket_path end |
Instance Method Details
#request(options = {}) ⇒ Object
16 17 18 19 20 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 |
# File 'lib/docker_swarm/connection.rb', line 16 def request( = {}) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) log_event("request_started", level: :debug, data: .merge(path: [:path])) method = [:method].to_s.downcase.to_sym idempotent = IDEMPOTENT_METHODS.include?(method) = { idempotent: idempotent, retry_errors: [ Excon::Error::Socket, Excon::Error::Timeout ], read_timeout: DockerSwarm.configuration.read_timeout.to_f, write_timeout: DockerSwarm.configuration.write_timeout.to_f, connect_timeout: DockerSwarm.configuration.connect_timeout.to_f, retries: idempotent ? DockerSwarm.configuration.max_retries.to_i : 0 }.merge() response = client.request() log_event("request_success", data: .merge( http_status: response.status, duration_s: calculate_duration(start_time) )) response.body rescue => e # Excon suele envolver excepciones de middleware en Excon::Error::Socket. # Recuperamos la causa original si es una excepción de DockerSwarm. actual_error = e.cause.is_a?(::DockerSwarm::Error) ? e.cause : e log_event("request_failure", level: :error, data: .merge( error: actual_error.class.name, message: actual_error., duration_s: calculate_duration(start_time) )) case actual_error when ::DockerSwarm::Error raise actual_error when Excon::Error::Socket raise ::DockerSwarm::Error::Communication, "Docker socket error: #{actual_error.}" else raise actual_error end end |