Class: WebhookSystem::Job

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/webhook_system/job.rb

Overview

This is the ActiveJob in charge of actually sending each event

Defined Under Namespace

Classes: ErrorResponse, RequestFailed

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_clientObject



88
89
90
91
92
93
94
95
# File 'lib/webhook_system/job.rb', line 88

def self.build_client
  Faraday.new do |faraday|
    faraday.response :logger if ENV['WEBHOOK_DEBUG']
    # use Faraday::Encoding middleware
    faraday.response :encoding
    faraday.adapter Faraday.default_adapter
  end
end

.build_request(client, subscription, event) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/webhook_system/job.rb', line 59

def self.build_request(client, subscription, event)
  payload, headers = Encoder.encode(subscription.secret, event, format: format_for_subscription(subscription))
  client.build_request(:post) do |req|
    req.url subscription.url
    req.headers.merge!(headers)
    req.body = payload.to_s
  end
end

.ensure_success(response) ⇒ Object



52
53
54
55
56
57
# File 'lib/webhook_system/job.rb', line 52

def self.ensure_success(response)
  status = response.status
  unless (200..299).cover? status
    raise RequestFailed.new("request failed with code: #{status}", status)
  end
end

.format_for_subscription(subscription) ⇒ Object



68
69
70
# File 'lib/webhook_system/job.rb', line 68

def self.format_for_subscription(subscription)
  subscription.encrypt ? 'base64+aes256' : 'json'
end

.log_response(subscription, event, request, response) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/webhook_system/job.rb', line 72

def self.log_response(subscription, event, request, response)
  event_log = EventLog.construct(subscription, event, request, response)

  # we write log in a separate thread to make sure it is created even if the whole job fails
  # Usually any background job would be wrapped into transaction,
  # so if the job fails we would rollback any DB changes, including the even log record.
  # We want the even log record to always be created, so we check if we are running inside the transaction,
  # if we are - we create the record in a separate thread. New Thread means a new DB connection and
  # ActiveRecord transactions are per connection, which gives us the "transaction jailbreak".
  if ActiveRecord::Base.connection.open_transactions == 0
    event_log.save!
  else
    Thread.new { event_log.save! }.join
  end
end

.post(subscription, event) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/webhook_system/job.rb', line 37

def self.post(subscription, event)
  client = build_client
  request = build_request(client, subscription, event)

  response =
    begin
      client.builder.build_response(client, request)
    rescue RuntimeError => exception
      ErrorResponse.new(exception)
    end

  log_response(subscription, event, request, response)
  ensure_success(response)
end

Instance Method Details

#perform(subscription, event) ⇒ Object



33
34
35
# File 'lib/webhook_system/job.rb', line 33

def perform(subscription, event)
  self.class.post(subscription, event)
end