Class: Desiru::Jobs::WebhookNotifier
- Inherits:
-
Object
- Object
- Desiru::Jobs::WebhookNotifier
- Defined in:
- lib/desiru/jobs/webhook_notifier.rb
Overview
Handles webhook notifications for job events
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#initialize(config = {}) ⇒ WebhookNotifier
constructor
A new instance of WebhookNotifier.
-
#notify(url, payload, options = {}) ⇒ WebhookResult
Send a webhook notification.
Constructor Details
#initialize(config = {}) ⇒ WebhookNotifier
Returns a new instance of WebhookNotifier.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/desiru/jobs/webhook_notifier.rb', line 13 def initialize(config = {}) @config = { timeout: 30, retry_count: 3, retry_delay: 1, headers: { 'Content-Type' => 'application/json', 'User-Agent' => "Desiru/#{Desiru::VERSION}" } }.merge(config) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/desiru/jobs/webhook_notifier.rb', line 11 def config @config end |
Instance Method Details
#notify(url, payload, options = {}) ⇒ WebhookResult
Send a webhook notification
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/desiru/jobs/webhook_notifier.rb', line 30 def notify(url, payload, = {}) uri = URI.parse(url) headers = config[:headers].merge([:headers] || {}) # Add signature if secret is provided if [:secret] signature = generate_signature(payload, [:secret]) headers['X-Desiru-Signature'] = signature end attempt = 0 last_error = nil while attempt < config[:retry_count] attempt += 1 begin response = send_request(uri, payload, headers) if response.code.to_i >= 200 && response.code.to_i < 300 return WebhookResult.new( success: true, status_code: response.code.to_i, body: response.body, headers: response.to_hash, attempts: attempt ) else last_error = "HTTP #{response.code}: #{response.body}" Desiru.logger.warn("Webhook failed (attempt #{attempt}/#{config[:retry_count]}): #{last_error}") end rescue StandardError => e last_error = e. Desiru.logger.error("Webhook error (attempt #{attempt}/#{config[:retry_count]}): #{e.}") end # Retry with delay if not the last attempt if attempt < config[:retry_count] sleep(config[:retry_delay] * attempt) # Exponential backoff end end # All attempts failed WebhookResult.new( success: false, error: last_error, attempts: attempt ) end |