Last active
March 24, 2019 11:44
-
-
Save himangSharatun/b6487c197bdc506aa151ad8b7df3f698 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class MassUpdateWorker | |
include Sneakers::Worker | |
QUEUE_NAME = MassUpdatePublisher::QUEUE_NAME | |
from_queue QUEUE_NAME, arguments: { 'x-dead-letter-exchange': "#{QUEUE_NAME}-retry" } | |
def work(msg) | |
data = ActiveSupport::JSON.decode(msg) | |
data["mutations"].each do |mutation | | |
update_balance(mutation.to_h) | |
end | |
ack! | |
rescue StandardError => e | |
create_log(false, data, { message: e.message }) | |
reject! | |
end | |
private | |
def update_balance(mutation) | |
user = User.find_by(username: mutation["username"].to_s) | |
user.balance += mutation["amount"].to_i | |
user.save! | |
rescue StandardError => e | |
create_log(false, mutation, { message: e.message }) | |
end | |
def create_log(success, payload, message = {}) | |
message = { | |
success: success, | |
payload: payload | |
}.merge(message).to_json | |
severity = success ? :info : :error | |
Rails.logger.send(severity, message) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment