Class: Desiru::Jobs::WebhookNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/desiru/jobs/webhook_notifier.rb

Overview

Handles webhook notifications for job events

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject (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

Parameters:

  • url (String)

    the webhook URL

  • payload (Hash)

    the payload to send

  • options (Hash) (defaults to: {})

    additional options

Returns:



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, options = {})
  uri = URI.parse(url)
  headers = config[:headers].merge(options[:headers] || {})

  # Add signature if secret is provided
  if options[:secret]
    signature = generate_signature(payload, options[: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.message
      Desiru.logger.error("Webhook error (attempt #{attempt}/#{config[:retry_count]}): #{e.message}")
    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