Class: Webhooks::CallbackUrlJob

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/sidekiq/webhooks/callback_url_job.rb

Constant Summary collapse

MAX_BODY_LENGTH =

denotes the how large of a body from the client url we record in our db.

500

Instance Method Summary collapse

Instance Method Details

#create_attempt(successful, response) ⇒ Object (private)



64
65
66
67
68
69
70
71
# File 'app/sidekiq/webhooks/callback_url_job.rb', line 64

def create_attempt(successful, response)
  attempt = Webhooks::NotificationAttempt.new do |a|
    a.success = successful
    a.response = response
  end
  attempt.save!
  attempt
end

#create_attempt_assoc(notification, attempt) ⇒ Object (private)



73
74
75
76
77
78
79
80
# File 'app/sidekiq/webhooks/callback_url_job.rb', line 73

def create_attempt_assoc(notification, attempt)
  attempt_assoc = Webhooks::NotificationAttemptAssoc.new do |naa|
    naa.webhooks_notification_id = notification.id
    naa.webhooks_notification_attempt_id = attempt.id
  end
  attempt_assoc.save!
  attempt_assoc
end

#notifyObject (private)



24
25
26
27
28
29
30
31
32
33
34
# File 'app/sidekiq/webhooks/callback_url_job.rb', line 24

def notify
  @response = Faraday.post(@url, @msg.to_json, 'Content-Type' => 'application/json')
rescue Faraday::ClientError, Faraday::Error => e
  Rails.logger.error("Webhooks::CallbackUrlJob Error in CallbackUrlJob #{e.message}", e)
  @response = e
rescue => e
  Rails.logger.error("Webhooks::CallbackUrlJob unexpected Error in CallbackUrlJob #{e.message}", e)
  @response = e
ensure
  record_attempt
end

#perform(url, ids, max_retries) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/sidekiq/webhooks/callback_url_job.rb', line 9

def perform(url, ids, max_retries)
  @url = url
  @ids = ids
  @max_retries = max_retries
  r = Webhooks::Notification.where(id: ids)
  @msg = { 'notifications' => [] }
  r.each do |notification|
    @msg['notifications'] << notification.msg
  end
  Rails.logger.info "Webhooks::CallbackUrlJob Notifying on callback url #{url} for ids #{ids} with msg #{@msg}"
  notify
end

#record_attemptObject (private)



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
# File 'app/sidekiq/webhooks/callback_url_job.rb', line 36

def record_attempt
  ActiveRecord::Base.transaction do
    successful = false
    if @response.respond_to? :success?
      successful = @response.success?
      attempt_response = { 'status' => @response.status, 'body' => @response.body[0...MAX_BODY_LENGTH] }
    else
      attempt_response = { 'exception' => @response.message }
    end

    # create the notification attempt record
    attempt = create_attempt(successful, attempt_response)

    # write an association record tied to each notification used in this attempt
    Webhooks::Notification.where(id: @ids).find_each do |notification|
      create_attempt_assoc(notification, attempt)

      # seal off the attempt if we received a successful response or hit our max retry limit
      if attempt.success? || notification.webhooks_notification_attempts.count >= @max_retries
        notification.final_attempt_id = attempt.id
      end

      notification.processing = nil
      notification.save!
    end
  end
end