1
0
مراية لـ https://github.com/postalserver/postal.git تم المزامنة 2025-11-30 21:32:30 +00:00

refactor: refactor webhook deliveries

هذا الالتزام موجود في:
Adam Cooke
2024-02-15 11:46:29 +00:00
ملتزم من قبل Adam Cooke
الأصل 6eb16c3fab
التزام e0403ba641
6 ملفات معدلة مع 209 إضافات و55 حذوفات

عرض الملف

@@ -2,18 +2,96 @@
class WebhookDeliveryService
def initialize(webhook_delivery:)
@webhook_delivery = webhook_delivery
RETRIES = { 1 => 2.minutes, 2 => 3.minutes, 3 => 6.minutes, 4 => 10.minutes, 5 => 15.minutes }.freeze
def initialize(webhook_request:)
@webhook_request = webhook_request
end
# TODO: move the logic from WebhookDelivery#deliver in to this service.
#
def call
if @webhook_delivery.deliver
log "Succesfully delivered"
else
log "Delivery failed"
logger.tagged(webhook: @webhook_request.webhook_id, webhook_request: @webhook_request.id) do
generate_payload
send_request
record_attempt
appreciate_http_result
update_webhook_request
end
end
def success?
@success == true
end
private
def generate_payload
@payload = {
event: @webhook_request.event,
timestamp: @webhook_request.created_at.to_f,
payload: @webhook_request.payload,
uuid: @webhook_request.uuid
}.to_json
end
def send_request
@http_result = Postal::HTTP.post(@webhook_request.url,
sign: true,
json: @payload,
timeout: 5)
@success = (@http_result[:code] >= 200 && @http_result[:code] < 300)
end
def record_attempt
@webhook_request.attempts += 1
if success?
@webhook_request.retry_after = nil
else
@webhook_request.retry_after = RETRIES[@webhook_request.attempts]&.from_now
end
@attempt = @webhook_request.server.message_db.webhooks.record(
event: @webhook_request.event,
url: @webhook_request.url,
webhook_id: @webhook_request.webhook_id,
attempt: @webhook_request.attempts,
timestamp: Time.now.to_f,
payload: @webhook_request.payload.to_json,
uuid: @webhook_request.uuid,
status_code: @http_result[:code],
body: @http_result[:body],
will_retry: @webhook_request.retry_after.present?
)
end
def appreciate_http_result
if success?
logger.info "Received #{@http_result[:code]} status code. That's OK."
@webhook_request.destroy!
@webhook_request.webhook&.update_column(:last_used_at, Time.current)
return
end
logger.error "Received #{@http_result[:code]} status code. That's not OK."
@webhook_request.error = "Couldn't send to URL. Code received was #{@http_result[:code]}"
end
def update_webhook_request
if @webhook_request.retry_after
logger.info "Will retry #{@webhook_request.retry_after} (this was attempt #{@webhook_request.attempts})"
@webhook_request.locked_by = nil
@webhook_request.locked_at = nil
@webhook_request.save!
return
end
logger.info "Have tried #{@webhook_request.attempts} times. Giving up."
@webhook_request.destroy!
end
def logger
Postal.logger
end
end