Class: WhatsAppNotifier::Bulk::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp_notifier/bulk/dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:, configuration:, sleeper: ->(seconds) { sleep(seconds) }, rng: Random.new) ⇒ Dispatcher

Returns a new instance of Dispatcher.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/whatsapp_notifier/bulk/dispatcher.rb', line 6

def initialize(client:, configuration:, sleeper: ->(seconds) { sleep(seconds) }, rng: Random.new)
  @client = client
  @configuration = configuration
  @rate_limiter = RateLimiter.new(
    base_delay: configuration.bulk_base_delay_seconds,
    jitter: configuration.bulk_jitter_seconds,
    sleeper: sleeper,
    rng: rng
  )
  @retry_policy = RetryPolicy.new(
    max_attempts: configuration.bulk_max_attempts,
    retryable_error_codes: configuration.bulk_retryable_error_codes
  )
  @sleeper = sleeper
end

Instance Method Details

#deliver(messages, provider: nil) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/whatsapp_notifier/bulk/dispatcher.rb', line 22

def deliver(messages, provider: nil)
  raise ConfigurationError, "messages must be an array" unless messages.is_a?(Array)
  raise ConfigurationError, "bulk_max_recipients exceeded" if messages.length > @configuration.bulk_max_recipients

  sent_keys = Set.new
  results = messages.map.with_index do |message, idx|
    @rate_limiter.wait_before_next if idx.positive?
    deliver_one(message, sent_keys: sent_keys, provider: provider)
  end

  { total: results.length, success: results.count(&:success?), failed: results.count(&:failure?), results: results }
end