Class: Sidekiq::TransactionAwareClient

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/transaction_aware_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(pool: nil, config: nil) ⇒ TransactionAwareClient

Returns a new instance of TransactionAwareClient.



8
9
10
11
12
13
14
15
16
# File 'lib/sidekiq/transaction_aware_client.rb', line 8

def initialize(pool: nil, config: nil)
  @redis_client = Client.new(pool: pool, config: config)
  @transaction_backend =
    if ActiveRecord.version >= Gem::Version.new("7.2")
      ActiveRecord.method(:after_all_transactions_commit)
    else
      AfterCommitEverywhere.method(:after_commit)
    end
end

Instance Method Details

#batching?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/sidekiq/transaction_aware_client.rb', line 18

def batching?
  Thread.current[:sidekiq_batch]
end

#push(item) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/sidekiq/transaction_aware_client.rb', line 22

def push(item)
  # 6160 we can't support both Sidekiq::Batch and transactions.
  return @redis_client.push(item) if batching?

  # 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)
  @transaction_backend.call { @redis_client.push(item) }
  item["jid"]
end

#push_bulk(items) ⇒ Object

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.



37
38
39
# File 'lib/sidekiq/transaction_aware_client.rb', line 37

def push_bulk(items)
  @redis_client.push_bulk(items)
end