32 lines
914 B
Ruby
32 lines
914 B
Ruby
class TriggerWebhookJob < ApplicationJob
|
|
queue_as :default
|
|
retry_on StandardError, wait: :exponentially_longer, attempts: 3
|
|
|
|
def perform(webhook_config_id, payload)
|
|
webhook = WebhookConfig.find(webhook_config_id)
|
|
|
|
# Skip if webhook is not active
|
|
return unless webhook.active?
|
|
|
|
success = webhook.execute(payload)
|
|
|
|
if success
|
|
Rails.logger.info("Webhook #{webhook.name} triggered successfully")
|
|
else
|
|
Rails.logger.warn("Webhook #{webhook.name} failed")
|
|
raise StandardError, "Webhook execution failed" if attempts_count < webhook.retry_count
|
|
end
|
|
rescue ActiveRecord::RecordNotFound => e
|
|
Rails.logger.error("Webhook config not found: #{e.message}")
|
|
rescue StandardError => e
|
|
Rails.logger.error("Webhook trigger failed: #{e.message}")
|
|
raise if attempts_count < (webhook&.retry_count || 3)
|
|
end
|
|
|
|
private
|
|
|
|
def attempts_count
|
|
executions
|
|
end
|
|
end
|