1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/transaction_aware_client.rb

45 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2022-05-06 13:52:38 -07:00
require "securerandom"
require "sidekiq/client"
module Sidekiq
class TransactionAwareClient
def initialize(pool: nil, config: nil)
@redis_client = Client.new(pool: pool, config: config)
end
def push(item)
2022-05-06 13:52:38 -07:00
# pre-allocate the JID so we can return it immediately and
# save it to the database as part of the transaction.
item["jid"] ||= SecureRandom.hex(12)
AfterCommitEverywhere.after_commit { @redis_client.push(item) }
2022-05-06 13:52:38 -07:00
item["jid"]
end
##
# We don't provide transactionality for push_bulk because we don't want
# to hold potentially hundreds of thousands of job records in memory due to
# a long running enqueue process.
def push_bulk(items)
@redis_client.push_bulk(items)
end
end
end
##
# Use `Sidekiq.transactional_push!` in your sidekiq.rb initializer
module Sidekiq
def self.transactional_push!
2022-05-06 13:52:38 -07:00
begin
require "after_commit_everywhere"
rescue LoadError
raise %q(You need to add `gem "after_commit_everywhere"` to your Gemfile to use Sidekiq's transactional client)
2022-05-06 13:52:38 -07:00
end
Sidekiq.default_job_options["client_class"] = Sidekiq::TransactionAwareClient
2022-05-06 13:52:38 -07:00
Sidekiq::JobUtil::TRANSIENT_ATTRIBUTES << "client_class"
true
end
end