Module: Webhooks::Outgoing::DeliverySupport

Extended by:
ActiveSupport::Concern
Included in:
Delivery
Defined in:
app/models/concerns/webhooks/outgoing/delivery_support.rb

Constant Summary collapse

ATTEMPT_SCHEDULE =
{
  1 => 15.seconds,
  2 => 1.minute,
  3 => 5.minutes,
  4 => 15.minutes,
  5 => 1.hour,
}

Instance Method Summary collapse

Instance Method Details

#attempt_countObject



44
45
46
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 44

def attempt_count
  delivery_attempts.count
end

#deliverObject



34
35
36
37
38
39
40
41
42
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 34

def deliver
  # TODO If we ever do away with the `async: true` default for webhook generation, then I believe this needs to
  # change otherwise we'd be attempting the first delivery of webhooks inline.
  if delivery_attempts.new.attempt
    touch(:delivered_at)
  else
    deliver_async
  end
end

#deliver_asyncObject



28
29
30
31
32
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 28

def deliver_async
  if still_attempting?
    Webhooks::Outgoing::DeliveryJob.set(wait: next_reattempt_delay).perform_later(self)
  end
end

#delivered?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 48

def delivered?
  delivered_at.present?
end

#failed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 57

def failed?
  !(delivered? || still_attempting?)
end

#label_stringObject



20
21
22
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 20

def label_string
  event.short_uuid
end

#max_attemptsObject



65
66
67
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 65

def max_attempts
  ATTEMPT_SCHEDULE.keys.max
end

#nameObject



61
62
63
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 61

def name
  event.short_uuid
end

#next_reattempt_delayObject



24
25
26
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 24

def next_reattempt_delay
  ATTEMPT_SCHEDULE[attempt_count]
end

#still_attempting?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'app/models/concerns/webhooks/outgoing/delivery_support.rb', line 52

def still_attempting?
  return false if delivered?
  attempt_count < max_attempts
end