Class: Webhooks::Outgoing::Delivery

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/webhooks/outgoing/delivery.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



53
54
55
# File 'app/models/webhooks/outgoing/delivery.rb', line 53

def attempt_count
  delivery_attempts.count
end

#deliverObject



45
46
47
48
49
50
51
# File 'app/models/webhooks/outgoing/delivery.rb', line 45

def deliver
  if delivery_attempts.create.attempt
    touch(:delivered_at)
  else
    deliver_async
  end
end

#deliver_asyncObject



39
40
41
42
43
# File 'app/models/webhooks/outgoing/delivery.rb', line 39

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

#delivered?Boolean

Returns:

  • (Boolean)


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

def delivered?
  delivered_at.present?
end

#failed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/webhooks/outgoing/delivery.rb', line 66

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

#label_stringObject

🚅 add delegations above.



31
32
33
# File 'app/models/webhooks/outgoing/delivery.rb', line 31

def label_string
  event.short_uuid
end

#max_attemptsObject



74
75
76
# File 'app/models/webhooks/outgoing/delivery.rb', line 74

def max_attempts
  ATTEMPT_SCHEDULE.keys.max
end

#nameObject



70
71
72
# File 'app/models/webhooks/outgoing/delivery.rb', line 70

def name
  event.short_uuid
end

#next_reattempt_delayObject



35
36
37
# File 'app/models/webhooks/outgoing/delivery.rb', line 35

def next_reattempt_delay
  ATTEMPT_SCHEDULE[attempt_count]
end

#still_attempting?Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'app/models/webhooks/outgoing/delivery.rb', line 61

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