Last active
September 16, 2021 19:00
-
-
Save Unkas82/79293c13f63f06ecff8c4c0472d844b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Salesforce | |
# see more error classes | |
# in /gems/restforce-5-1-0/lib/restforce/error_code.rb | |
# Salesforce exceptions that should be ignored in Sentry. Not much we can do about this. These errors are temporary | |
# and further retries will likely fix them | |
RECOVERABLE_EXCEPTIONS = [ | |
Restforce::ErrorCode::RequestLimitExceeded, | |
Restforce::ErrorCode::QueryTimeout, | |
Restforce::ErrorCode::ServerUnavailable, | |
Restforce::ErrorCode::UnknownException | |
] | |
class SalesforceWorker | |
include Sidekiq::Worker | |
# Don't have a salesforce queue so we'll put it on the intercom queue for now | |
sidekiq_options queue: 'intercom', retry: Sidekiq::RETRY_24_HOURS | |
# see notes on ExternalServiceWorker for why we do silly magic | |
def self.inherited(subclass) | |
subclass.class_eval { prepend ::ExternalServiceWorker.new(RECOVERABLE_EXCEPTIONS) } | |
end | |
def salesforce | |
::Salesforce.client | |
end | |
# Save the salesforce_id for a resource | |
def store_salesforce_id(resource:, salesforce_id:) | |
return unless resource.respond_to?(:salesforce_id) | |
return unless resource.salesforce_id != salesforce_id | |
resource.update_column(:salesforce_id, salesforce_id) | |
end | |
end | |
end | |
# For example: | |
# | |
# RECOVERABLE_EXCEPTIONS = [::Intercom::ServiceConnectionError, ::Intercom::BadGatewayError, ::Intercom::ServerError] | |
# class IntercomWorker | |
# include Sidekiq::Worker | |
# def self.inherited(subclass) | |
# subclass.class_eval { prepend ::ExternalServiceWorker.new(RECOVERABLE_EXCEPTIONS) } | |
# end | |
# end | |
# | |
class ExternalServiceWorker < Module | |
def initialize(recoverable_exceptions = []) | |
@recoverable_exceptions = recoverable_exceptions | |
end | |
def prepended(includer_class) | |
# define a local var so the define_method closure will capture it | |
recoverable_exceptions = @recoverable_exceptions | |
define_method(:perform) do |*args| | |
begin | |
super(*args) #calls worker perform | |
rescue *recoverable_exceptions | |
# SentryIgnorableError is ignored by sentry but | |
# the exception correctly results in a sidekiq retry | |
raise SentryIgnorableError | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment