Ask A Question

Notifications

You’re not receiving notifications from this thread.

Sending Webhooks with Exponential Backoff Discussion

Hi,

love the episode!

Just a tiny remark; when running this code;

  def send_create_webhook!
    User.has_webhook_enabled.find_each do |user|
      SendWebhookJob.perform_later(user.webhook_url, {
        type: "tweet.created",
        id: id,
        body: body,
        user: {
          id: user_id,
          name: user.name,
        }
      })
    end
  end

Wouldn't that be sending the wrong user.name? You pass in the user that wants to receive the webhook and in the body, you refer to that webhook user, instead of the user that created the tweet.

I think refering to self.user.name would fix that.

Reply

Sidekiq has an automatic job retry with exponential backoff:
https://github.com/mperham/sidekiq/wiki/Error-Handling#automatic-job-retry

Why didn't you use that?

Reply

I make a change to use Sidekiq retry

class SendWebhookJob < ApplicationJob
WebhookNotFound = Class.new(StandardError)

retry_on WebhookNotFound
queue_as :default

MAX_RETRIES = 10

def perform(webhook_url, data)
puts "#{Time.zone.now} : Sending data to #{webhook_url}"

response = HTTP.post(webhook_url, json: data)
successful = response.code == 200 || raise(WebhookNotFound)

end
end

Reply

Awesome. Thanks you!

Reply

hi

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.