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 حذوفات

عرض الملف

@@ -28,8 +28,6 @@ class WebhookRequest < ApplicationRecord
include HasUUID
include HasLocking
RETRIES = { 1 => 2.minutes, 2 => 3.minutes, 3 => 6.minutes, 4 => 10.minutes, 5 => 15.minutes }.freeze
belongs_to :server
belongs_to :webhook, optional: true
@@ -38,48 +36,6 @@ class WebhookRequest < ApplicationRecord
serialize :payload, Hash
def deliver
payload = { event: event, timestamp: created_at.to_f, payload: self.payload, uuid: uuid }.to_json
Postal.logger.tagged(event: event, url: url) do
Postal.logger.info "Sending webhook request"
result = Postal::HTTP.post(url, sign: true, json: payload, timeout: 5)
self.attempts += 1
self.retry_after = RETRIES[self.attempts]&.from_now
server.message_db.webhooks.record(
event: event,
url: url,
webhook_id: webhook_id,
attempt: self.attempts,
timestamp: Time.now.to_f,
payload: self.payload.to_json,
uuid: uuid,
status_code: result[:code],
body: result[:body],
will_retry: (retry_after ? 0 : 1)
)
if result[:code] >= 200 && result[:code] < 300
Postal.logger.info "Received #{result[:code]} status code. That's OK."
destroy!
webhook&.update_column(:last_used_at, Time.now)
true
else
Postal.logger.error "Received #{result[:code]} status code. That's not OK."
self.error = "Couldn't send to URL. Code received was #{result[:code]}"
if retry_after
Postal.logger.info "Will retry #{retry_after} (this was attempt #{self.attempts})"
self.locked_by = nil
self.locked_at = nil
save!
else
Postal.logger.info "Have tried #{self.attempts} times. Giving up."
destroy!
end
false
end
end
end
class << self
def trigger(server, event, payload = {})

عرض الملف

@@ -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