Class: Apisync::Rails::SyncModelJob::Sidekiq
- Inherits:
-
Object
- Object
- Apisync::Rails::SyncModelJob::Sidekiq
- Defined in:
- lib/apisync/rails/sync_model_job/sidekiq.rb
Instance Method Summary collapse
-
#perform(model_name, id, attributes, attempt = 1) ⇒ Object
include ::Sidekiq::Worker (deferred).
Instance Method Details
#perform(model_name, id, attributes, attempt = 1) ⇒ Object
include ::Sidekiq::Worker (deferred)
Sidekiq module is included in the Extensions class. We don’t do it here because we don’t know if Sidekiq is loaded or not. If it is not, we don’t want to include it in this class.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/apisync/rails/sync_model_job/sidekiq.rb', line 11 def perform(model_name, id, attributes, attempt = 1) unless defined?(::Sidekiq) raise ArgumentError, "Sidekiq is not defined but an ApiSync job is being spun up." end begin response = Apisync::Rails::Http.post( attributes, request_concurrency: :asynchronous, concurrency_lib: "Sidekiq #{::Sidekiq::VERSION}", too_many_requests_attempts: attempt.to_s ) unless response.success? raise Apisync::RequestFailed, "[apisync] Request failed: #{response.body}" end # When there are too many requests and ApiSync's API cannot take it, # this algorithm will push this job to be retried in the future. rescue Apisync::TooManyRequests ::Rails.logger.warn "[apisync] Too many simultaneous HTTP requests. Requests are being automatically throttled to solve this problem. Contact ApiSync support for details." retry_in = Random.new.rand(270) + 30 # 30 seconds - 5 minutes self.class.perform_in( retry_in, model_name, id, attributes, attempt.to_i + 1 ) end end |