Class: ComplyanceSDK::Retry::RetryManager

Inherits:
Object
  • Object
show all
Defined in:
lib/complyance_sdk/retry/retry_manager.rb

Overview

Manager class that coordinates retry strategies and circuit breakers

Instance Method Summary collapse

Constructor Details

#initialize(config, redis_config = {}) ⇒ RetryManager

Initialize a new retry manager

Parameters:



14
15
16
17
18
# File 'lib/complyance_sdk/retry/retry_manager.rb', line 14

def initialize(config, redis_config = {})
  @config = config
  @redis_config = redis_config
  @circuit_breakers = {}
end

Instance Method Details

#circuit_breaker_status(operation_name) ⇒ Hash

Get circuit breaker status for an operation

Parameters:

  • operation_name (String)

    Name of the operation

Returns:

  • (Hash)

    Circuit breaker status information



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/complyance_sdk/retry/retry_manager.rb', line 72

def circuit_breaker_status(operation_name)
  circuit_breaker = get_circuit_breaker(operation_name)
  
  {
    name: operation_name,
    state: circuit_breaker.current_state,
    failure_count: circuit_breaker.failure_count,
    last_failure_time: circuit_breaker.last_failure_time,
    disabled: circuit_breaker.disabled?
  }
end

#execute(operation_name, context = {}) { ... } ⇒ Object

Execute a block with retry logic and circuit breaker protection

Parameters:

  • operation_name (String)

    Name of the operation for circuit breaker

  • context (Hash) (defaults to: {})

    Context information for logging/debugging

Yields:

  • The block to execute

Returns:

  • The result of the block



26
27
28
29
30
31
32
33
# File 'lib/complyance_sdk/retry/retry_manager.rb', line 26

def execute(operation_name, context = {})
  circuit_breaker = get_circuit_breaker(operation_name)
  retry_strategy = RetryStrategy.new(@config.retry_config, circuit_breaker)
  
  retry_strategy.execute(context.merge(operation: operation_name)) do
    yield
  end
end

#execute_async(request_data, job_type: :active_job, callback_url: nil, callback_headers: {}) ⇒ String, Object

Execute a block asynchronously using background jobs

Parameters:

  • request_data (Hash)

    The request data

  • job_type (Symbol) (defaults to: :active_job)

    The job type (:active_job or :sidekiq)

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

    Optional callback URL

  • callback_headers (Hash) (defaults to: {})

    Optional callback headers

Returns:

  • (String, Object)

    Job ID or job object



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
# File 'lib/complyance_sdk/retry/retry_manager.rb', line 42

def execute_async(request_data, job_type: :active_job, callback_url: nil, callback_headers: {})
  case job_type
  when :active_job
    if defined?(ActiveJob)
      require_relative "../jobs/process_document_job"
      job = Jobs::ProcessDocumentJob.perform_later(request_data, callback_url, callback_headers)
      job.job_id
    else
      raise ComplyanceSDK::Exceptions::ConfigurationError.new(
        "ActiveJob is not available. Please add 'activejob' to your Gemfile."
      )
    end
  when :sidekiq
    if defined?(Sidekiq)
      require_relative "../jobs/sidekiq_job"
      Jobs::SidekiqJob.perform_async(request_data, callback_url, callback_headers)
    else
      raise ComplyanceSDK::Exceptions::ConfigurationError.new(
        "Sidekiq is not available. Please add 'sidekiq' to your Gemfile."
      )
    end
  else
    raise ArgumentError, "Invalid job_type: #{job_type}. Must be :active_job or :sidekiq"
  end
end

#reset_all_circuit_breakersObject

Reset all circuit breakers



93
94
95
# File 'lib/complyance_sdk/retry/retry_manager.rb', line 93

def reset_all_circuit_breakers
  @circuit_breakers.each_value(&:reset!)
end

#reset_circuit_breaker(operation_name) ⇒ Object

Reset a circuit breaker

Parameters:

  • operation_name (String)

    Name of the operation



87
88
89
90
# File 'lib/complyance_sdk/retry/retry_manager.rb', line 87

def reset_circuit_breaker(operation_name)
  circuit_breaker = get_circuit_breaker(operation_name)
  circuit_breaker.reset!
end