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
# frozen_string_literal: true | |
module EnqueueableJob | |
extend ActiveSupport::Concern | |
included do | |
def process_enqueued_job(queueable:) | |
enqueued_job = nil | |
ActiveRecord::Base.transaction do |
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 LongRunningBatchJob < ApplicationJob | |
queue_as :default | |
include EnqueueableJob | |
def perform(batch_id) | |
batch = Batch.find(batch_id) | |
process_enqueued_job(queueable: batch) do | |
# do the long running batch job |
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
# frozen_string_literal: true | |
module Queueable | |
extend ActiveSupport::Concern | |
included do | |
def enqueue(job_class_name:, schedule_options: {}) | |
result = false | |
ActiveRecord::Base.transaction do |
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 EnqueuedJob < ApplicationRecord | |
belongs_to :queueable, polymorphic: true | |
end |
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
ADVISORY_LOCK_NAMESPACES = { | |
create_user_job: 1 | |
} | |
class CreateUserJob < ApplicationJob | |
queue_as :default | |
def perform(user_email) | |
ActiveRecord::Base.transaction do | |
# 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 CreateUserJob < ApplicationJob | |
queue_as :default | |
def perform(user_email) | |
puts "************** #{provider_job_id} About to create user: #{user_email}" | |
found = User.find_by(email: user_email) | |
return if found |
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 |
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 = User.find(user_id) | |
puts "Calculating balance for user: #{user.email}" | |
user_balance = user.user_balance |
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
user = User.find_by!(email: "[email protected]") | |
puts "Initial user_balance: #{user.user_balance.balance}" | |
5.times do | |
CalculateUserBalanceJob.perform_later(user.id) | |
end | |
sleep(60) # just wait for the background job to finish. This is long enough | |
# for all the 5 Sidekiq jobs to finish |
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 = User.find(user_id) | |
puts "#{Thread.current.object_id} -> Calculating balance for user: #{user.email}" | |
user_balance = user.user_balance |
NewerOlder