Last active
April 12, 2025 14:39
-
-
Save pmatsinopoulos/e2fee3d4ce74be9614f152196668ec9e to your computer and use it in GitHub Desktop.
Calculate using Pessimistic Locking
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 CalculateUserBalanceJob < ApplicationJob | |
queue_as :default | |
def perform(user_id) | |
User.transaction do | |
user = User.lock.find(user_id) | |
puts "************** #{provider_job_id} Calculating balance for user: #{user.email}" | |
user_balance = user.user_balance | |
sleep rand(5..10) | |
new_balance = rand(5..50) | |
user_balance.update!(balance: new_balance, job_id: provider_job_id) | |
JobLog.create!(job_id: provider_job_id, balance: new_balance) | |
end | |
puts "................ #{provider_job_id} FINISHED #{provider_job_id}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment