diff --git a/app/controllers/ajax/answer_controller.rb b/app/controllers/ajax/answer_controller.rb index 37bc1ec0..7c5b48ea 100644 --- a/app/controllers/ajax/answer_controller.rb +++ b/app/controllers/ajax/answer_controller.rb @@ -48,7 +48,9 @@ class Ajax::AnswerController < AjaxController end services = JSON.parse params[:share] - ShareWorker.perform_async(current_user.id, answer.id, services) + services.each do |service| + ShareWorker.perform_async(current_user.id, answer.id, service) + end @response[:status] = :okay diff --git a/app/workers/share_worker.rb b/app/workers/share_worker.rb index fc4eb5b9..d0964b3c 100644 --- a/app/workers/share_worker.rb +++ b/app/workers/share_worker.rb @@ -1,19 +1,19 @@ class ShareWorker include Sidekiq::Worker - sidekiq_options queue: :share, retry: false + sidekiq_options queue: :share, retry: 5 # @param user_id [Integer] the user id # @param answer_id [Integer] the user id - # @param services [Array] array containing strings - def perform(user_id, answer_id, services) - User.find(user_id).services.each do |service| - begin - service.post(Answer.find(answer_id)) if services.include? service.provider - rescue => e - logger.info "failed to post answer #{answer_id} to #{service.provider} for user #{user_id}: #{e.message}" - NewRelic::Agent.notice_error(e) - end - end + # @param service [String] the service to post to + def perform(user_id, answer_id, service) + service_type = "Services::#{service.camelize}" + user_service = User.find(user_id).services.find_by(type: service_type) + + user_service.post(Answer.find(answer_id)) + rescue => e + logger.info "failed to post answer #{answer_id} to #{service.provider} for user #{user_id}: #{e.message}" + NewRelic::Agent.notice_error(e) + raise end end