Class: LcpRuby::BackgroundJobs::Declarative::CallWebhookAction

Inherits:
BaseAction show all
Defined in:
lib/lcp_ruby/background_jobs/declarative/call_webhook_action.rb

Constant Summary collapse

RETRYABLE_STATUS_CODES =
[ 408, 429, 500, 502, 503, 504 ].freeze
PERMANENT_FAILURE_RANGE =
(400..499).freeze

Instance Attribute Summary

Attributes inherited from BaseHandler

#definition, #execution

Instance Method Summary collapse

Methods inherited from BaseAction

#config

Methods inherited from BaseHandler

#attach_result!, #check_cancellation!, #flush_log!, #initialize, #log!, #params, #set_result_url!, #target_record, #triggered_by, #update_progress!

Constructor Details

This class inherits a constructor from LcpRuby::BackgroundJobs::BaseHandler

Instance Method Details

#performObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lcp_ruby/background_jobs/declarative/call_webhook_action.rb', line 11

def perform
  url = resolve_url(config["url"])
  uri = validate_webhook_uri!(url)
  method = (config["method"] || "POST").upcase
  headers = resolve_headers(config["headers"] || {})
  body = resolve_body(config["body"])
  http_timeout = (config["timeout"] || 30).to_i
  expect_status = config["expect_status"]

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = http_timeout
  http.read_timeout = http_timeout

  request = build_request(method, uri, headers, body)

  response = http.request(request)
  handle_response(response, expect_status)
rescue UrlSafety::UnsafeUrlError => e
  log!("Blocked webhook URL: #{e.message}", level: :error)
  raise JobError, "Webhook URL is not allowed: #{e.message}"
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ETIMEDOUT,
       SocketError, Net::OpenTimeout, Net::ReadTimeout => e
  log!("Network error: #{e.class}: #{e.message}", level: :error)
  raise JobError, "Webhook network error: #{e.message}"
end